AlgoMaster Logo

git checkout branch

Last Updated: January 3, 2026

5 min read

While the git switch command has become the go-to option for changing branches, the git checkout command is still a powerful tool in your Git arsenal.

Understanding how to use git checkout effectively can enhance your workflow and provide greater flexibility when managing branches.

Understanding git checkout

The git checkout command serves dual purposes: it allows you to switch branches and to restore files within your working directory. For this chapter, we will focus on its role in branch management.

When you execute git checkout <branch-name>, Git performs several actions under the hood. It updates the HEAD pointer to point to the specified branch. This means that the working directory and the staging area are changed to match the contents of that branch.

The Mechanics of Switching Branches

Here's a simplified breakdown of what happens when you execute git checkout:

  1. Update the HEAD Pointer: HEAD is a special pointer that indicates your current branch. When you switch branches, HEAD updates to the new branch.
  2. Update the Working Directory: Git replaces the files in your working directory with the versions that exist in the new branch, ensuring that your local environment reflects the state of that branch.
  3. Stage Changes: If you have uncommitted changes in your working directory, Git will not allow you to switch branches unless those changes are either committed or stashed.

This under-the-hood process is what allows you to work on multiple features or fixes seamlessly.

git checkout Syntax

Using git checkout to switch branches is straightforward, but understanding the syntax can help you avoid common pitfalls.

You can also use additional flags with git checkout. Here are a few useful ones:

  • -b: Create a new branch and switch to it.
  • --track: Create a new branch and set it to track a remote branch.
  • --: This allows you to specify a file to restore from another branch without switching branches.

Real-World Example

Imagine you are working on a project and need to switch from the develop branch to a new feature branch called feature/login. You would execute:

If you need to create and switch to a new branch at the same time, you'd do:

This creates the feature/login branch based on your current branch and switches you to it immediately.

Handling Uncommitted Changes

When you try to switch branches with uncommitted changes, Git will throw an error to prevent potential data loss. Here are some strategies for handling this situation:

1. Stash Your Changes

If you are not ready to commit your changes but want to switch branches, you can stash them:

This saves your changes temporarily. You can apply them later with:

2. Commit Your Changes

If the changes are complete, you can commit them:

3. Create a New Branch for Your Changes

If you're in the middle of working on something and want to keep those changes separate, you can create a new branch:

This way, you can switch context without losing your work.

Switching to a Remote Branch

When working in a team, you may need to switch to branches that exist only on remote repositories. Here’s how you can do that with git checkout.

If you know a branch exists on the remote but not locally, you can fetch all branches first:

Then, use:

This creates a local branch that tracks the remote branch. For example:

This command creates a new local branch feature/login that tracks the remote branch origin/feature/login.

Advanced Usage of git checkout

While git checkout is often used for switching branches, it also has advanced capabilities that can be beneficial in certain situations.

Checkout Specific Files

You can use git checkout to restore specific files from a branch without switching branches. This can be helpful when you only need to update a few files from another branch.

For example, if you want to get an updated version of app.js from the develop branch while on feature/login, you could run:

This command updates app.js to the state it was in on the develop branch, allowing you to incorporate the latest changes without switching branches.

Undoing Changes

Sometimes, you may want to undo changes in your working directory. If you have modified a file but wish to discard those changes, you can use:

This resets the specified file back to the last committed state.

As you continue your Git journey, the next important concept to master is deleting branches. In the upcoming chapter, we will explore the processes for safely removing branches, both local and remote, and discuss best practices to ensure you maintain a clean and efficient Git repository.