为什么切换分支的 git 命令名为“git checkout”?
为什么切换分支的 git 命令名为 git checkout
?
这真的有意义吗?
我将其命名为 git switch
。此外,git checkout
还有其他含义:例如恢复文件(如svn revert
)
Why is the git command to switch branches named git checkout
?
Does it really make sense ?
I would name it git switch
instead. Besides, git checkout
has other meanings: e.g. reverting a file (like svn revert
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我看到大多数其他答案都在解释 git checkout 的作用以及为什么“checkout”可能是描述它的合理方式。然而,虽然我非常喜欢 git,但这确实触及了当我试图帮助人们理解该系统时出现的两个严重的挫败点:
git checkout
做了两件截然不同的事情,如果它们是单独的命令,这对新手会有帮助。愤世嫉俗的人可能会认为 git 的术语是故意选择的,目的是为了迷惑来自 CVS 和 Subversion 的人!您提到的(
checkout
)就是一个很好的例子。另一个是commit
,它在 git 中完全是本地的,并且完全依赖于 CVS / SVN 中的服务器 - “记录”的 Darcs 术语对于刚接触 git 的人来说不需要太多的学习。我喜欢的另一个例子是您在 git 中看到的消息“需要更新”,这实际上意味着 “需要提交” :)当然,人们总是可以使用不同的前端来 git,例如 简单的 git,iolaus等等,但无论如何,大多数人最终都必须学习标准命令,所以你只需要习惯其中一些命令的命名是相当令人惊讶的。
我确信 git 中这些不同命令的名称有历史原因,但如果选择不同的单词将会很有帮助......
更新: VonC 链接到 一个带有简洁别名的答案,可以使
git checkout
在其两种用法中更安全;)I see that most other answers are explaining what
git checkout
does and why "checkout" might be a reasonable way to describe that. However, while I love git dearly, this does touch on two serious points of frustration that arise when I'm trying to help people to understand the system:git checkout
does two very distinct things, and it would be helpful for newcomers if they were separate commands.A cynic might suggest that git's terminology was deliberately chosen to confuse people coming from CVS and Subversion! The one you mention (
checkout
) is a great example. Another iscommit
, which is entirely local in git and entirely dependent on the server in CVS / SVN - the darcs terminology of "record" would have required less un-learning for people new to git. The other example I like is the message "needs update" that you see in git, which really means "needs to be committed" :)Of course, one could always use a different frontend to git, such as easy git, iolaus, etc. but most people are going to have to learn the standard commands eventually anyway, so you just have to get used to some of them being named rather surprisingly.
I'm sure there are historical reasons for the names of these various commands in git, but it would have been helpful if different words had been chosen...
Update: VonC links in the comments to an answer with a neat alias to make
git checkout
safer in either of its two usages ;)这是一个很好的名称,因为在签出分支时,您要求存储库为您提供(就像从图书馆“签出”书籍一样)该分支内最新修订状态的所有适当文件作为您的工作副本。
这里实际上并不存在 git checkout 具有“其他含义”的问题。该命令为您提供修订状态 X 的单个文件或一组文件(读作:“分支”)。无论您是否考虑“恢复”,都忽略了更重要的一点,即 git checkout 非常灵活并且有点通用。在这两种情况下,它都会从存储库中签出一定数量的状态并将其设置为工作副本,准备进行编辑。
It's a good name because when checking out a branch, you are asking the repository to give you (as if "checking out" books from a library) all of the appropriate files at their latest revision states within that branch as your working copy.
There isn't really an issue of
git checkout
having "other meanings" here. The command gives you an individual file or a set of files (read: "a branch") at revision state X. Whether you consider that "reverting" or not is missing the bigger point which is thatgit checkout
is flexible and a bit general. In both cases, it is checking out some amount of state from the repository and setting it as your working copy, ready to be edited.使用 Git 2.23(2019 年 8 月),您不必再使用令人困惑的
git checkout
命令。您可以使用
git switch
代替,就像您建议的那样,八年前。如果您需要更新工作树(不切换分支),新命令
git Restore
< /strong> 负责这个。如需了解更多信息,请访问“Git 2.23 亮点”来自泰勒·布劳。
With Git 2.23 (August 2019), you don't have to use the confusing
git checkout
command anymore.You can use
git switch
instead, just as you suggested, eight years ago.If you need to update the working tree (without switching branch), the new command
git restore
is in charge of that.See more at "Highlights from Git 2.23" from Taylor Blau.
因为该命令可用于执行两个操作,所以使用“checkout”关键字“有意义”。
这两个操作是:
当您想要区分提交 ID 和分支名称时,还可以使用“--”参数
Because the command can be used to do two actions it "makes" sense to use the "checkout" keyword.
The two actions are:
You can also use the '--' argument when you want to differenciate a commit ID from a branch name
checkout
指的是更新工作树中的文件。恢复还意味着将工作树中的文件更新为之前的提交。
因此,在我看来,使用 git checkout 来更新或恢复一个命令更为现实。
checkout
refers to updating the file in the working tree.Reverting also means updating the file in the working tree to its previous commit.
So in my sense it is more realistic to have one command to update or revert using
git checkout
.Git 开发得出了与您类似的结论 -
checkout
执行多项操作令人困惑。为了解决这个问题,在 2019 年的 Git 2.23.0 版本中,该命令被拆分为两个不同的实验命令:switch
— 切换到指定分支restore
— ;从另一个分支或源恢复文件提交的提交消息添加了
switch
命令概述了他们对此事的想法:请注意,(截至 2024 年 9 月)新命令仍被列为实验性命令 (
switch< /code>
,
恢复
):The Git development came to a similar conclusion as you — that
checkout
doing multiple things is confusing. To remedy this, the command was split into two different experimental commands in the 2.23.0 release of Git in 2019:switch
— Switch to a specified branchrestore
— Restore file(s) from another branch or sourceThe commit message for the commit that added the
switch
command outlines their thoughts on the matter:Note that (as of September 2024) the new commands are still listed as experimental (
switch
,restore
):