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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5244786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:18:33  来源:igfitidea点击:

Why is the git command to switch branches named "git checkout"?

gitgit-checkout

提问by Michael

Why is the git command to switch branches named git checkout?
Does it really make sense ?

为什么切换分支的 git 命令命名为git checkout
真的有意义吗?

I would name it git switchinstead. Besides, git checkouthas other meanings: e.g. reverting a file (like svn revert)

我会git switch改为命名它。此外,git checkout还有其他含义:例如恢复文件(如svn revert

回答by Mark Longair

I see that most other answers are explaining what git checkoutdoes 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以及为什么“结帐”可能是一种合理的描述方式。然而,虽然我非常喜欢 git,但这确实触及了当我试图帮助人们理解系统时出现的两个严重挫折点:

  1. git checkoutdoes 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":)

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

  2. 愤世嫉俗者可能会认为 git 的术语是故意选择来混淆来自 CVS 和 Subversion 的人的!你提到的 ( checkout) 就是一个很好的例子。另一个是commit,它在 git 中完全是本地的,并且完全依赖于 CVS / SVN 中的服务器 - “记录”的 darcs 术语对于 git 新手来说需要更少的学习。我喜欢的另一个例子是你在 git 中看到的消息“需要更新”,这真的意味着“需要提交”:)

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.

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

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

我确信 git 中这些不同命令的名称有历史原因,但如果选择不同的词会有所帮助......



Update:VonClinks in the comments to an answer with a neat alias to make git checkoutsaferin either of its two usages ;)

更新:VonC将评论中git checkout链接链接到一个带有简洁别名的答案,以使其两种用法中的任何一种更安全;)

回答by David

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 checkouthaving "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 checkoutis flexible and a bit general. In both cases, it is checking outsome amount of state from the repository and setting it as your working copy, ready to be edited.

这里没有真正的git checkout“其他含义”的问题。该命令为您提供修订状态为 X的单个文件或一组文件(读作:“一个分支”)。无论您是否考虑“恢复”,都忽略了更大的一点,即git checkout灵活且有点笼统。在这两种情况下,它都会从存储库中检出一些状态并将其设置为您的工作副本,准备好进行编辑。

回答by mmrs151

checkoutrefers 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.

checkout指更新工作树中的文件。恢复也意味着将工作树中的文件更新到它之前的提交。因此,在我看来,使用一个命令来更新或还原使用git checkout.

回答by Artusamak

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

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

The two actions are:

这两个动作是:

  • Resetting modifications on a file to a previous commit ID
  • Switching to a branch
  • 将文件修改重置为之前的提交 ID
  • 切换到分支

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

当您想将提交 ID 与分支名称区分开来时,您还可以使用“--”参数

回答by VonC

I would name it git switch instead.

我会把它命名为 git switch 。

With Git 2.23 (August 2019), you don't have to use the confusing git checkoutcommand anymore.

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

You can use git switchinstead, just as you suggested, eight years ago.

你可以改用git switch八年前的建议,就像你建议的那样。

If you need to update the working tree (without switching branch), the new command git restoreis in charge of that.

如果您需要更新工作树(不切换分支),则由新命令git restore负责。

See more at "Highlights from Git 2.23" from Taylor Blau.

Taylor Blau 的Git 2.23 亮点”中查看更多信息。