Difference between `git branch -f ` and `git checkout ; git reset --hard ` under a clean working tree?

前端 未结 1 928
忘了有多久
忘了有多久 2021-02-07 05:58

Up until now, I have always used git checkout ; git reset --hard to move a branch back to an earlier commit.

Then I came acr

相关标签:
1条回答
  • 2021-02-07 06:26

    The main difference is that git branch -f <branchname> <commitref> moves <branchname> to point the specified commit without touching HEAD, the index or the working copy, while git checkout <branchname> && git reset --hard <commitref> modifies all three.

    If you want to quickly rearrange branches without moving HEAD or modifying your current working tree, then git branch -f is a good way to do it. It will also work if you have uncommitted changes, which isn't always possible if you use git checkout.

    Another difference is related to performance, but it's only relevant for very large projects.
    In those cases, modifying your working tree with git checkout and git reset --hard could potentially be an expensive operation with lots of disk I/O. On the other hand, with git branch -f only a single file will be written on disk, i.e. the one that contains the <commithash> referenced by <branchname>.

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