Git checkout explained in detail [Practical Examples]


GIT

Author: Steve Alila
Reviewer: Deepak Prasad

The git checkout command is a fundamental part of Git's workflow, allowing users to navigate between the different states and versions of their project. It serves several key purposes:

  • Switching Branches: One of the primary uses of git checkout is to switch between branches. By using git checkout <branch-name>, you can switch to any branch that exists in your repository.
  • Restoring Files: git checkout can also be used to restore files to a previous state. If you've made changes to a file that you want to discard, you can use git checkout -- <file> to revert the file to the last committed state.
  • Navigating Commits: Another important function of git checkout is to navigate between different commits in the repository. By using git checkout <commit-hash>, you can switch your working directory to reflect the state of the repository at a specific commit.

 

git checkout Basic Syntax and Usage

Let's explore the syntax for different scenarios:

 

Switching Branches

To switch to an existing branch, use the following command

git checkout <branch-name>

For example, if you want to switch to a branch named feature-branch, you would use:

git checkout feature-branch

 

Checking Out Specific Commits

To check out a specific commit, use the commit hash (a unique identifier for each commit):

git checkout <commit-hash>

When you do this, the git checkout command will carry out three actions:

  1. It changes the HEAD pointer to point to the commit you are switching onto.
  2. It populates the staging area with all the files and directories that are part of the commit you are switching onto.
  3. It copies the contents of the staging area into the working directory.

For example, if you want to view the state of the project at commit a1b2c3d4, you would use:

git checkout a1b2c3d4

This command puts you in a "detached HEAD" state, meaning you are not on any branch. Any changes made here won't belong to a branch unless you create a new branch from this state.

 

Restoring Files

To discard changes in a specific file and restore it to the last committed state, use:

git checkout -- <file>

For example, to restore changes in index.html, you would use:

git checkout -- index.html

 

git checkout command examples

1. Undo a specific commit

Let us print log history and check out the second commit from the local HEAD.

git log --pretty=oneline
git checkout 0cff5e7c8263a11b405ed8d3735d6257fdeeb055

Git tells us we are in a detached HEAD. That means the ref in the .git/HEAD file does not match a branch, as git expects it to.

Git sees ref: refs/heads/<commit> instead of the expected format ref: refs/heads/<branch>. So, git does not know what to do with changes you do at the misplaced HEAD.

The best time to check out a commit is when restoring commits after a hard reset. Let us return the HEAD to the main branch.

git checkout main

Reset the last commit.

git reset --hard HEAD^

And restore it from the reflog using the checkout command and a new branch.

git reflog
git checkout 0cff5e7
git switch -c new_branch

Return to the main branch and rebase the new_branch.

git checkout main
git rebase new_branch

Rechecking the history

git log --pretty=oneline

confirms the restored changes.

Git checkout explained in detail [Practical Examples]

 

2. Checkout and switch to an existing branch

First, list all branches in your repository to see which ones are available:

git branch

This command will show something like:

* main
feature-branch
bugfix-branch

The * indicates the current branch.

To switch to an existing branch, use:

git checkout <branch-name>

For example, to switch to feature-branch:

git checkout feature-branch

 

3. Creating and Switching to New Branch

Creating a new branch and switching to it immediately can be done with a single command:

Use the -b flag to create and switch to a new branch:

git checkout -b <new-branch-name>

For example, to create and switch to a branch named new-feature

git checkout -b new-feature

This will create a new branch new-feature and immediately switch to the same branch. You can verify using git branch command.

 

4. Checkout and switch into a remote branch

Fetch all remote branches:

git fetch

List all branches, including remote branches:

git branch -a

You might see something like:

* main
remotes/origin/main
remotes/origin/feature-branch

To check out a remote branch, create a local tracking branch:

git checkout -b <local-branch> <remote-branch>

For example, to check out the remote branch origin/feature-branch:

git checkout -b feature-branch origin/feature-branch

Sample Output:

Branch 'feature-branch' set up to track remote branch 'feature-branch' from 'origin'.
Switched to a new branch 'feature-branch'

 

5. Restoring Files

You can use git checkout to discard changes in your working directory and restore files to their last committed state:

To restore a specific file:

git checkout -- <file>

For example, to restore index.html

git checkout -- index.html

Example:

$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: index.html

$ git checkout -- index.html
$ git status
On branch main
nothing to commit, working tree clean

 

6. Detached HEAD State

A detached HEAD state occurs when you check out a specific commit instead of a branch:

Use the commit hash to check out a specific commit:

git checkout <commit-hash>

For example:

git checkout a1b2c3d4

In a detached HEAD state, HEAD points directly to a commit, not a branch. This means any new commits won't be associated with any branch.

Example:

$ git log --oneline
a1b2c3d4 Commit message
e5f6g7h8 Another commit
i9j0k1l2 Yet another commit

$ git checkout a1b2c3d4
Note: checking out 'a1b2c3d4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them. 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 a1b2c3d4 Commit message

$ git status
HEAD detached at a1b2c3d4
nothing to commit, working tree clean

 

Conclusion

Git checkout is a multi-purpose command. It allows you to undo changes and switch the HEAD between branches. Using it effectively requires understanding the origin of the term HEAD and distinguishing the command from related commands, as explained in this tutorial.

 

Steve Alila

Steve Alila

He specializes in web design, WordPress development, and data analysis, with proficiency in Python, JavaScript, and data extraction tools. Additionally, he excels in web API development, AI integration, and data presentation using Matplotlib and Plotly. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment