git - 查找添加文件的提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11533199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 14:08:51  来源:igfitidea点击:

git - Find commit where file was added

gitcommitgit-commitgit-log

提问by Steven Penny

Say I have a file foo.jsthat was committed some time ago. I would like to simply find the commit where this file was first added.

假设我有一个foo.js前一段时间提交的文件。我只想找到首次添加此文件的提交。

After reading the answers and my own tinkering, this works for me

阅读答案和我自己的修补后,这对我有用

git log --follow --diff-filter=A --find-renames=40% foo.js

回答by stelterd

Here's simpler, "pure Git" way to do it, with no pipeline needed:

这是一种更简单的“纯 Git”方式,无需管道:

git log --diff-filter=A -- foo.js

Check the documentation. You can do the same thing for Deleted, Modified, etc.

检查文档。您可以对已删除、已修改等执行相同的操作。

https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203

https://git-scm.com/docs/git-log#Documentation/git-log.txt---diff-filterACDMRTUXB82308203

I have a handy alias for this, because I always forget it:

我有一个方便的别名,因为我总是忘记它:

git config --global alias.whatadded 'log --diff-filter=A'

This makes it as simple as:

这使它变得如此简单:

git whatadded -- foo.js


The below one liner will recursively search through sub directories of the $PWDfor foo.jswithout having to supply and absolute or relative path to the file, nor will the file need to be in the same directory as the $PWD

下面的一行将递归搜索$PWDfor 的子目录,foo.js而无需提供文件的绝对或相对路径,文件也不需要与for位于同一目录中$PWD

git log --diff-filter=A -- **foo.js

回答by Seth Robertson

git log --oneline -- foo.js | tail -n 1

回答by Reidel

The following may not be not of you interest, but I think it will help you in future and is part of debugging ecosystem in Git:

您可能不感兴趣以下内容,但我认为它会在将来对您有所帮助,并且是 Git 中调试生态系统的一部分:

You can use git-blameto show what revision and author last modified each line of a file, especially file annotation. Visit https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git

您可以使用git-blame来显示文件的每一行最后修改的是哪个修订版和作者,尤其是文件注释。访问https://git-scm.com/book/en/v2/Git-Tools-Debugging-with-Git

For example,

例如,

git blame -L 174,190  xx.py