Git Checkout Documentation, Usage, Examples

0
219
How Does Git Checkout Work

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.

# 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

Learn more about Git.

Previous articleGit Branch All Commands In-depth
Next articleLaravel Email Verification (version 5.7-7) Tutorial
Welcome to the world of web development, where technology and creativity come together to bring ideas to life. My name is Keyur Gadher, and as a web development blogger, I am here to share my knowledge and experience with you. From front-end web development to back-end programming, I will provide you with valuable tips, tricks, and techniques to help you create beautiful and functional websites that are tailored to your specific needs. Whether you're a beginner or a seasoned pro, I am here to help you take your web development skills to the next level. Join me today and let's start coding!

LEAVE A REPLY

Please enter your comment!
Please enter your name here