Can I recover a branch after its deletion in Git?

后端 未结 20 2116
广开言路
广开言路 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:38

    First go to git batch the move to your project like :

    cd android studio project
    cd Myproject
    then type :
    git reflog
    

    You all have a list of the changes and the reference number take the ref number then checkout
    from android studio or from the git betcha. another solution take the ref number and go to android studio click on git branches down then click on checkout tag or revision past the reference number then lol you have the branches.

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

    Adding to tfe answer: there is also the git-resurrect.sh script in the contrib/ area of the Git sources (in git.git repository), which might help you.

    git-resurrect <name> attempts to find traces of a branch tip called <name>, and tries to resurrect it. Currently, the reflog is searched for checkout messages, and with -r also merge messages. With -m and -t, the history of all refs is scanned for Merge <name> into other/Merge <other> into <name> (respectively) commit subjects, which is rather slow but allows you to resurrect other people's topic branches.

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

    Make sure to perform all of this locally, and confirm your repo is in the state you desire before pushing to Bitbucket Cloud. It may also be a good idea to clone your current repo, and test these solutions out first.

    1. If you just deleted the branch, you'll see something like this in your terminal:
        Deleted branch <your-branch> (was <sha>)
    

    2.To restore the branch, use:

        git checkout -b <branch> <sha>
    

    If you don't know the 'sha' off the top of your head, you can:

    1. Find the 'sha' for the commit at the tip of your deleted branch using:
        git reflog
    
    1. To restore the branch, use:
        git checkout -b <branch> <sha>
    

    If your commits are not in your reflog:

    1. You can try recovering a branch by reseting your branch to the sha of the commit found using a command like:
        git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt
    

    2.You can then display each commit using one of these:

        git log -p <commit>
        git cat-file -p <commit>
    
    0 讨论(0)
  • 2020-11-22 06:43

    From my understanding if the branch to be deleted can be reached by another branch, you can delete it safely using

    git branch -d [branch]
    

    and your work is not lost. Remember that a branch is not a snapshot, but a pointer to one. So when you delete a branch you delete a pointer.

    You won't even lose work if you delete a branch which cannot be reached by another one. Of course it won't be as easy as checking out the commit hash, but you can still do it. That's why Git is unable to delete a branch which cannot be reached by using -d. Instead you have to use

    git branch -D [branch]
    

    This is part of a must watch video from Scott Chacon about Git. Check minute 58:00 when he talks about branches and how delete them.

    Introduction to Git with Scott Chacon of GitHub

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

    Adding to tfe's answer, you can recover with this process mentioned, unless it's commits are not garbage collected. Git branch is simply a pointer to a particular commit in the commit tree. But if you delete the pointer, and the commits on that branch are not merged into other existing branch, then git treats it as dangling commits and removes them during garbage collection, which it may run automatically periodically.

    If your branch wasn't merged to an existing branch, and if it was garbage collected, then you will loose all commits up until the point from where branch was forked from an existing branch.

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

    If you like to use a GUI, you can perform the entire operation with gitk.

    gitk --reflog
    

    This will allow you to see the branch's commit history as if the branch hadn't been deleted. Now simply right click on the most recent commit to the branch and select the menu option Create new branch.

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