gpt4 book ai didi

git checkout 与 git checkout 分支

转载 作者:太空狗 更新时间:2023-10-29 12:52:27 25 4
gpt4 key购买 nike

我在玩 git 并在这里感到困惑。

developHEAD分行位于
235a6d8

当我这样做时:

git checkout 235a6d8

来自任何其他分支机构 或来自 develop分支,这让我保持独立的头脑。
我不确定为什么在我检查该分支上的最新提交时会发生这种情况。

当我这样做时:

git checkout develop

我可以正确切换到 develop 分支。

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

最佳答案

A git checkout <commit-hash> ,准备在<commit>之上工作,通过分离它的 HEAD(参见“DETACHED HEAD" section”),并更新工作树中的索引和文件。

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

这令人困惑。

Mark Longair在“Why is the git command to switch branches named “ git checkout ”?”中记录了这种混淆

他还在 2012 年 5 月写道:“The most confusing git terminology”:

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:

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

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

嗯...这就是为什么 Git 2.23(2019 年第 3 季度)会将 checkout 拆分为:

  • git restore 更新工作树(可能还有索引)
  • git switch 它可以切换分支,或者根据要求分离一个分支,以便将所有新提交添加到该分支的顶端。

关于git checkout <commit-hash> 与 git checkout 分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57123031/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com