Git checkout command is used for switching branch or a specific commit. It will work on branches, commits, and files too. Git provides lots of options to use with this command. You can find all the documentation here.
Post Contents
# Checking Out Branch
To checkout (switch) branch execute the following command. A remote branch can be checkout with this command too, but you need to fetch all remote branches to local copy with git fetch --all command. Switching branches allows you to work on multiple tasks simultaneously.
git checkout <branch-name>
In order to force checkout to the branch use -f or --force option.
git checkout -f <branch-name>
git checkout --force <branch-name>
Also, you can create a new branch and checkout the newly created branch using -b option.
git checkout -b <branch-name>
Remember, its recommended to commit all files before checking out to another branch. Otherwise, all of the modified files will be shown in the latest checkout branch or commit. You can check it with git status.
# Checking Out To Specific Commit
Run the following command to checkout specific commit.
git checkout <commit-id>
Similarly, use HEAD~1 to revert specific version using. It’s called a detached HEAD. Primarily, this command update HEAD to a specific commit. Keep in mind that after detaching HEAD into a specific commit, all of your new changes will record in that commit and there is no reference of a branch. Simply, use git branch branch-name to point branch to your detached HEAD. This will re-attach current HEAD to given branch. You can use any number with HEAD~ to checkout specific commit.
git checkout HEAD~1