git checkout command
git checkoutThe command is used for operations such as switching between different branches, restoring files, and creating new branches.
Note:The git checkout command, after Git version 2.23, introduced...git switchandgit restoreCommands, used for branch switching and file restoration respectively, to provide clearer semantics and error checking. If you are using a newer version of Git, you can consider using these commands instead of git checkout.
Switch branch:
The following command allows you to switch from the current branch to the specified branch:<branch-name>:
git checkout <branch-name>
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:<new-branch-name>And immediately switch to the newly created branch:
git checkout -b <new-branch-name>
For example, to create a new branch named feature-branch and switch to it:
git checkout -b feature-branch
Switch to the previous branch:
The following command allows you to quickly switch back to the previous branch without needing to remember the branch name:
git checkout -
Checkout file:
The following command can restore specified files in the working directory:<file>Restore to the state of the most recent commit, discarding all uncommitted changes, which is very useful for undoing unwanted changes:
git checkout -- <file>
Specifically, this command undoes all uncommitted changes to the files and restores them to the state recorded by the HEAD (latest commit) of the current branch.
Switch to a specific commit:
You can use the commit hash<commit-hash>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. Under normal circumstances, it is not recommended to work in a detached HEAD state because changes may be lost.
git checkout <commit-hash>
Switch to a tag:
If you have a tag<tag-name>, you can use this command to switch to the commit state that the tag points to.
git checkout tags/<tag-name>
other extensions