Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

git checkout Command

git checkout Command

Git Basic OperationsGit Basic Operations


git checkout The command is used for switching between different branches, restoring files, creating new branches, and other operations.

Note:The git checkout command, after Git version 2.23, introduced the git switch and git restore commands, respectively used for branch switching and file restoration, to provide clearer semantics and error checking. If you use a newer version of Git, you can consider using these commands instead of git checkout.

Switch branches:

The following command allows you to switch from the current branch to the specified branch :

git checkout 

For example, to switch your working directory to the main branch:

git checkout master 

Create a new branch and switch:

The following command is used to create a new branch and immediately switch to the newly created branch:

git checkout -b 

For example, create a new branch named feature-branch and switch to it:

git checkout -b feature-branch

Switch to the previous branch:

The following command lets you quickly switch back to the previous branch without having to remember the branch name:

git checkout -

Check out files:

The following command can restore a specified file in the working directory to the state of the most recent commit, discarding all uncommitted changes; this is very useful for undoing unwanted changes:

git checkout -- 

Specifically, this command undoes all uncommitted changes to the file, restoring it to the state recorded by the current branch's HEAD (the latest commit).

Switch to a specific commit:

You can use the commit's hash value to switch to a specific commit state. This will put you in a "detached HEAD" state, where you can only view history and cannot perform branch operations. In general, it is not recommended to work in a detached HEAD state, because changes may be lost.

git checkout 

Switch to a tag:

If you have a tag , you can use this command to switch to the commit state that the tag points to.

git checkout tags/


Git Basic OperationsGit Basic Operations