git checkout

Look at a historical version of your repository using git checkout

Using git checkout along with a specific branch or revision will allow you to change the working copy of your repository to a historical version of itself.

Changing branches

To switch to a different branch, simply type:

$ git checkout my-other-branch
Switched to branch 'my-other-branch'

This will automatically change your working copy to the latest commit (or HEAD) of the new branch. If you wish to create a new branch and switch to that, type the following:

$ git checkout -b my-new-branch
Switched to a new branch 'my-new-branch'

You'll be switched to the new branch and all previous work from the branch you were in before will be carried over to it, including any uncommitted or unstaged files.

Changing revisions

To change to a different version of your code:

$  git checkout 956951bfc15e077bf6dac35ee552117f5a7e8a43
Note: checking out '956951bfc15e077bf6dac35ee552117f5a7e8a43'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 956951b Revert "Remove Cheddar"

You'll notice a large amount of information accompanies the output of this command. This explains that you're now viewing a "detatched" version of the repository, which means you can look at, and make changes to it without affecting your live (or anyone else's) copy of the repository.

To make changes and have them actually take effect, you'll need to run git checkout -b <new-branch-name> to create a new branch, then commit those changes to the new branch.

Proudly powered by Katapult. Running on 100% renewable energy.