Git Checkout Branch (original) (raw)

Last Updated : 26 Feb, 2026

git checkout is a versatile Git command used for branch management, navigating commits, and managing changes in the working directory.

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

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.

git checkout -b < new-branch-name >

Screenshot-2024-08-10-150853

Switching Between Local Branches

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.

git fetch origin

Screenshot-2024-08-10-152050

Checking Out to Remote Branch

git branch -r

Screenshot-2024-08-10-152151

Checking Out to Remote Branch

git checkout branch-name

Screenshot-2024-08-10-152329

Checking Out to Remote Branch

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

Returning to a Branch

git checkout

Screenshot-2024-08-14-231626

Navigating to a Specific Commit

Advanced Uses of Git Checkout Branch

Advanced uses of git checkout enable switching branches, creating branches, navigating commits, and restoring files for precise workflow control.

Checking Out Files from a Different Branch

Checkout a File from Another Branch:

git checkout --

Staging the Change

git add

Committing the Change

git commit -m "Replaced with version from "

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

Screenshot-2024-08-14-232214

Switch Branches with git checkout

**Dropping the Stash

git stash drop

Screenshot-2024-08-14-232313

Dropping the Stash