Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to checkout a file in git based on a tag

Tags:

I have a commit hash abcx and I have given it a tag 100. Now I want to checkout a file example.pl to that tag 100. Is it possible in git?
I can do git checkout abcs example.pl, but I directly want to checkout a file based on the tag.

Can anyone help me with this please?

like image 805
Newbie Avatar asked Apr 27 '15 10:04

Newbie


People also ask

Can you checkout a tag in git?

Checking Out TagsYou can view the state of a repo at a tag by using the git checkout command. The above command will checkout the v1. 4 tag.

How do I force git to checkout a file?

Force a Checkout You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How do I search for a specific tag in git?

In order to find the latest Git tag available on your repository, you have to use the “git describe” command with the “–tags” option. This way, you will be presented with the tag that is associated with the latest commit of your current checked out branch.


1 Answers

Because of how tags work in Git, they are basically just read only branches which you can treat as any branch in Git. You can address them as tags/<tag>.

To checkout the entire state of the repository to the working directory with a tag, you write:

git checkout tags/<yourtag> 

But as with a case of normal branches you can decrease the scope of the checkout to individual files by listing them:

git checkout tags/<yourtag> <file1> <file2> ... 

The behaviour you are seeing with the log happens because of the fact that Git has two methods of pointing to the repositories. Either your local repository pointer is being changed when you checkout entire repository with:

git checkout <tag/hash> 

or individual statues of the files changes when you check them out to specific state with:

git checkout <tag/hash> filelist 

Even if you do checkout all your files to the previous repository state:

git checkout <tag/hash> * 

your pointer of local repository won't change. The git log is using the local repository pointer to log the changes on the file so it will be aware of the "future" changes that happened before the state of the file you checked it into. So from the git perspective command:

git checkout <tag/hash/branch> file 

is not affecting the repository pointer, it is only changing the state of the files to match the specific point in time, if you will run the git status you will see those files as local changes, it doesn't differ in any way from taking the copy paste from the state of the file in the repository.

like image 62
cerkiewny Avatar answered Sep 20 '22 15:09

cerkiewny