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

喜欢而已 提交于 2019-12-04 17:11:37

问题


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

Then I came across this question, but the answers and comments do not explain in great detail the differences between them.

Assuming I have a clean working tree, what internal differences are there between

git branch -f <branch_name> <hash>

and

git checkout <branch_name>
git reset --hard <hash>

and do such differences, if any, have any subtle implications for advanced usage?


回答1:


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>.



来源:https://stackoverflow.com/questions/28149804/difference-between-git-branch-f-branch-name-hash-and-git-checkout-branc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!