How can I match a file to a git commit?
Let's say as part of the build process for a website, I copy a git project to a web-accessible directory and remove the .git directory. Now, a few days later after I'm a few commits ahead of the public web directory, what's a simple way to figure out which version of /index.html is currently being served?
# /var/www (public web dir):
/index.html
开发者_运维问答# Git repository:
HEAD:/index.html
HEAD~1:/index.html
HEAD~2:/index.html
.
. <-- /var/www/index.html matches in here somewhere.
.
HEAD~N:/index.html
The only thing I can think to do is write a script to checkout every version of the file in git and check each one, but in my limited experience with git I've found that there is usually some canonical way to accomplish whatever git task I dream up.
Using git-describe is a good idea. Also, you might consider using git-archive to generate a tarball rather than cloning the git repository and then deleting the .git directory. Keep the archive around and use git-get-commit-tar-id to find the commit. To answer your actual question, use git-hash-object to get the blob of the file in question, then see the answers to this Which commit has this blob?
As part of your build process, run git describe > version_file
and put that file with the rest of your files. This way you always have a reference to what commit the files are from.
Another option if you want the info available to you in git while working is to set a reference to the commit as part of your build process, either a branch or a lightweight tag and overwrite it on every build.
A final option that is closer to what you asked than what I assumed you ment, is to use the ident filter in .gitattributes to replace $Id$ in each file with the files hash. The problem with this though is that it will only allow you to figure out which commits (multiple) contain this exact version of the file and not which commit (single) the file came from.
If you have a point in time that you know that the file matches you can use bisect to find when the file changed, so you don't need to look at every version
If you don't know any revision that matches the file you would need to inspect all of the revisions
精彩评论