git reset是关于更新索引,移动HEAD。
Git签出是关于更新工作树(到索引或指定的树)。只有当您签出一个分支时,它才会更新HEAD(否则,您最终会得到一个分离的HEAD)。
(实际上,对于Git 2.23 Q3 2019,这将是Git恢复,而不一定是Git检出)
相比之下,由于svn没有索引,只有一个工作树,svn checkout将把给定的修订版复制到一个单独的目录上。
git签出的类似内容如下:
svn update(如果你在同一个分支,意味着相同的svn URL)
svn switch(如果您签出同一个分支,但是从另一个svn repo URL)
所有这三种工作树修改(svn checkout, update, switch)在git中只有一个命令:git checkout。
但是因为git也有索引的概念(在repo和工作树之间的“暂存区域”),你也需要git重置。
Thinkeye在评论中提到了文章“Reset Demystified”。
For instance, if we have two branches, 'master' and 'develop' pointing at different commits, and we're currently on 'develop' (so HEAD points to it) and we run git reset master, 'develop' itself will now point to the same commit that 'master' does.
On the other hand, if we instead run git checkout master, 'develop' will not move, HEAD itself will. HEAD will now point to 'master'.
So, in both cases we're moving HEAD to point to commit A, but how we do so is very different. reset will move the branch HEAD points to, checkout moves HEAD itself to point to another branch.
不过,在这些方面:
LarsH在评论中补充道:
然而,这个答案的第一段是误导性的:“git checkout…只有当你签出分支时才会更新HEAD(如果没有,你最终会得到一个分离的HEAD)”。
不对:git checkout会更新HEAD,即使你签出了一个不是分支的提交(是的,你最终得到了一个分离的HEAD,但它仍然被更新了)。
git checkout a839e8f更新HEAD指向提交a839e8f。
新竞赛在评论中:
@LarsH is correct.
The second bullet has a misconception about what HEAD is in will update the HEAD only if you checkout a branch.
HEAD goes wherever you are, like a shadow.
Checking out some non-branch ref (e.g., a tag), or a commit directly, will move HEAD. Detached head doesn't mean you've detached from the HEAD, it means the head is detached from a branch ref, which you can see from, e.g., git log --pretty=format:"%d" -1.
Attached head states will start with (HEAD ->,
detached will still show (HEAD, but will not have an arrow to a branch ref.