Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 730
Michael Avatar asked Mar 09 '11 11:03

Michael


People also ask

Does git checkout switch to a branch?

The "checkout" command can switch the currently active branch - but it can also be used to restore files. The most common use case for "checkout" is when you want to switch to a different branch, making it the new HEAD branch.

Why is it called Checkout git?

NAME git-checkout - Checkout a branch or paths to the working tree [...] DESCRIPTION Updates files in the working tree to match the version in the index or the specified tree. If no paths are given, git checkout will also update HEAD to set the specified branch as the current branch.

What is the git command to switch branches?

A quick way of switching branch on Git is to use the “git switch” command and specify the name of the branch you want to switch to. If the destination branch does not exist, you have to specify the “-c” option (for “create branch“), otherwise you will get an error message when switching to that branch.

What is git checkout branch name?

To checkout a branch we use the git checkout [name] command. In the above example we are switching to the dev branch. Now, if we commit changes using the git add and git commit commands then it will be saved in the dev branch as we are currently on it.


2 Answers

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

like image 80
Mark Longair Avatar answered Oct 17 '22 13:10

Mark Longair


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.

like image 33
David Avatar answered Oct 17 '22 13:10

David