Last Updated: January 3, 2026
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.
git checkoutThe 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.
Here's a simplified breakdown of what happens when you execute git checkout:
HEAD Pointer: HEAD is a special pointer that indicates your current branch. When you switch branches, HEAD updates to the new branch.This under-the-hood process is what allows you to work on multiple features or fixes seamlessly.
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.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.
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:
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:
If the changes are complete, you can commit them:
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.
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.
git checkoutWhile git checkout is often used for switching branches, it also has advanced capabilities that can be beneficial in certain situations.
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.
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.
Be cautious when using this command, as it will permanently discard any changes that haven't been committed.
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.