git checkout
Switch branches or restore working tree files
git checkout is a versatile command that switches between branches, creates new branches, restores files from specific commits, and can put your repository in a 'detached HEAD' state. While newer Git versions split some functionality into 'git switch' and 'git restore', checkout remains widely used and understood.
Synopsis
git checkout <branch>
git checkout -b <new-branch> [<start-point>]
git checkout -B <branch> [<start-point>]
git checkout --detach [<branch>]
git checkout [--] <pathspec>...
git checkout <commit> -- <pathspec>...Options
Choosing the Right Option
Creating Branches
Different ways to create and switch to a new branch
| Command | If Branch Exists | History | Best For |
|---|---|---|---|
-b <name> | Fails with error | Normal (connected) | Standard new feature work |
-B <name> | Resets to current commit | Normal (connected) | Restart a branch from scratch |
--orphan <name> | Fails with error | None (disconnected) | gh-pages, docs, clean slate |
Switching with Uncommitted Changes
What happens to your local modifications when switching branches
| Command | Uncommitted Changes | Risk Level | When to Use |
|---|---|---|---|
git checkout <branch> | Blocked if conflict | Safe | Default behavior - prevents accidents |
git checkout -f <branch> | Discarded permanently | Dangerous | Abandon all local changes intentionally |
git checkout -m <branch> | Merged via 3-way merge | Medium | Carry changes to different branch |
git stash + checkout | Saved for later | Safe | Temporary context switch |
Restoring Files
Discard or restore file contents from different sources
| Command | Source | Affects Staging | Recoverable |
|---|---|---|---|
-- <file> | Index (staged version) | No | No - changes lost forever |
<commit> -- <file> | Specific commit | Yes (staged) | No - but staged for review |
-p -- <file> | Index (interactive) | No | Partial - choose which hunks |
Remote Branch Tracking
Control the relationship between local and remote branches
| Command | Creates Tracking | Use Case | git push/pull |
|---|---|---|---|
--track origin/feat | Yes (automatic) | Work on teammate's branch | Works without arguments |
--no-track -b exp origin/main | No (independent) | Safe experimentation | Requires explicit remote |
<remote-branch> | Yes (if unambiguous) | Quick checkout of remote | Works without arguments |
Resolving Merge Conflicts
Choose a side during merge conflicts
| Command | Keeps | Discards | When to Use |
|---|---|---|---|
--ours <file> | Your branch's version | Incoming changes | Your changes are correct |
--theirs <file> | Incoming version | Your changes | Accept their implementation |
(manual edit) | Combined result | Nothing | Need parts from both sides |
checkout vs Modern Commands
Git 2.23+ split checkout into focused commands
| Command | Switches Branches | Restores Files | Recommended For |
|---|---|---|---|
git checkout | Yes | Yes | Familiarity, older Git versions |
git switch | Yes | No | Clear intent: branch operations only |
git restore | No | Yes | Clear intent: file operations only |
Workflow Examples
git stashSave current uncommitted work
git checkout other-branchSwitch to the other branch
(review or make quick fix)Do the necessary work on this branch
git checkout -Switch back to previous branch (shortcut)
git stash popRestore your saved work
git checkout mainSwitch to main branch
git pull origin mainGet latest changes from remote
git checkout -b feature-nameCreate and switch to new feature branch
(make changes and commits)Work on the feature
git push -u origin feature-namePush branch and set up tracking
git statusSee which files have been modified
git diff file.jsReview the changes you're about to discard
git checkout -- file.jsDiscard changes to specific file
git checkout -- .Or discard ALL unstaged changes in tracked files
git log --onelineFind the commit hash you need
git checkout <commit-hash>Check out that specific commit (detached HEAD)
(inspect, test, or debug)Investigate the old code
git checkout mainReturn to main branch when done
git log --oneline -- file.jsFind commits that changed this file
git show <commit>:file.jsPreview the old version
git checkout <commit> -- file.jsRestore file from that commit (stages it)
git commit -m 'Restore file.js'Commit the restored version
git fetch originGet latest remote branch info
git branch -rSee available remote branches
git checkout --track origin/featureCreate local branch tracking the remote
git pullNow pull works without specifying remote
Troubleshooting
error: Your local changes to the following files would be overwritten by checkout
Cause: You have uncommitted changes that conflict with the target branch
Either commit your changes, stash them, or use -f to force (discards changes)
git stash && git checkout branch && git stash poperror: pathspec 'branch-name' did not match any file(s) known to git
Cause: The branch doesn't exist, or you may have typo'd the name
Check available branches with git branch -a, or create the branch with -b
git branch -a | grep branch-nameYou are in 'detached HEAD' state
Cause: You checked out a commit hash or tag instead of a branch name
If you want to keep working, create a branch. Otherwise checkout an existing branch
git checkout -b new-branch-nameCommits made in detached HEAD are lost
Cause: You made commits while in detached HEAD then switched to another branch
Find the commits with reflog and create a branch to save them
git reflog && git branch recovered-work <commit-hash>Cannot checkout: untracked file would be overwritten
Cause: You have untracked files that exist in the target branch
Either delete, move, or stash the untracked files first
git stash --include-untracked && git checkout branchAccidentally discarded changes with checkout --
Cause: git checkout -- <file> permanently removes uncommitted changes
Unfortunately, Git cannot recover these. Check your editor's local history
(check IDE local history if available)fatal: 'name' could be both a local file and a tracking branch
Cause: A file or folder has the same name as a remote branch you want to check out
Add a trailing -- to tell Git the name is a branch, or use git switch
git checkout branch-name --Branch not updating after checkout
Cause: You may have checked out a remote-tracking branch directly
Create a local tracking branch instead of checking out origin/branch directly
git checkout --track origin/branch-nameTips & Pitfalls
Best Practices
- ✓Use 'git stash' before switching branches if you have uncommitted work you want to keep
- ✓Prefer 'git checkout -b' over separate 'git branch' + 'git checkout' for creating and switching
- ✓Use '--' to clearly separate file paths from branch names: 'git checkout -- file.txt'
- ✓Consider using modern 'git switch' for branches and 'git restore' for files - clearer intent
- ✓Use 'git checkout -' to quickly switch back to the previous branch (like 'cd -')
- ✓Before using '-f' to force checkout, make sure you really want to discard your changes
- ✓When in detached HEAD, create a branch immediately if you plan to make commits
- ✓Use '--track' when checking out remote branches to set up push/pull automatically
- ✓Use 'git checkout <commit> -- <file>' to restore specific files without switching branches
- ✓Avoid checking out commits directly unless investigating - use branch names for regular work
- ✓Use '-p' (patch mode) when you want to selectively discard only some changes in a file
- ✓Always verify you're on the right branch with 'git branch' or 'git status' before starting work
Common Mistakes
- ✗Trying to checkout with uncommitted changes. Git refuses to switch if it would overwrite your changes - either commit, stash, or use -f to force (discards changes).
- ✗Confusing checkout for branches vs files. Accidentally restoring files when trying to switch branches - use -- before file paths: git checkout -- file.txt
- ✗Making commits in detached HEAD state. Commits become orphaned when you checkout another branch - create a branch first: git checkout -b my-branch
- ✗Using -B without understanding it overwrites. This can lose the existing branch's position - use -b for new branches, -B only when intentionally resetting.
FAQ
'git switch' is a newer command (Git 2.23+) that only handles branch switching, making it clearer and safer. 'git checkout' can both switch branches AND restore files, which can be confusing. Use 'git switch' for branches and 'git restore' for files if you prefer the modern approach.
Unfortunately, you can't undo this with Git alone - the changes are permanently discarded. Some IDEs have local history features that might help. This is why it's important to be careful with this command.
Detached HEAD means you've checked out a specific commit rather than a branch. You can look around and make commits, but those commits won't belong to any branch. Create a branch with 'git checkout -b name' to save your work.
'-b' creates a new branch and fails if it already exists. '-B' creates a new branch or resets an existing one to the current position. Use '-B' when you intentionally want to reset a branch.
Use 'git checkout other-branch -- path/to/file'. This copies the file from that branch into your working directory and staging area. You can then commit it to your current branch.
Git protects you from losing work. If switching branches would overwrite your changes, it refuses. Either commit your changes, stash them with 'git stash', or use 'git checkout -f' to force (which discards changes).
It switches to the previously checked out branch, similar to 'cd -' in the shell. It's a quick way to toggle between two branches you're working on.
First fetch with 'git fetch origin', then either 'git checkout branch-name' (Git auto-creates a tracking branch if origin/branch-name exists) or explicitly 'git checkout --track origin/branch-name'.
Yes, use 'git checkout -p <commit> -- <file>' for interactive mode, or simply 'git checkout <commit> -- <specific-files>' to restore only certain files from that commit.
Because it does two very different things: switch branches AND restore files. Git 2.23 introduced 'git switch' and 'git restore' to separate these concerns. 'git checkout' still works and is widely used.