为什么切换分支的 git 命令名为“git checkout”?

发布于 2024-10-20 18:42:30 字数 171 浏览 2 评论 0原文

为什么切换分支的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

墨小沫ゞ 2024-10-27 18:42:30

我看到大多数其他答案都在解释 git checkout 的作用以及为什么“checkout”可能是描述它的合理方式。然而,虽然我非常喜欢 git,但这确实触及了当我试图帮助人们理解该系统时出现的两个严重的挫败点:

  1. git checkout 做了两件截然不同的事情,如果它们是单独的命令,这对新手会有帮助。

  2. 愤世嫉俗的人可能会认为 git 的术语是故意选择的,目的是为了迷惑来自 CVS 和 Subversion 的人!您提到的(checkout)就是一个很好的例子。另一个是 commit,它在 git 中完全是本地的,并且完全依赖于 CVS / SVN 中的服务器 - “记录”的 Darcs 术语对于刚接触 git 的人来说不需要太多的学习。我喜欢的另一个例子是您在 git 中看到的消息“需要更新”,这实际上意味着 “需要提交” :)

当然,人们总是可以使用不同的前端来 git,例如 简单的 gitiolaus等等,但无论如何,大多数人最终都必须学习标准命令,所以你只需要习惯其中一些命令的命名是相当令人惊讶的。

我确信 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:

  1. git checkout does two very distinct things, and it would be helpful for newcomers if they were separate commands.

  2. 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 is commit, 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 ;)

流年里的时光 2024-10-27 18:42:30

这是一个很好的名称,因为在签出分支时,您要求存储库为您提供(就像从图书馆“签出”书籍一样)该分支内最新修订状态的所有适当文件作为您的工作副本。

这里实际上并不存在 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 that git 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.

他夏了夏天 2024-10-27 18:42:30

我会将其命名为 git switch。

使用 Git 2.23(2019 年 8 月),您不必再使用令人困惑的 git checkout 命令。

您可以使用 git switch 代替,就像您建议的那样,八年前。

如果您需要更新工作树(不切换分支),新命令git Restore< /strong> 负责这个。

如需了解更多信息,请访问“Git 2.23 亮点”来自泰勒·布劳

I would name it git switch instead.

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.

在巴黎塔顶看东京樱花 2024-10-27 18:42:30

因为该命令可用于执行两个操作,所以使用“checkout”关键字“有意义”。

这两个操作是:

  • 将文件上的修改重置为先前的提交 ID
  • 切换到分支

当您想要区分提交 ID 和分支名称时,还可以使用“--”参数

Because the command can be used to do two actions it "makes" sense to use the "checkout" keyword.

The two actions are:

  • Resetting modifications on a file to a previous commit ID
  • Switching to a branch

You can also use the '--' argument when you want to differenciate a commit ID from a branch name

旧时浪漫 2024-10-27 18:42:30

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.

昔日梦未散 2024-10-27 18:42:30

Git 开发得出了与您类似的结论 - checkout 执行多项操作令人困惑。为了解决这个问题,在 2019 年的 Git 2.23.0 版本中,该命令被拆分为两个不同的实验命令:

  • switch — 切换到指定分支
  • restore —  ;从另一个分支或源恢复文件

提交的提交消息添加了 switch 命令概述了他们对此事的想法:

“git checkout”做太多事情是许多人感到困惑的根源
用户(有时甚至会咬老用户)。为了解决这个问题,
命令将分为两个新命令:switch 和 Restore。好的
旧的“git checkout”命令仍然在这里,直到所有(或大多数)
用户)已经厌倦了。

请注意,(截至 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 branch
  • restore — Restore file(s) from another branch or source

The commit message for the commit that added the switch command outlines their thoughts on the matter:

"git checkout" doing too many things is a source of confusion for many
users (and it even bites old timers sometimes). To remedy that, the
command will be split into two new ones: switch and restore. The good
old "git checkout" command is still here and will be until all (or most
of users) are sick of it.

Note that (as of September 2024) the new commands are still listed as experimental (switch, restore):

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文