Frequently asked questions around Git and Version Control.
How to Checkout a Commit in Git
In Git, "checking out" means making a certain state your current working state: the actual files in your Working Copy will be the ones from that exact point in time.
In this short article, we'll discuss how you can checkout branches and specific revisions in Git.
The Git Cheat Sheet
No need to remember all those commands and parameters: get our popular "Git Cheat Sheet" - for free!
Checking Out Branches
Most of the time, you will want to checkout a branch (and not a specific revision). Branches are very practical because they are pointers to the latest commit in a certain context (it helps to think of branches simply as specific, separate contexts with names).
This means that, actually, branches don't point to a certain commit but really always to the latest commit on the corresponding branch. This also means that, if a new commit is made in that context, the branch pointer is automatically moved to that newest commit. The user does not have to do this manually.
This makes branches a very safe and convenient tool in Git. As said, most of the time you'll want to "checkout" branches, and not individual commits. Here's how to do this:
$ git switch my-branch
git switch command (or, alternatively, the
git checkout command), you can simply provide the name of the branch you want to checkout.
This branch will then be your current working branch, also referred to as "HEAD" in Git. Any new commits you make from this point on (until you switch branches again) will be recorded in this branch's context.
In case you are using the Tower Git client, you can double-click the branch you want or (in case you have lots and lots of branches) simply use the "Quick Action" dialog to enter the branch's name, not using the mouse at all:
Checking Out Commits
There are very few reasons to checkout a commit (and not a branch). Maybe you want to experiment with a specific, old revision and therefore need to have that revision's files in your working copy folder.
To checkout a specific commit, you can use the
git checkout command and provide the revision hash as a parameter:
$ git checkout 757c47d4
You will then have that revision's files in your working copy. However, you are now also in a state called "Detached HEAD".
In case you are using the Tower Git client, you can simply right-click any commit and choose "Check Out <commit-hash>" from the contextual menu:
The Detached HEAD State
The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when checking out a branch, Git automatically moves the HEAD pointer along when you create new commits: you're automatically and always on the newest commit of the chosen branch.
When you instead choose to check out a specific commit hash, Git will NOT do this for you. This means that when you make changes and commit them, these changes do NOT belong to any branch.
The consequence is that these changes can easily get lost once you check out a different revision or branch: not being recorded in the context of a branch, you lack the possibility to access that state easily.
To make a long story short: be very careful when checking out a specific commit instead of a branch (and make sure this is really what you want and need).
- More frequently asked questions about Git & version control