Open In App

How to Checkout a Tag in Git?

Last Updated : 06 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When working with Git, checkout a tag is one of the basic operations. Tags are pointers that show certain points in the history of your project and usually indicate releases or some important stages. The purpose of checkout a tag is to make your working directory represent the repository as it was at the time when the tag was done.

In this article, we will see how we can check out a tag in Git.

Approach: Using git checkout command

Step 1: Open your terminal or command prompt.

Step 2: Navigate to your Git repository using cd command.

Step 3: Execute the following command to check out the desired tag:

Screenshot-2024-04-22-202814

command to check out the desired tag.

Replace `<tag_name>` with the name of the tag you want to checkout.

We can use checkout command to discard unstaged changes in the tracked files of working directory.

  1. Only for working directory.
  2. To discard unstaged changes (The changes which are not added to staging area).
  3. In the tracked files (The files which are already added to staging area/commit).

Example:

1: Create one folder

Screenshot-2024-04-24-164736

folder

2: Open git bash

3: Creating new file using git command vim filename

Screenshot-2024-04-24-165034

creating new file using git command.

4: After that we are writing some lines in file then save the file using git command

:wq!  
Screenshot-2024-04-24-165417

save the file using git command.

5: We need to create repository using command git init and this command initialized empty repository.

Screenshot-2024-04-24-165645

initialized empty repository.

6: Now we add this file in staging and also commit the local repository using the command

git and  .(dot) 

this command add the particular file in staging.

Screenshot-2024-04-24-165943

commit the local repository

7: After committing this file in local repository using command git commit -m “first commit” . After using this command one file is added in repository and created file is called tracking file.

Screenshot-2024-04-24-170411

file is added in repository.

8: Currently file is containing only two lines we can see using command cat filename

Screenshot-2024-04-24-170731

file containing data.

9: Now we are trying to add some more lines in butler file then first open the file using command

vim filename 

after that add two additional files in file1.

Screenshot-2024-04-24-171314

add some more lines in butler file.

10: We can see using the command

cat filename
Screenshot-2024-04-24-171518

two new lines added.

11: Stagging and local repository we have only two line that means two new lines (third and fourth lines are not stagging in the area that means they have unstaged changes. This change is not a part of stagging and local repository we can called as a unstaged changes.

12: After that we want to discard these things that means we want our stage file and working directory equal then we used checkout command

git checkout -- filename
Screenshot-2024-04-24-172135

checkout command

After that these two lines are discarded from working directory file. Now again use command cat filename. You can see two lines of code. So that means able to discard unstaged changes in the tracked files of working directory.

Screenshot-2024-04-24-172154

discard unstaged changes in the tracked files.

checkout command is only applicable for tracked file not for untracked file.

Untracked file means file which are not adding stagging and local repertory that called is an untracked file.

Example:

1: first we create second file using command

vim filename
Screenshot-2024-04-24-173241

creating new file using git command.

2: After that we are trying to checkout these files. They will throw error. Now we are using command

git checkout filename
Screenshot-2024-04-24-173302

throw error.

Now this is giving error because git is not aware of this file. Because this file is still in out working directory even staging area and local repository is not aware of this file that’s reason we called untracked file.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads