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
)
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.
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.
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.
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.
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:
git checkout
does two very distinct things, and it would be helpful for newcomers if they were separate commands.
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 ;)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With