Does deleting a branch in git remove it from the history?

后端 未结 3 549
一整个雨季
一整个雨季 2020-11-27 10:38

Coming from svn, just starting to become familiar with git.

When a branch is deleted in git, is it removed from the history?

In svn, you can easily recover

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 11:10

    In Git, branches are just pointers (references) to commits in a directed acyclic graph (DAG) of commits. This means that deleting a branch removes only references to commits, which might make some commits in the DAG unreachable, thus invisible. But all commits that were on a deleted branch would still be in the repository, at least until unreachable commits get pruned (e.g. using git gc).

    Note that git branch -d would refuse to delete a branch if it cannot be sure that deleting it wouldn't leave unreachable commits. You need to use the stronger git branch -D to force deletion of a branch if it might leave unreachable commits.

    Note also that unreachable commits, if they are present, are only those commits between the last tip of a deleted branch and either a commit that got merged to another existing branch, any tagged commit, or the branching point; whichever is later. For example in the following situation:

    ----O----*----*----/M----*    <-- master <-- HEAD
         \            /
          \--.----.--/--x---y     <-- deleted branch
    

    only commits 'x' and 'y' would become unreachable after deleting the branch.

    If you operated on a deleted branch within the gc.reflogExpire period, default 90 days, you would have the last tip of a deleted branch recorded in HEAD reflog (see git reflog show HEAD, or git log --oneline --walk-reflogs HEAD). You should be able to use HEAD reflog to recover the deleted pointer. Note also that in this case, unreachable commits in just a deleted branch would be protected from pruning (removing) within the gc.reflogExpireUnreachable period, which by default is 30 days.

    If you can't find the tip of a just deleted branch in reflog for HEAD, you can try to use git fsck to find "unreachable commit ", and examine those (via git show or git log ) to find the tip of the deleted branch.

    Independent on how you find the tip of a deleted branch, you can undo deletion, or rather re-create a just deleted branch using

    git branch  
    

    Note however that reflog for a branch would be lost.


    There is also git-resurrect.sh script in contrib/ which helps find traces of a branch tip with given name and resurrect (undelete) it.

提交回复
热议问题