Michael
Michael

Reputation: 10303

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

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)

Upvotes: 23

Views: 21502

Answers (6)

M. Justin
M. Justin

Reputation: 21315

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.

Upvotes: -1

VonC
VonC

Reputation: 1328152

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.

Upvotes: 7

Mark Longair
Mark Longair

Reputation: 467921

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

Upvotes: 16

David
David

Reputation: 326

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.

Upvotes: 8

mmrs151
mmrs151

Reputation: 4092

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.

Upvotes: 2

Artusamak
Artusamak

Reputation: 2510

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

Upvotes: 2

Related Questions