使用 Git 版本控制查看文件的更改历史记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/278192/
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
View the change history of a file using Git versioning
提问by Richard
How can I view the change history of an individual file in Git, complete details with what has changed?
如何在 Git 中查看单个文件的更改历史记录,以及更改内容的完整详细信息?
I have got as far as:
我已经到了:
git log -- [filename]
which shows me the commit history of the file, but how do I get at the content of each of the file changes?
它向我展示了文件的提交历史,但我如何获得每个文件更改的内容?
I'm trying to make the transition from MS SourceSafe and that used to be a simple right-click
→ show history
.
我正在尝试从 MS SourceSafe 过渡,这曾经是一个简单的right-click
→ show history
。
采纳答案by Claudio Acciaresi
For this I'd use:
为此,我会使用:
gitk [filename]
or to follow filename past renames
或跟随文件名过去重命名
gitk --follow [filename]
回答by VolkA
You can use
您可以使用
git log -p filename
to let git generate the patches for each log entry.
让 git 为每个日志条目生成补丁。
See
看
git help log
for more options - it can actually do a lot of nice things :) To get just the diff for a specific commit you can
更多选项 - 它实际上可以做很多好事:) 要获得特定提交的差异,您可以
git show HEAD
or any other revision by identifier. Or use
或按标识符进行的任何其他修订。或使用
gitk
to browse the changes visually.
直观地浏览更改。
回答by Dan Moulding
git log --follow -p -- path-to-file
git log --follow -p -- path-to-file
This will show the entirehistory of the file (including history beyond renames and with diffs for each change).
这将显示文件的整个历史记录(包括重命名以外的历史记录以及每次更改的差异)。
In other words, if the file named bar
was once named foo
, then git log -p bar
(without the --follow
option) will only show the file's history up to the point where it was renamed -- it won't show the file's history when it was known as foo
. Using git log --follow -p bar
will show the file's entire history, including any changes to the file when it was known as foo
. The -p
option ensures that diffs are included for each change.
换句话说,如果命名的文件bar
曾经被命名foo
,那么git log -p bar
(没有--follow
选项)将只显示文件的历史记录到它被重命名的点——当它被称为foo
. Usinggit log --follow -p bar
将显示文件的整个历史记录,包括文件被称为foo
. 该-p
选项可确保每次更改都包含差异。
回答by Falken
If you prefer to stay text-based, you may want to use tig.
如果您更喜欢基于文本,则可能需要使用tig。
Quick Install:
快速安装:
- apt-get:
# apt-get install tig
- Homebrew (OS X):
$ brew install tig
- apt-get:
# apt-get install tig
- 自制软件(OS X):
$ brew install tig
Use it to view history on a single file: tig [filename]
Or browse detailed repo history: tig
使用它查看单个文件的历史记录:tig [filename]
或浏览详细的 repo 历史记录:tig
Similar to gitk
but text based. Supports colors in terminal!
类似于gitk
但基于文本。支持终端颜色!
回答by farktronix
git whatchanged -p filename
is also equivalent to git log -p filename
in this case.
git whatchanged -p filename
git log -p filename
在这种情况下也等效。
You can also see when a specific line of code inside a file was changed with git blame filename
. This will print out a short commit id, the author, timestamp, and complete line of code for every line in the file.
This is very useful after you've found a bug and you want to know when it was introduced (or who's fault it was).
您还可以查看文件中的特定代码行何时更改为git blame filename
. 这将为文件中的每一行打印出简短的提交 ID、作者、时间戳和完整的代码行。在您发现错误并且您想知道它何时被引入(或者是谁的错)之后,这非常有用。
回答by Mark Fox
SourceTree users
SourceTree 用户
If you use SourceTree to visualize your repository (it's free and quite good) you can right click a file and select Log Selected
如果您使用 SourceTree 来可视化您的存储库(它是免费的并且非常好),您可以右键单击一个文件并选择Log Selected
The display (below) is much friendlier than gitk and most the other options listed. Unfortunately (at this time) there is no easy way to launch this view from the command line — SourceTree's CLI currently just opens repos.
显示(如下)比 gitk 和列出的大多数其他选项友好得多。不幸的是(此时)没有简单的方法从命令行启动这个视图——SourceTree 的 CLI 目前只是打开 repos。
回答by yllohy
To show what revision and author last modified each line of a file:
要显示文件的每一行最后修改的版本和作者:
git blame filename
or if you want to use the powerful blame GUI:
或者,如果您想使用强大的blame GUI:
git gui blame filename
回答by John Lawrence Aspden
Summary of other answers after reading through them and playing a bit:
通读并玩了一下其他答案后的总结:
The usual command line command would be
通常的命令行命令是
git log --follow --all -p dir/file.c
But you can also use either gitk (gui) or tig (text-ui) to give much more human-readable ways of looking at it.
但是您也可以使用 gitk (gui) 或 tig (text-ui) 来提供更多人类可读的查看方式。
gitk --follow --all -p dir/file.c
tig --follow --all -p dir/file.c
Under debian/ubuntu, the install command for these lovely tools is as expected :
在 debian/ubuntu 下,这些可爱工具的安装命令如预期的那样:
sudo apt-get install gitk tig
And I'm currently using:
我目前正在使用:
alias gdf='gitk --follow --all -p'
so that I can just type gdf dir
to get a focussed history of everything in subdirectory dir
.
这样我就可以只键入gdf dir
以获取子目录中所有内容的重点历史记录dir
。
回答by Palesz
Add this alias to your .gitconfig:
将此别名添加到您的 .gitconfig:
[alias]
lg = log --all --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'\n--abbrev-commit --date=relative
And use the command like this:
并使用这样的命令:
> git lg
> git lg -- filename
The output will look almost exactly the same as the gitk output. Enjoy.
输出看起来与 gitk 输出几乎完全相同。享受。
回答by lang2
Lately I discovered tig
and found it very useful. There are some cases I'd wish it does A or B but most of the time it's rather neat.
最近我发现tig
并发现它非常有用。在某些情况下,我希望它做 A 或 B,但大多数时候它相当整洁。
For your case, tig <filename>
might be what you're looking for.
对于您的情况,tig <filename>
可能就是您正在寻找的。