Reputation: 2708
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?
Upvotes: 24
Views: 25108
Reputation: 2851
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.
Upvotes: 33