如何查看单个文件的历史记录以及更改内容的完整详细信息?

git-log--[filename]显示文件的提交历史,但如何查看更改的文件内容?


当前回答

您也可以尝试这个方法,它列出了更改文件特定部分的提交(在Git1.8.4中实现)。

返回的结果将是修改此特定部分的提交列表。命令:

git log --pretty=short -u -L <upperLimit>,<lowerLimit>:<path_to_filename>

其中upperLimit是文件的起始行号,lowerLimit是结束行号。

更多详情请访问http://techpurohit.in/list-some-useful-git-commands.

其他回答

将此别名添加到.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

使用如下命令:

> git lg
> git lg -- filename

输出看起来几乎与gitk输出完全相同。享受

阅读并播放一点后,总结其他答案:

通常的命令行命令是

git log --follow --all -p dir/file.c

但是,您也可以使用gitk(GUI)或tig(文本UI)来提供更多人类可读的方式来查看它。

gitk --follow --all -p dir/file.c

tig --follow --all -p dir/file.c

在Debian/Uubuntu下,这些可爱工具的安装命令如下所示:

sudo apt-get install gitk tig

我目前正在使用:

alias gdf='gitk --follow --all -p'

这样我就可以键入gdf-dir来获取子目录dir中所有内容的集中历史。

如果您将Eclipse与Git插件一起使用,那么它具有与历史的极好的比较视图。右键单击文件并选择“比较”→ “历史”。

如果您使用TortoiseGit,您应该能够右键单击该文件并执行TortoiseGit-->显示日志。在弹出的窗口中,确保:

未选中“显示整个项目”选项。选中“所有分支”选项。

如何在meld中以图形方式手动查看文件的更改历史

如果您只想查看哪个提交更改了文件,那么可以对它们执行gitdifftool,以图形方式查看meld的更改(如我在这里所解释的),请改为执行以下操作:

git log --follow --oneline -- path/to/file.c

样本运行和输出:

eRCaGuy_hello_world$ git log --follow --oneline -- c/alsa_aplay__play_tone_sound_WIP.c
04b67fb (HEAD -> master) Update c/alsa_aplay__play_tone_sound_WIP.c
301122a wip: alsa_aplay__play_tone_sound.c: finish initial version of program
d4e8092 wip: add c/alsa_aplay__play_tone_sound.c

现在我可以像这样以图形方式查看meld中的最后更改(从上面的输出中提取提交散列)。

请注意,我有意省略了文件名,以便它可以在文件重命名后自动为我正确跟踪文件,我知道这些提交可能只编辑了这个文件:

# just the last change
git difftool 04b67fb~..04b67fb

# and the one before that
git difftool 301122a~..301122a

# ...and before that
git difftool d4e8092~..d4e8092

如果需要指定文件名,只需使用:

git difftool 04b67fb~..04b67fb -- path/to/file.c