Why is the git command to switch branches named “g

2020-07-01 07:13发布

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)

5条回答
趁早两清
2楼-- · 2020-07-01 07:22

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 ;)

查看更多
Emotional °昔
3楼-- · 2020-07-01 07:23

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.

查看更多
欢心
4楼-- · 2020-07-01 07:26

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

查看更多
Lonely孤独者°
5楼-- · 2020-07-01 07:38

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.

查看更多
我命由我不由天
6楼-- · 2020-07-01 07:38

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.

查看更多
登录 后发表回答