checkout Git Command Guide
The git checkout command is used to switch branches or restore working tree files. It updates files in the working tree to match the version in the index or the specified tree, and can also update HEAD to set the specified branch as the current branch.
git checkout Syntax:
Section titled “git checkout Syntax:”git checkout [-q] [-f] [-m] [<branch>]git checkout [-q] [-f] [-m] [[-b|-B|--orphan] <new-branch>] [<start-point>]git checkout [-f] <tree-ish> [--] <pathspec>...git checkout (-p|--patch) [<tree-ish>] [--] [<pathspec>...]Options:
Section titled “Options:”| Option | Description |
|---|---|
| -q, —quiet | Suppress feedback messages |
| -f, —force | Force checkout, ignore untracked files |
| -m | Allow merging working tree changes |
| -b | Create and checkout new branch |
| -B | Reset existing branch and checkout |
| —detach | Detach HEAD at specified commit |
| —ours, —theirs | Checkout stage #2 (ours) or #3 (theirs) for merges |
| —conflict= | Specify merge conflict resolution style |
| -p, —patch | Interactively select hunks to checkout |
| —progress | Show progress status |
| —orphan | Create orphan branch |
| —track | Set up branch tracking (implied with -b) |
| —no-track | Don’t set up branch tracking |
Parameters:
Section titled “Parameters:”| Parameter | Description |
|---|---|
| [ | Branch to switch to (default: current if empty) |
| [ | Starting point for new branch |
| [ | Tree/commit to checkout files from |
| [ | Files/paths to checkout |
git checkout Command Samples:
Section titled “git checkout Command Samples:”Switch to a branch
Section titled “Switch to a branch”git checkout developSwitch to the develop branch and update working tree.
Create and switch to new branch
Section titled “Create and switch to new branch”git checkout -b feature/new-featureCreate and switch to a new branch called feature/new-feature.
Force checkout, discard changes
Section titled “Force checkout, discard changes”git checkout -f mainForce switch to main branch, discarding all local changes and untracked files.
Checkout specific files from another branch
Section titled “Checkout specific files from another branch”git checkout origin/main -- README.mdCheckout README.md file from origin/main branch.
Create orphan branch (new root commit)
Section titled “Create orphan branch (new root commit)”git checkout --orphan new-projectCreate new orphan branch for completely different project history.
Detach HEAD at specific commit
Section titled “Detach HEAD at specific commit”git checkout --detach HEAD~5Detach HEAD at 5 commits back from current position.
Interactive hunk selection
Section titled “Interactive hunk selection”git checkout -p HEAD~1 -- file.txtInteractively choose which hunks to checkout from HEAD~1 for file.txt.
Resolve merge conflicts checking out our version
Section titled “Resolve merge conflicts checking out our version”git checkout --ours conflicted-file.txtCheckout our version (stage 2) of conflicted-file.txt.
Reset existing branch
Section titled “Reset existing branch”git checkout -B feature/in-progress HEAD~10Reset feature/in-progress branch to 10 commits back and switch to it.
How do I switch to an existing branch?
Section titled “How do I switch to an existing branch?”To switch to an existing branch, run:
git checkout <branch-name>How can I create a new branch and switch to it?
Section titled “How can I create a new branch and switch to it?”To create a new branch and switch to it, use:
git checkout -b <new-branch-name>How do I discard local changes and go back to last commit?
Section titled “How do I discard local changes and go back to last commit?”To discard local changes and reset working tree, use:
git checkout -f HEADHow can I checkout a specific file from another branch?
Section titled “How can I checkout a specific file from another branch?”To checkout a specific file from another branch, run:
git checkout <branch> -- path/to/fileWhat’s the difference between git switch and git checkout?
Section titled “What’s the difference between git switch and git checkout?”git switch focuses only on branch switching while git checkout has broader functionality including file and conflict resolution. git switch is more intuitive for branch operations.
How do I resolve merge conflicts by choosing sides?
Section titled “How do I resolve merge conflicts by choosing sides?”To resolve conflicts by choosingvin our side, use:
git checkout --ours <file>git checkout --theirs <file>How can I inspect a historical commit without switching?
Section titled “How can I inspect a historical commit without switching?”To inspect a historical commit without switching branches, use:
git checkout --detach <commit-hash>Applications of the git checkout command
Section titled “Applications of the git checkout command”- Switching between branches during development
- Creating feature branches for new work
- Restoring individual files from other commits
- Resolving merge conflicts by choosing sides
- Inspecting historical versions of files
- Temporarily checking out tags or commits for inspection