git checkout< commit-hash> vs git checkout分支 [英] git checkout <commit-hash> vs git checkout branch

查看:110
本文介绍了git checkout< commit-hash> vs git checkout分支的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在玩git,在这里感到困惑.

I was playing around with git and got confused here.

develop分支的 HEAD 位于
235a6d8

The HEAD of develop branch is at
235a6d8

当我这样做时:

git checkout 235a6d8

来自任何其他分支 develop分支,这使我陷入了孤立的头脑.
我不确定在签出该分支的最新提交时为什么会发生这种情况.

from any other branch or from develop branch, this leaves me in detached head.
I am not sure why does this happen when I am checking out to the latest commit on this branch.

当我这样做时:

git checkout develop

我可以正确切换到开发分支.

I can switch to develop branch correctly.

我没有得到 git checkout <commit-has> git checkout branchname 之间的区别.
它们有何不同?

I am not getting the difference between git checkout <commit-has> and git checkout branchname.
How they are different ?

推荐答案

A ),并更新工作树中的索引和文件.

A git checkout <commit-hash>, Prepare to work on top of <commit>, by detaching HEAD at it (see "DETACHED HEAD" section), and updating the index and the files in the working tree.

git checkout <branch>进行切换时:它准备在<branch>上工作,通过更新工作树中的索引和文件,并将HEAD指向分支来切换到它.

While a git checkout <branch> does a switch: it prepares for working on <branch>, switch to it by updating the index and the files in the working tree, and by pointing HEAD at the branch.

这令人困惑.

Mark Longair 在"git checkout"的分支?"

Mark Longair documented that confusion in "Why is the git command to switch branches named "git checkout"?"

他还在2012年5月写道:"最令人困惑的git术语":

He also wrote in May 2012: "The most confusing git terminology":

在CVS和Subversion中,签出"会创建链接到该存储库的源代码的新本地副本.
Git中最接近的命令是"git clone".
但是,在git中,"git checkout"用于完全不同的内容.
实际上,它具有两种截然不同的操作模式:

In CVS and Subversion "checkout" creates a new local copy of the source code that is linked to that repository.
The closest command in Git is "git clone".
However, in git, "git checkout" is used for something completely distinct.
In fact, it has two largely distinct modes of operation:

  • 在用法git checkout <branch>中,要切换HEAD使其指向新的分支或提交.如果<branch>确实是本地分支,它将切换到该分支(即HEAD将指向引用名称),或者如果它解析为提交将分离HEAD并将其直接指向提交的对象名称.
  • 将工作副本和索引中的一个或多个文件替换为来自特定提交或索引的内容.
    在用法中可以看到这一点:git checkout -- (update from the index)git checkout <tree-ish> --(其中<tree-ish>通常是提交).
  • To switch HEAD to point to a new branch or commit, in the usage git checkout <branch>. If <branch> is genuinely a local branch, this will switch to that branch (i.e. HEAD will point to the ref name) or if it otherwise resolves to a commit will detach HEAD and point it directly to the commit’s object name.
  • To replace a file or multiple files in the working copy and the index with their content from a particular commit or the index.
    This is seen in the usages: git checkout -- (update from the index) and git checkout <tree-ish> -- (where <tree-ish> is typically a commit).

在我理想的世界中,这两种操作方式将具有不同的动词,而且都不是"checkout" .

In my ideal world, these two modes of operation would have different verbs, and neither of them would be "checkout".

好吧... 这就是为什么Git 2.23(2019年第三季度)会将结帐拆分为:

Well... That is why Git 2.23 (Q3 2019) will split checkout into:

  • git restore 会更新工作树(可能还有索引)
  • git switch 可以切换分支,或在需要时分离分支,以便添加所有新的提交到该分支的顶端.

这篇关于git checkout&lt; commit-hash&gt; vs git checkout分支的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆