问题描述
我正要在现有项目中移动大量文件。在执行此操作之前,我想牢牢掌握一些用于分析具有多文件历史记录的代码的技术。
I'm about to move a ton of files around in an existing project. Before I do this, I'd like to have a firm grasp on some techniques used to analyze code that has a multi-file history.
如何使用git问:这行代码来自哪里?当内容已经移动到它的生命期中的多个文件?
How can I use git to ask: "where did this line of code come from?" when the content has moved through multiple files in it's life time?
我知道git不会显式地跟踪重命名(出于很好的理由),所以看起来我应该可以问这个,我只是不知道如何。如果有方法可视化这也是很好的知道。
I know git doesn't track renames explicitly (for good reason) so it seems like I should be able to ask this, I'm just not sure how. If there are method's to visualize this that'd be great to know too.
推荐答案
我会建议看看三个工具混帐工具箱。第一个是怪,这和CVS中的非常相似。它向您显示哪个提交最后一次触及文件中的每一行。如果你想看看之前有什么,你可以采取触及该行的提交并查看前一个提交。
I would recommend looking at three tools in the git toolbox. The first one is blame, which is very much the same as it is in cvs. It shows you which commit last touched each line in a file. If you want to look see what was there before, you can take the commit that touched the line and look at the previous commit.
git show <sha1_of_interesting_commit>^ -- file/path
你可以重复一遍,
git blame <sha1_of_interesting_commit>^ -- file/path
第二个工具是使用 - 跟踪
来跟踪过去重命名的文件。
The second tool is using --follow
to track files past renames.
git log --follow -- file/path
第三个 - 也可能是最有用的工具 - 是记录镐的选项。这将搜索历史记录以更改包含给定位文本的已删除,已引入或已更改的行。这对跟踪诸如函数名称之类的东西特别有用。它可能在特定提交中的文件中是新的,但它是否来自不同的源文件?是否在同一时间或在它移动之前添加了一个调用?
The third - and possibly the most useful tool - is the pickaxe option to log. This searches history for changes the removed, introduced or changed lines that include a given bit of text. This is especially useful for tracking things like function names. It may be new in a file in a particular commit, but did it come from a different source file? Was a call to it added at the same time, or before it moved?
git log -S"Interesting_Function"
如果您使用的是补丁或统计选项(例如 -p
或者 - stat
),那么输出将被限制为那些实际上涉及搜索字符串的文件,除非您还使用 - pickaxe-all
If you are using a patch or stat option (e.g. -p
or --stat
) the output will be restricted to those files whose changes actually involved the search string unless you also use --pickaxe-all
where the whole change is displayed.
结合 git grep
来显示所有更改当前出现的字符串是,镐是一个非常有用的历史挖掘工具。
Combined with git grep
to show where all the current occurrences of a string are, pickaxe is an extremely useful history mining tool.
这篇关于Git:如何分析具有多文件历史记录的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!