Git Checkout: Switching Branches, Restoring Files, and Beyond
git checkout command is a fundamental Git operation used to switch between branches, restore files to a previous state, or inspect specific commits within a repository. While powerful, its dual functionality led to the introduction of more specific commands, git switch and git restore, in Git version 2.23 (released August 2019), which we recommend for clearer intent in modern workflows.What is Git Checkout Doing?
Git checkout fundamentally changes the state of your repository. It moves the HEAD pointer and updates your working directory to reflect the chosen branch, commit, or file revision. This ensures that the files you see and work with match the exact historical state you’ve selected, whether that’s the latest version of a development branch or a specific snapshot from weeks ago.
When you execute git checkout, Git performs several actions. It first checks for any uncommitted changes in your working directory that might be overwritten. If such changes exist and are not handled, Git will often prevent the operation to avoid data loss. Once safe, it then updates the files in your working directory and the index (staging area) to match the state of the target reference (branch, commit, or file). This dynamic capability makes git checkout a versatile tool for navigating your project’s history and managing different lines of development.

How Git Checkout Works: Understanding the HEAD Pointer
The core mechanism behind git checkout involves manipulating the HEAD pointer. This is Git’s symbolic reference to the currently active branch or commit. When you switch branches, git checkout updates HEAD to point to the tip of the new branch. It then populates your working directory with the files from that branch’s latest commit. For instance, if HEAD points to master and you run git checkout develop, HEAD will now point to develop. Your project files will change to match the develop branch’s content.
This process is crucial for isolating different lines of development. By changing what HEAD points to, Git allows you to smoothly switch contexts without manually managing different project folders. When you check out a specific commit, HEAD points directly to that commit hash. This leads to what is known as a “detached HEAD” state. In this state, you can inspect past versions. However, any new commits you make will not belong to a branch, making them easily lost unless you explicitly create a new branch from that detached HEAD.
Switching Between Branches with Git Checkout
One of the most common uses for git checkout is navigating between different branches in your repository. This allows you to work on separate features or bug fixes without interfering with other ongoing development.

Switching to an Existing Branch
To move from your current branch to another one that already exists, you use the command followed by the branch name. Before switching, we always recommend committing or stashing any uncommitted changes to prevent conflicts or data loss.
git checkout <existing-branch-name>
For example, if you are on the main branch and want to switch to a branch named feature-x, you would run:
git checkout feature-x
Upon successful execution, your terminal will typically confirm that you have switched to the new branch, and your working directory will update to reflect its content.
Creating and Switching to a New Branch
Often, you’ll want to create a new branch and immediately start working on it. The git checkout command provides a convenient shortcut for this, combining both operations into a single step.
git checkout -b <new-branch-name>
This command first creates a new branch named <new-branch-name> based on your current branch’s state. It then switches your HEAD to this newly created branch. For example, to create and switch to a branch for a new login feature:
git checkout -b login-feature
This is equivalent to running git branch login-feature followed by git checkout login-feature, saving you a step.
How to Git Checkout a Remote Branch
When collaborating, you often need to work on branches that exist only on a remote repository. To check out a remote branch, you typically create a local tracking branch that links to it. This allows you to pull and push changes easily.
If you want to create a local branch that tracks a remote branch with the same name, you can often just use:
git checkout <remote-branch-name>
Git is smart enough to detect that <remote-branch-name> exists on the remote (e.g., origin/<remote-branch-name>). It will automatically create a local tracking branch for you. For instance, if a branch named development exists on origin:
git checkout development
If you want to create a local branch with a different name that tracks a remote branch, you use the full remote reference:
git checkout -b <local-branch-name> <remote>/<remote-branch-name>
For example, to create a local branch named my-dev that tracks origin/development:
git checkout -b my-dev origin/development
Restoring Files with Git Checkout
Another powerful application of git checkout is its ability to discard uncommitted changes in your working directory or restore specific files to a previous state. This can be a lifesaver when you’ve made a mistake and need to revert a file.
Discarding Uncommitted Local Changes to a File
If you’ve made changes to a file but haven’t committed them, and you decide you want to revert that file to its last committed state, you can use git checkout with a double-dash (--).
git checkout -- <filename>
The double-dash is important here; it tells Git that what follows is a file path, not a branch name. For example, to discard changes to src/App.js:
git checkout -- src/App.js
This command effectively overwrites your local, uncommitted changes to src/App.js with the version from the current HEAD. Be cautious, as these changes are lost permanently.
Restoring a File from Another Branch or Commit
You can also use git checkout to grab a specific file from a different branch or even a past commit. It places the file into your current working directory. This is useful for cherry-picking a file without switching your entire branch.
git checkout <branch-or-commit> -- <filename>
For example, to get the README.md file from the develop branch:
git checkout develop -- README.md
Or, to restore config.yml to how it was in a specific commit:
git checkout a1b2c3d4 -- config.yml
After running this, the specified file will be updated in your working directory and staged for your next commit.
Inspecting Specific Commits and Tags
git checkout allows you to travel back in time to examine the state of your repository at any given commit or tag. This is invaluable for debugging, reviewing old code, or understanding how a feature evolved.
Switching to a Specific Commit
To view your project as it existed at a particular commit, you can check out its hash. This will put your repository into a “detached HEAD” state.
git checkout <commit-hash>
For instance, if you want to inspect a commit with the hash f7e8a9b0:
git checkout f7e8a9b0
In a detached HEAD state, your HEAD pointer points directly to a commit, not a branch. You can browse files and even make new commits, but these new commits won’t be part of any branch unless you explicitly create one from this point. We advise against making significant changes in a detached HEAD state without first creating a new branch.
How to Checkout from a Git Tag
A Git tag is essentially a fixed pointer to a specific commit, often used to mark release points (e.g., v1.0.0). Checking out a tag is similar to checking out a commit; it places you in a detached HEAD state at that tagged commit.
git checkout <tag-name>
For example, to view the code at your v1.0.0 release:
git checkout v1.0.0
This is useful for recreating past builds or examining the exact code that went into a particular release.
Git Checkout vs. Git Switch and Git Restore: The Modern Approach
As Git repositories grew in complexity, the dual nature of git checkout (for both switching branches and restoring files) sometimes led to confusion, especially for beginners. According to current facts, Git version 2.23 (released August 2019) addressed this by introducing two new commands:
git switch: Dedicated solely to switching branches.git restore: Dedicated solely to restoring files.
These commands provide a clearer intent and reduce the mental overhead when performing common Git operations. While git checkout still works for most of its original functions, we recommend adopting git switch and git restore for new projects and for anyone seeking a more explicit and less error-prone workflow.

When to Use Git Switch
Use git switch whenever your primary goal is to change the active branch. It handles both switching to existing branches and creating new ones with clearer syntax.
- To switch to an existing branch:
git switch <existing-branch-name> - Next, to create and switch to a new branch:
git switch -c <new-branch-name> - To create a new branch tracking a remote branch:
git switch --track <remote>/<remote-branch-name>
When to Use Git Restore
Use git restore when you need to revert changes to files, either in your working directory or the staging area. It provides more granular control over what you’re restoring.
- To discard uncommitted changes in your working directory:
git restore <filename> - Also, to unstage a file (move from staging area back to working directory):
git restore --staged <filename> - To restore a file from a specific commit or branch:
git restore --source=<commit-or-branch> <filename>
One critical advantage of git restore is its explicit handling of the working tree versus the index (staging area), which helps prevent accidental data loss, a risk sometimes associated with git checkout -- <filename> if not used carefully.
If Git Checkout Still Fails: Common Issues and Solutions
Even with careful use, you might encounter issues when running git checkout. Here are a few common scenarios and what to do:

Uncommitted Changes Blocking Checkout
Symptom: Git prevents you from checking out a branch, stating that you have uncommitted changes that would be overwritten. This is a safety mechanism to prevent data loss.
Fix: You have a few options:
- Commit your changes: If the changes are ready, commit them to your current branch.
git add . git commit -m "WIP: Save current changes" - Stash your changes: If the changes are not ready for a commit but you want to save them temporarily, use
git stash.git stash save "My temporary changes"You can then apply them later using
git stash pop. - Discard your changes (use with caution): If you are certain you don’t need the changes, you can discard them. For all changes:
git reset --hard HEADOr for specific files, as discussed earlier:
git checkout -- <filename>
Incorrect Branch or Commit Name
Symptom: Git reports an error like “pathspec ‘<branch-name>’ did not match any file(s) known to git” or “error: pathspec ‘<branch-name>’ did not match any file(s) known to git”.
Fix: Double-check the spelling of your branch name or commit hash. You can list all local branches with git branch and all remote branches with git branch -r to verify. For commits, ensure you have the correct hash.
git branch
git branch -r
Detached HEAD State Confusion
Symptom: After checking out a commit or tag, Git informs you that you are in a ‘detached HEAD’ state, and new commits won’t be part of any branch.
Fix: If you intended to just inspect the past state, you can simply check out a branch to return to normal development:
git checkout <your-branch-name>
If you intended to make changes from that past commit and save them, immediately create a new branch from your detached HEAD:
git switch -c <new-branch-name-from-commit>
This creates a new branch pointing to your current detached HEAD commit and switches you to it, allowing you to commit safely.
How to Confirm It Is Fixed
After performing a git checkout operation, you can confirm the repository’s state using a few simple commands. These checks ensure that you are on the expected branch or commit and that your working directory reflects the correct files.
Verify Current Branch or Commit
To see which branch you are currently on, or if you are in a detached HEAD state, use git status. This command provides a comprehensive overview of your repository’s current state.
git status
Expected Output:
- If on a branch:
On branch <branch-name> - If in detached HEAD:
HEAD detached at <commit-hash>
Alternatively, git branch (without any arguments) will list all local branches and highlight your current branch with an asterisk (*).
git branch
Inspect Working Directory Changes
After restoring files, you can verify that the changes have been applied correctly by inspecting the file itself or running git status again to ensure the file is no longer modified or is staged as expected.
git status
Expected Output: The file you restored should no longer appear under “Changes not staged for commit” if you discarded local modifications, or it might appear under “Changes to be committed” if you restored it from another source.
Sources: Git – git
Frequently asked questions
When should I use git checkout versus git switch or git restore?
We recommend using git switch for all branch-related operations (creating, switching, tracking branches) and git restore for all file-related operations (discarding changes, restoring files from history). Use git checkout primarily when you need to inspect a specific commit or tag. This puts you in a detached HEAD state.
What happens if I use git checkout without specifying a branch or file?
Running git checkout without specifying a branch or file typically has no effect other than printing tracking information for your current branch, if applicable. It will not switch branches or restore files unless you provide further arguments.
Can I use git checkout to undo a commit?
No, git checkout alone does not undo a commit in the sense of removing it from history. While you can check out a commit prior to the one you want to “undo” (putting you in a detached HEAD state), this only changes your view of the repository. To genuinely undo commits, you would typically use commands like git revert (to create a new commit that reverses previous changes) or git reset (to move the branch pointer back, potentially rewriting history).