22

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)

Zoe
  • 27,060
  • 21
  • 118
  • 148
Michael
  • 10,185
  • 12
  • 59
  • 110
  • 5
    because Linus decided it has to be that way? – eckes Mar 09 '11 at 11:04
  • 4
    `git switch` is now a reality! (in Git 2.23, August 2019). See [my answer below](https://stackoverflow.com/a/57578455/6309) – VonC Aug 20 '19 at 17:01

5 Answers5

15

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

Community
  • 1
  • 1
Mark Longair
  • 446,582
  • 72
  • 411
  • 327
  • +1000: 100% agreed. More on the checkout dangers: http://stackoverflow.com/questions/2961240/get-back-the-changes-after-accidental-checkout/2961348#2961348 – VonC Mar 09 '11 at 11:52
  • @VonC: On this topic, I thought you might be interested in a new blog post of mine on [confusing git terminology](http://longair.net/blog/2012/05/07/the-most-confusing-git-terminology/) which adds a few more examples. – Mark Longair May 07 '12 at 10:08
  • Thank you. I will check it out later: your site is categorized as "Social Networking" and blocked from work. – VonC May 07 '12 at 10:24
  • Seven years later, no more alias needed: no more checkout: [`git switch` and `git restore`](https://stackoverflow.com/a/57123118/6309) to the rescue! (Git 2.23, Q3 2019) – VonC Jul 21 '19 at 01:55
  • And I was think all this time that I'm too dumb to grasp the concepts... the old terminology was like overload a + operator with a - operator... And guys, when in a Windows Gui Bash, do a `git --version` and compare with the [current one](https://git-for-windows.github.io/) – Marcelo Scofano Diniz Jul 28 '20 at 13:11
7

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.

David
  • 316
  • 1
  • 4
  • 1
    My issue with the term "checkout" is that it implies you are obliged to check in later. Like the library metaphor--you borrow a book and then bring it back later. Or other source control software, where checking out has the same meaning--having temporary (maybe exclusive) access to a file that should be relinquished when you later check out. – Erik Hermansen Jul 22 '14 at 18:29
5

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Good one, just wondering why people couldn't think of using it earlier. – surajs1n Aug 20 '19 at 17:20
  • Great point, thanks! More on the "differences" between `git checkout` vs `git switch`, [here](https://stackoverflow.com/a/57266005/1175496) – Nate Anderson Dec 10 '22 at 19:45
  • 1
    @TheRedPea Not quite: More on the difference between `git checkout` and `git switch` [here](https://stackoverflow.com/a/57066202/6309), and in the second part of [here](https://stackoverflow.com/a/3965714/6309). – VonC Dec 10 '22 at 19:49
1

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

Artusamak
  • 2,470
  • 19
  • 19
1

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.

mmrs151
  • 3,924
  • 2
  • 34
  • 38