Git

Checkout Branches, Commits, & Tags

Git is a versioning control system used to track and save the added changes. These changes can be made by developers. In Git, the term checkout refers to the process of switching or notifying Git to which commit, branch, or tag developers want changes applied. More specifically, the “$ git checkout” command is used for this specified purpose.

In this tutorial, we will talk about:

Let’s move ahead to the mentioned points!

How to Checkout Commits?

Git users can checkout commits by following the given procedure.

Step 1: Move to Git repository

Run the “cd” command to navigate to the desired repository:

$ cd "C:\Users\nazma\Git\Comments"

Step 2: Create new File

Create a new file by utilizing the “touch” command:

$ touch file1.txt

Step 3: Track File

Next, add the newly created file to staging area through the following command:

$ git add file1.txt

Step 4: Update to Repository

Run the “git commit” command with “-m” flag:

$ git commit -m "new file added"

Step 5: Check Log History

View the current branch log history using provided command:

$ git log .

Next, copy the required commit reference number to the clipboard:

Step 6: Commit Checkout

Lastly, execute the “git checkout” command and checkout to copied commit reference:

$ git checkout 3b80ca9

Now, move ahead to understand how to checkout branches.

How to Checkout Branches?

If you want to check out the Git local repository branch, follow the below-listed steps.

Step 1: List Local Branches

Run the “git branch” command with the “-a” option:

$ git branch -a

Adding -a option will enlist all existing local and remote branches:

Step 2: Checkout Branch

Next, run the “git checkout” command to the checkout the required branch:

$ git checkout alpha

The below output indicates that we have checkout successfully to the existing branch:

Now, move ahead and check out how to switch tags.

How to Checkout Tags?

While working on Git, tags are created to have references to the release version. To checkout tags, follow the below-listed steps:

Step 1: List Tags

View the list of tags by utilizing the “git tag” command:

$ git tag

Select any of the tags from the printed list:

Step 2: Checkout Tags

Execute the “git checkout” command with tags:

$ git checkout v1.0

As you can see, the HEAD pointer is moved to tag:

We have compiled the procedure to check out the commits, branches, and tags.

Conclusion

Git users can check out branches, commits, and tags. To check out branches, the “$ git checkout <branch-name>” command is used. The “$ git checkout <commit-ref>” command can be utilized to check out commits. If you want to check out tags, run the “$ git checkout <tag>” command. In this tutorial, we have demonstrated the method to check out branches, commits, and tags.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.