Can I recover a branch after its deletion in Git?

后端 未结 20 2094
广开言路
广开言路 2020-11-22 05:53

If I run git branch -d XYZ, is there a way to recover the branch? Is there a way to go back as if I didn\'t run the delete branch command?

相关标签:
20条回答
  • 2020-11-22 06:31

    If you removed the branch and forgot it's commit id you can do this command:

    git log --graph --decorate $(git rev-list -g --all)
    

    After this you'll be able to see all commits. Then you can do git checkout to this id and under this commit create a new branch.

    0 讨论(0)
  • 2020-11-22 06:33

    IF you are using VSCode... and you synced your branch with the server at some point before deleting it...

    Note that git branch delete only deletes the local copy, not the copy on the server. First, in the Git panel (git icon on left toolbar), look through the branches and see if your branch is still there under "origin/your_branch_name". If so, just select that and you should get your code back (suggest that you immediately copy/paste/save it locally somewhere else).

    If you didn't see an "origin/your_branch_name", Install the GitLens extension. This allows you to visually poke around in the server repositories and locate the copy you synced to the server. If you have multiple repositories, note that it might be necessary to have at least one file opened from the desired repository in order to make the repository appear in GitLens. Then:

    1. Open the GitLens panel

    2. Expand the repository

    3. You should see a list of categories: Branches / Contributors / Remotes / Stashes / etc

    You should find YourLostTreasure under "Branches" or possibly under "Remotes -> Origins". Hopefully, you will see a branch with the desired name - if you expand it, you should see the files you changed in that branch. Double-click the file names to open them, and immediately back up that code.

    If you don't immediately see your lost branch, poke around and if you find something promising, immediately open it and grab the code. I had to poke around quite a bit until I found TheGoldenBranch, and even then the code was missing the last one or two saves (possibly because I failed to sync to server before attempting-a-Branch-Merge-but-accidentally-clicking-Branch-Delete). My search was unnecessarily lengthened because when I first found the branch I wasn't completely sure the name was correct so kept looking, and it took some time to re-find that first branch. (Thus, Carpe Carpum and then keep looking.)

    0 讨论(0)
  • 2020-11-22 06:36

    A related issue: I came to this page after searching for "how to know what are deleted branches".

    While deleting many old branches, felt I mistakenly deleted one of the newer branches, but didn't know the name to recover it.

    To know what branches are deleted recently, do the below:

    If you go to your Git URL, which will look something like this:

    https://your-website-name/orgs/your-org-name/dashboard
    

    Then you can see the feed, of what is deleted, by whom, in the recent past.

    0 讨论(0)
  • 2020-11-22 06:37

    Just using git reflog did not return the sha for me. Only the commit id (which is 8 chars long and a sha is way longer)

    So I used git reflog --no-abbrev

    And then do the same as mentioned above: git checkout -b <branch> <sha>

    0 讨论(0)
  • 2020-11-22 06:38

    I used the following commands to find and retrieve my deleted branch. The first steps are from gcb's description.

    $ git fsck --full --no-reflogs --unreachable --lost-found > lost
    $ cat lost | cut -d\  -f3 > commits
    $ cat commits | xargs -n 1 git log -n 1 --pretty=oneline
    

    Now look for the git commit id (GIT-SHA) based on the commit comments and use it in the command below. Checkout a new branch called NEW-BRANCH with the previously found GIT-SHA:

    $ git checkout -b NEW-BRANCH GIT-SHA
    
    0 讨论(0)
  • 2020-11-22 06:38

    If you don't have a reflog, eg. because you're working in a bare repository which does not have the reflog enabled and the commit you want to recover was created recently, another option is to find recently created commit objects and look through them.

    From inside the .git/objects directory run:

    find . -ctime -12h -type f | sed 's/[./]//g' | git cat-file --batch-check | grep commit
    

    This finds all objects (commits, files, tags etc.) created in the last 12 hours and filters them to show only commits. Checking these is then a quick process.

    I'd try the git-ressurect.sh script mentioned in Jakub's answer first though.

    0 讨论(0)
提交回复
热议问题