Checkout
git checkout
is a fundamental Git command used for switching branches, restoring files from previous commits, creating new branches, and more. It has been a core part of Git since its inception and remains widely used. However, Git has introduced newer commands like git switch
and git restore
that offer similar functionality and improved features.
Features of git checkout
-
Switching Branches
git checkout <branch>
Allows you to switch between branches in your repository. -
Creating New Branches
git checkout -b <new-branch>
Creates a new branch and switches to it in one step. -
Restoring Files
git checkout <commit> -- <file>
Restores a specific file from a previous commit to the working directory. -
Discarding Local Changes
git checkout -- <file>
Discards local changes made to a specific file and restores it to the state from the last commit. -
Checking Out Files from Other Branches
git checkout <branch> -- <file>
Checks out a specific file from another branch into the current branch's working directory.
Comparison with Newer Commands
Previous command | New command | Explanation |
---|---|---|
git checkout <branch> |
git switch <branch> |
Both commands are used to switch branches. The new command git switch offers a clearer syntax for this task. |
git checkout |
N/A (use git status ) |
The git checkout command without arguments is often used to check the status of the repository. |
git checkout -b <new_branch> [<start_point>] |
git switch -c <new-branch> [<start-point>] |
Both commands are used to create and switch to a new branch. The new command git switch -c offers a clearer syntax for branch creation. |
git checkout -B <new_branch> [<start_point>] |
git switch -C <new-branch> [<start-point>] |
Both commands are used to forcibly create and switch to a new branch. The new command git switch -C offers a clearer syntax for this task. |
git checkout --orphan <new_branch> |
git switch --orphan <new-branch> |
Both commands are used to create a new orphan branch. |
git checkout --orphan <new_branch> <start_point> |
N/A (use git switch <start-point> then git switch --orphan <new-branch> ) |
There is no direct equivalent for this operation. You can achieve similar behavior by first switching to the start point and then creating the orphan branch. |
git checkout [--detach] <commit> |
git switch --detach <commit> |
Both commands are used to checkout a specific commit in a detached HEAD state. |
git checkout --detach [<branch>] |
git switch --detach [<branch>] |
Both commands are used to detach the HEAD from the current or specified branch. |
git checkout [--] <pathspec>… |
git restore [--] <pathspec>… |
Both commands are used to restore files in the working directory. The new command git restore provides a clearer syntax for this operation. |
git checkout --pathspec-from-file=<file> |
git restore --pathspec-from-file=<file> |
Both commands are used to restore files specified in a file. The new command git restore provides a clearer syntax for this operation. |
git checkout <tree-ish> [--] <pathspec>… |
git restore -s <tree> [--] <pathspec>… |
Both commands are used to restore files from a specific tree. The new command git restore provides a clearer syntax for this operation. |
git checkout <tree-ish> --pathspec-from-file=<file> |
git restore -s <tree> --pathspec-from-file=<file> |
Both commands are used to restore files specified in a file from a specific tree. The new command git restore provides a clearer syntax for this operation. |
git checkout -p [<tree-ish>] [--] [<pathspec>…] |
git restore -p [-s <tree>] [--] [<pathspec>…] |
Both commands are used to interactively restore changes from a specific tree. The new command git restore provides a clearer syntax for this operation. |
As shown by this comparison, some prior usages can be converted to the new commands by simply replacing the old command name (checkout) with the new one (switch, restore), whereas others require additional adjustment. Notable changes include:
- The -b/-B options for creating a new branch before switching are renamed to -c/-C. They also have long option variants (--create/--force-create), unlike before.
- --detach (or -d) is now always required when switching to a detached head, where it was previously optional for commits but required for branches.
- The source tree for restoring is now given by the -s (or --source) option, rather than being an inline argument.
- Switching using --force (or -f) now fails if there are unmerged entries, rather than ignoring them. --force has also been renamed to --discard-changes, with --force being kept as an alias.
git switch
Provides a clearer and more intuitive syntax specifically for switching branches.
git restore
Offers dedicated options for restoring files and discarding changes, providing a more explicit and versatile interface for these operations.
Use Cases
- Simple Branch Switching For basic branch switching operations,
git checkout
remains a straightforward and commonly used command. - File-Level Operations When performing file-level operations like restoring files from commits or discarding local changes,
git checkout
provides a concise syntax. - Advanced Branch Management For more advanced branch management tasks or when using newer Git features,
git switch
andgit restore
may offer advantages in terms of clarity and functionality.
git checkout
is a versatile Git command with various features for branch switching, file restoration, and more. While newer commands like git switch
and git restore
offer some improvements and specialized functionality, git checkout
remains a fundamental and widely used command in Git workflows. Depending on the specific task at hand and personal preference, you may choose to use git checkout
or explore the capabilities of newer Git commands for branch and file management.