Git checkout

The git checkout command is used to switch between branches in a repository. It’s a versatile tool that also allows you to create and switch to a new branch in one step.



Tutorials dojo strip

Initial Example

To switch to an existing branch, use:

git checkout <branch_name>




CHECKOUT Options

OptionDescription
-b <branch>Create and switch to a new branch.
-B <branch>Create and switch to a new branch, or reset it if it already exists.
<branch>Switch to the specified branch.
<commit>Switch to the specified commit, entering a “detached HEAD” state.
-l, --detachSwitch to the specified commit, even if it’s a branch, resulting in a “detached HEAD” state.
-t, --trackSet up tracking configuration for a new branch created with -b.
--no-trackDo not set up tracking configuration for a new branch created with -b.
-d <path>Create and switch to a branch named after the directory at .
-m, --mergePerform a three-way merge between the current branch, the specified branch, and the upstream branch.
-f, --forceForce switch branches, even if the index or working directory has uncommitted changes.
--orphan <new_branch>Create a new branch without any commit history.
-p, --patchSelect changes interactively before switching.
--pathspec-from-file <file>Read pathspec from the specified file instead of the command-line arguments.
--ignore-other-worktreesIgnore other worktrees when switching branches.
--conflict <style>Specify conflict resolution style when performing a three-way merge.
--recurse-submodulesCheckout the specified commit in all submodules when switching branches.
-s <strategy>, --strategy <strategy>Use the specified merge strategy when performing a three-way merge.




Examples

1. Switching to Another Branch

To switch to a branch named feature-branch, use:

git checkout feature-branch

This moves you to the specified branch and updates the working directory to match.

2. Creating and Switching to a New Branch

To create a new branch and switch to it in one command, use:

git checkout -b new-branch

This creates a branch named new-branch and switches to it immediately.

3. Switching to a Commit or Tag

You can also use git checkout to switch to a specific commit or tag:

git checkout <commit_hash>

or

git checkout <tag_name>

This places you in a “detached HEAD” state, where you can view the state of the repository at that specific commit or tag.

Scroll to Top