简体   繁体   English

如何将文件匹配到git commit?

[英]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. 假设在网站构建过程中,我将git项目复制到可通过网络访问的目录中,然后删除.git目录。 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? 现在,几天后,我在公共Web目录之前进行了几次提交操作之后,找出当前正在提供哪个版本的/index.html的简单方法是什么?

# /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. 我唯一想做的就是编写一个脚本来检出git中文件的每个版本并检查每个版本,但是根据我对git的有限经验,我发现通常有一些规范的方法可以完成我执行的git任务梦想起来。

Using git-describe is a good idea. 使用git-describe是个好主意。 Also, you might consider using git-archive to generate a tarball rather than cloning the git repository and then deleting the .git directory. 另外,您可能会考虑使用git-archive生成tarball,而不是克隆git存储库,然后删除.git目录。 Keep the archive around and use git-get-commit-tar-id to find the commit. 保留存档,并使用git-get-commit-tar-id查找提交。 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? 要回答您的实际问题,请使用git-hash-object获取有问题的文件的blob,然后查看对此的答案哪个提交有该blob?

As part of your build process, run git describe > version_file and put that file with the rest of your files. 在构建过程中,请运行git describe > version_file并将该文件与其余文件放在一起。 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. 如果希望在工作时在git中向您提供信息,另一种选择是在构建过程中设置对提交的引用(分支或轻量级标记),并在每次构建时覆盖它。

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. 最后一个更接近您要求的选项是使用.gitattributes中的ident过滤器,用文件哈希替换每个文件中的$ Id $。 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 如果您有时间知道文件匹配,则可以使用bisect查找文件更改的时间,因此您无需查看每个版本

If you don't know any revision that matches the file you would need to inspect all of the revisions 如果您不知道与文件匹配的任何修订,则需要检查所有修订

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM