git 显示文件的历史?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9807393/
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
Show history of a file?
提问by Chris
Possible Duplicate:
View the change history of a file using Git versioning
可能的重复:
使用 Git 版本控制查看文件的更改历史记录
Sometimes I want to step through the history of a particular file. In the past I used P4V and this was very quick and intuitive.
有时我想单步查看特定文件的历史记录。过去我使用 P4V,这非常快速和直观。
- Right click on a file and select history.
- Scrolling through the dates and see a nice diff of exactly what changed in thatfile on thatdate. Simple.
- 右键单击文件并选择历史记录。
- 通过日期滚动,看到的究竟是什么改变一个很好的差异是在文件该日期。简单的。
Switching to git this is now a grueling task.
切换到 git 现在是一项艰巨的任务。
- "git log filename"
- Look at history and pick a date, copy hash
- "git diff hash"
- Scroll through diff for the stuff that changed in the file I am interested in.
- Nope, that's not it, lets try a different date - back to step 2, rinse and repeat.
- “git 日志文件名”
- 查看历史并选择一个日期,复制哈希
- “git diff 哈希”
- 滚动 diff 以查找我感兴趣的文件中更改的内容。
- 不,不是这样,让我们尝试不同的日期 - 回到第 2 步,冲洗并重复。
I've searched SO, and I've tried a few of the commonly suggested guis: github, gitk, gitg, git-gui.
我搜索了 SO,并尝试了一些常用的 gui:github、gitk、gitg、git-gui。
These all remove the need to manually run commands, but the workflow is the same for this. View history of file; view commit; search through diff of lots of irrelevant files. It's slow and repetitive.
这些都消除了手动运行命令的需要,但工作流程是相同的。查看文件历史记录;查看提交;搜索大量不相关文件的差异。它缓慢且重复。
All the data is in the repo so I see no reason this simple common use case could not be more streamlined.
所有数据都在 repo 中,所以我认为这个简单的常见用例没有理由不能更加精简。
Can anyone recommend a tool that does this - or a more efficient way to utilize the command line to do what I want?
任何人都可以推荐一个可以执行此操作的工具 - 或者更有效的方式来利用命令行来做我想做的事?
Thanks for any suggestions.
感谢您的任何建议。
采纳答案by Pierre Mage
Have you tried this:
你有没有试过这个:
gitk path/to/file
回答by ralphtheninja
You can use git log to display the diffs while searching:
您可以在搜索时使用 git log 显示差异:
git log -p -- path/to/file
回答by Jeff Ferland
git log -p
will generate the a patch (the diff) for every commit selected. For a single file, use git log --follow -p $file
.
git log -p
将为每个选择的提交生成一个补丁(差异)。对于单个文件,请使用git log --follow -p $file
.
If you're looking for a particular change, use git bisect
to find the changein log(n) views by splitting the number of commits in half until you find where what you're looking for changed.
如果您正在查找特定更改,请使用通过将提交数量分成两半git bisect
来查找log(n) 视图中的更改,直到找到您要查找的更改的位置。
Also consider looking back in history using git blame
to follow changes to the linein question if you know what that is. This command shows the most recent revision to affect a certain line. You may have to go back a few versions to find the first change where something was introduced if somebody has tweaked it over time, but that could give you a good start.
如果您知道那是什么,还可以考虑使用回顾历史git blame
来跟踪相关行的更改。此命令显示影响某一行的最新修订。如果有人随着时间的推移对其进行了调整,您可能需要返回几个版本才能找到引入某些内容的第一个更改,但这可以为您提供一个良好的开端。
Finally, gitk
as a GUI does show me the patch immediatelyfor any commit I click on.
Example :
示例:
回答by LiKao
The main question for me would be, what are you actually trying to find out? Are you trying to find out, when a certain set of changes was introduced in that file?
对我来说主要的问题是,你到底想找出什么?您是否想知道该文件中何时引入了某些更改?
You can use git blame
for this, it will anotate each line with a SHA1 and a date when it was changed. git blame
can also tell you when a certain line was deleted or where it was moved if you are interested in that.
您可以git blame
为此使用它,它将用 SHA1 和更改日期来注释每一行。git blame
如果您对此感兴趣,还可以告诉您某行何时被删除或移动到何处。
If you are trying to find out, when a certain bug was introduced, git bisect
is a very powerfull tool. git bisect
will do a binary search on your history. You can use git bisect start
to start bisecting, then git bisect bad
to mark a commit where the bug is present and git bisect good
to mark a commit which does not have the bug. git will checkout a commit between the two and ask you if it is good or bad. You can usually find the faulty commit within a few steps.
如果您试图找出某个错误何时引入,这git bisect
是一个非常强大的工具。git bisect
将对您的历史进行二分搜索。您可以使用git bisect start
开始二等分,然后git bisect bad
标记存在错误的提交并git bisect good
标记没有错误的提交。git 将检查两者之间的提交,并询问您是好是坏。您通常可以在几个步骤内找到错误的提交。
Since I have used git, I hardly ever found the need to manually look through patch histories to find something, since most often git offers me a way to actually look for the information I need.
自从我使用过 git 以来,我几乎没有发现需要手动查看补丁历史记录来查找某些内容,因为大多数情况下,git 为我提供了一种实际查找所需信息的方法。
If you try to think less of how to do a certain workflow, but more in what information you need, you will probably many workflows which (in my opinion) are much more simple and faster.
如果您尝试较少考虑如何执行某个工作流程,而更多地考虑您需要哪些信息,那么您可能会有许多工作流程(在我看来)更简单和更快。