git 如何在git中列出目录树的更改历史
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7093602/
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
How to list the change history of the directory tree in git
提问by Uwe Geuder
Git does not track directories as such. It only tracks files that live in some directory. (See How can I add an empty directory to a Git repository?)
Git 不会像这样跟踪目录。它只跟踪位于某个目录中的文件。(请参阅如何将空目录添加到 Git 存储库?)
However, if I have certain history of commits I implicitly also have history of changes to the directory tree.
但是,如果我有特定的提交历史,我隐含地也有目录树的更改历史。
So how do I answer questions like:
那么我该如何回答以下问题:
- when was directory foo/bar created (in git terminology: when was the first file created in that directory). There could be more than one qualifying commit if foo/bar has been deleted some time in history and recreated later.
- when was directory foo/bar removed (in git terminology: when was the last file removed from that directory). As above there could be more than one qualifying commit.
- what are the subdirectories of foo/bar that existed in any point of time in history
- 目录 foo/bar 何时创建(在 git 术语中:在该目录中创建的第一个文件是什么时候)。如果 foo/bar 在历史上的某个时间被删除并在以后重新创建,则可能会有多个符合条件的提交。
- 目录 foo/bar 何时被删除(在 git 术语中:从该目录中删除的最后一个文件是什么时候)。如上所述,可能有多个符合条件的提交。
- 历史上任何时间点都存在的 foo/bar 的子目录是什么
The closest I could come up with is in pseudo code:
我能想到的最接近的是伪代码:
loop over all commits (git rev-list --all)
start from repo root directory
do recursively on the directory tree rebuilt so far
call git ls-tree and grep the tree lines
rebuild next level of directory tree
end
end
Obviously this could be written in your favorite scripting language.
显然,这可以用您最喜欢的脚本语言编写。
Then I have all directory trees and I still need to search them in a smart way in order to be to answer questions of type 1 - 3. Again, doable but probably not in a couple of minutes.
然后我拥有所有目录树,我仍然需要以智能方式搜索它们,以便回答类型 1 - 3 的问题。同样,可行,但可能不会在几分钟内完成。
The questions are:
问题是:
- Is there an easier way?
- If not: are suitable the scripts already on the net? (my googling didn't reveal any, but I did not come up the perfect search words either)
- 有更容易的方法吗?
- 如果不是:是否适合网络上已有的脚本?(我的谷歌搜索没有显示任何内容,但我也没有找到完美的搜索词)
回答by eckes
For questions 1 and 2, it's quite easy:
对于问题 1 和 2,这很简单:
when was directory foo/bar created?
git log --oneline -- foo/bar | tail -n 1
when was directory foo/bar deleted?
git log --oneline -- foo/bar | head -n 1
目录 foo/bar 是什么时候创建的?
git log --oneline -- foo/bar | tail -n 1
目录 foo/bar 何时删除?
git log --oneline -- foo/bar | head -n 1
However, the third part is a bit tricky and I cannot answer it completely.
但是,第三部分有点棘手,我无法完全回答。
The two commands above give you $FIRST_REV
(created) and $LAST_REV
(deleted).
上面的两个命令给你$FIRST_REV
(created) 和$LAST_REV
(deleted)。
The following snippet gives you all commits where the tree was modified:
以下代码段为您提供了修改树的所有提交:
for rev in $(git rev-list FIRST_REV..LAST_REV)
do
git ls-tree -r -t $rev | grep tree | cut -f 2
done
Then, you have a list of directories that were present. But there are still duplicates. Pass that list to a sort -u
and you're done:
然后,您有一个存在的目录列表。但是还是有重复的。将该列表传递给 asort -u
就完成了:
#!/bin/sh
for r in $(git rev-list FIRST_REV..LAST_REV)
do
git ls-tree -r -t $r | grep tree | cut -f 2
done | sort -u
However, you lose the information of the commits where these directories were affected. That's the drawback.
但是,您会丢失这些目录受到影响的提交的信息。这就是缺点。
And, this assumes that foo/bar
was created only once and is no longer present.
并且,这假设foo/bar
只创建了一次并且不再存在。
回答by wadim
gitk foo/bar
gives you a user interface for browsing the git history limited to commits that touched foo/bar
.
gitk foo/bar
为您提供了一个用户界面,用于浏览仅限于接触过的提交的 git 历史记录foo/bar
。