The Wayback Machine - https://web.archive.org/web/20250212082442/https://www.geeksforgeeks.org/git-checkout-branch/
Open In App

Git Checkout Branch

Last Updated : 22 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The git checkout command is one of the most versatile commands in Git. It allows you to switch between branches, create new branches, and even revert changes in your working directory. While its primary use is branch management, git checkout can also be used to navigate to specific commits and manipulate individual files across branches.

There are several ways the git checkout command can be used to manipulate branches:

1. Switching to an Existing Branch

Switching to an existing branch is one of the most common tasks in Git, it will let you work in different areas of your project without creating a mess for other branches.

Before switching, make sure you know what your current branch is by using the following command.

git status
  • By using this you will see if there are uncommitted changes or if your working directory is clean.
Screenshot-2024-08-10-145633
Switching to an Existing Branch

2. Creating and Checking Out a New Branch

Use the git checkout command followed by the branch name to switch to the desired branch.

git checkout -b branch-name
Screenshot-2024-08-10-150243
Creating and Checking Out a New Branch

Verify Switch: Confirm you have switched to the correct branch by using following command

git branch
Screenshot-2024-08-10-150736
Creating and Checking Out a New Branch

3. Switching Between Local Branches

This method combines the creation of a branch with switching to it in a single operation, it is useful in order to begin work on a new feature or bug fix right away without first needing to stop the world.

  • Create and Switch: Use the -b option with git checkout to create and switch to a new branch in one command.
git checkout -b < new-branch-name >
Screenshot-2024-08-10-150853
Switching Between Local Branches
  • Confirm Creation: Verify that the branch has been created and you have switched to it using:
git branch
Screenshot-2024-08-10-150923
Switching Between Local Branches

Here we see * indication. That indicates present we are in that branch

4. Checking Out to Remote Branch

When a branch exists only on the remote repository and not locally, you can use git checkout to create a local branch that tracks the remote branch.

  • Fetch Remote Changes: Ensure your local repository is up-to-date with the remote repository.
git fetch origin
Screenshot-2024-08-10-152050
Checking Out to Remote Branch
  • Check Remote Branches: List all remote branches to verify the branch you want to check out by using following command.
git branch -r
Screenshot-2024-08-10-152151
Checking Out to Remote Branch
  • Checkout Remote Branch: Use git checkout to create and switch to a local branch that tracks the remote branch use below command to track remote branch.
git checkout branch-name
Screenshot-2024-08-10-152329
Checking Out to Remote Branch
  • Verify Tracking: Ensure the local branch is tracking the remote branch by using:
git branch -vv
Screenshot-2024-08-10-152429
Checking Out to Remote Branch

5. Navigating to a Specific Commit

Using git checkout to Explore a Specific Commit

Find the Commit Hash

git log
Screenshot-2024-08-14-231328
Navigating to a Specific Commit

Understanding Detached HEAD State and How to Return to a Branch

  • In this state, you can view and make changes, but those changes are not associated with any branch.

Returning to a Branch

git checkout <branch-name>
Screenshot-2024-08-14-231626
Navigating to a Specific Commit

Advanced Uses of Git Checkout Branch

Checking Out Files from a Different Branch

Checkout a File from Another Branch:

git checkout <branch-name> -- <file-path>

Staging the Change

git add <file-path>

Committing the Change

git commit -m "Replaced <file-path> with version from <branch-name>"

Integrating git stash with git checkout for Safer Branch Switching

Stash Uncommitted Changes

git stash
Screenshot-2024-08-14-224201
Stash Uncommitted Changes

Switch Branches with git checkout

git checkout <branch-name>
Screenshot-2024-08-14-232214
Switch Branches with git checkout

Dropping the Stash

  • Once you’re sure the changes are no longer needed, drop the stash:
git stash drop
Screenshot-2024-08-14-232313
Dropping the Stash

Git Checkout Branch - FAQs

What is the purpose of the git checkout command?

Git checkout is a command used to change branches and restore files to a previous state, it updates your working directory to reflect the state of the branch or commit you switch to.

How do I switch to an existing branch using git checkout?

To switch to an already existing branch, run git checkout branch-name. This will update your working directory with the state of the specified branch.

Can I create and switch to a new branch with git checkout?

Sure, you can do a branch create and switch in one step with git checkout -b new-branch-name, which creates a new branch and immediately moves into it.

How do I switch to a branch that exists on the remote repository but not locally?

First, pull down the most recent changes from the remote repository by doing git fetch origin. Then switch over to the remote branch by running git checkout branch-name. This would create a local tracking branch for the remote branch.

What happens to my uncommitted changes when I switch branches?

If there are some changes to the code that have not been committed, Git does not allow you to switch branches as the changes may be lost. You would have to commit your changes, stash them using git stash, or discard them before changing.


Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg