如何在Git中查看单个文件的更改历史记录,完整的详细信息?
我有:
git log -- [filename]
Run Code Online (Sandbox Code Playgroud)
它显示了文件的提交历史记录,但是如何获取每个文件更改的内容?
我正试图从MS SourceSafe过渡到过去那么简单right-click
→ show history
.
Cla*_*esi 2257
为此,我会使用:
gitk [filename]
Run Code Online (Sandbox Code Playgroud)
或者按照文件名过去重命名
gitk --follow [filename]
Run Code Online (Sandbox Code Playgroud)
Vol*_*lkA 2112
您可以使用
git log -p filename
Run Code Online (Sandbox Code Playgroud)
让git为每个日志条目生成补丁.
看到
git help log
Run Code Online (Sandbox Code Playgroud)
对于更多选项 - 它实际上可以做很多好事:)为了获得特定提交的差异你可以
git show HEAD
Run Code Online (Sandbox Code Playgroud)
或标识符的任何其他修订.或者使用
gitk
Run Code Online (Sandbox Code Playgroud)
以可视方式浏览更改.
Dan*_*ing 1413
git log --follow -p -- path-to-file
这将显示文件的整个历史记录(包括重命名以外的历史记录以及每次更改的差异).
换句话说,如果命名的文件bar
曾被命名foo
,那么git log -p bar
(没有--follow
选项)将只显示文件的历史记录,直到它被重命名为止 - 当它被称为时,它不会显示文件的历史记录foo
.使用git log --follow -p bar
将显示文件的整个历史记录,包括文件的所有更改foo
.该-p
选项可确保每次更改都包含差异.
far*_*nix 109
git whatchanged -p filename
git log -p filename
在这种情况下也相当于.
您还可以查看文件中特定代码行的更改时间git blame filename
.这将为文件中的每一行打印出一个简短的提交ID,作者,时间戳和完整的代码行.在您发现错误并且想知道它何时被引入(或者它是谁的错误)之后,这非常有用.
Mar*_*Fox 99
如果您使用SourceTree可视化您的存储库(它是免费且非常好的),您可以右键单击文件并选择Log Selected
显示屏(下方)比gitk更友好,列出的大多数其他选项.不幸的是(此时)没有简单的方法从命令行启动此视图 - SourceTree的CLI目前只打开repos.
小智 62
要显示哪个修订版本和作者上次修改了文件的每一行:
git blame filename
Run Code Online (Sandbox Code Playgroud)
或者如果你想使用强大的blame GUI:
git gui blame filename
Run Code Online (Sandbox Code Playgroud)
Joh*_*den 47
通过阅读并播放后的其他答案摘要:
通常的命令行命令是
git log --follow --all -p dir/file.c
Run Code Online (Sandbox Code Playgroud)
但是你也可以使用gitk(gui)或tig(text-ui)来提供更加人性化的方式来查看它.
gitk --follow --all -p dir/file.c
tig --follow --all -p dir/file.c
Run Code Online (Sandbox Code Playgroud)
在debian/ubuntu下,这些可爱工具的安装命令如预期:
sudo apt-get install gitk tig
Run Code Online (Sandbox Code Playgroud)
而我目前正在使用:
alias gdf='gitk --follow --all -p'
Run Code Online (Sandbox Code Playgroud)
这样我就可以输入gdf dir
以获取子目录中所有内容的焦点历史记录dir
.
Pal*_*esz 24
将此别名添加到.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
Run Code Online (Sandbox Code Playgroud)
并使用这样的命令:
> git lg
> git lg -- filename
Run Code Online (Sandbox Code Playgroud)
输出看起来与gitk输出几乎完全相同.请享用.
Jia*_*ian 15
我为这个目的编写了git-playback
pip install git-playback
git playback [filename]
Run Code Online (Sandbox Code Playgroud)
这样既可以在命令行中显示结果(例如git log -p
),也可以使用箭头键(例如gitk
)逐步执行每次提交.
如果您在Repository菜单下使用git GUI(在Windows上),则可以使用"Visualize master's History".突出显示顶部窗格中的提交和右下角的文件,您将在左下方看到该提交的差异.
使用优秀的Git Extensions,您可以访问文件仍然存在的历史记录中的一个点(如果它已被删除,否则只需转到HEAD),切换到File tree
选项卡,右键单击该文件并选择File history
.
默认情况下,它通过重命名跟随文件,Blame
选项卡允许查看给定修订的名称.
它有一些小问题,比如fatal: Not a valid object name
在View
单击删除修订时在选项卡中显示,但我可以忍受.:-)
要获取特定文件的所有提交,请使用以下命令:
git rev-list HEAD --oneline FileName
例如
git rev-list HEAD --oneline index.html
输出
7a2bb2f update_index_with_alias
6c03e56 update_changes
e867142 Revert "add_paragraph"
Run Code Online (Sandbox Code Playgroud)
如果您想查看对文件所做的更改
git log -p fileName
您也可以尝试使用它列出更改了文件特定部分的提交(在Git 1.8.4中实现)。
返回的结果将是修改此特定部分的提交列表。命令:
git log --pretty=short -u -L <upperLimit>,<lowerLimit>:<path_to_filename>
Run Code Online (Sandbox Code Playgroud)
其中upperLimit是文件的起始行号,lowerLimit是文件的结束行号。
有关更多详细信息,请访问https://www.techpurohit.com/list-some-useful-git-commands
如果您使用 TortoiseGit,您应该可以右键单击该文件并执行TortoiseGit --> Show Log
. 在弹出的窗口中,确保:
' Show Whole Project
' 选项未选中。
' All Branches
' 选项被选中。
这是我的偏好:在 meld 中直观地查看更改历史记录,一次一个提交,时间倒退,从 commit 开始commit
:
# Option 1: for all files and folders
git difft commit
# Option 2: just for the specified files and folders
git difft commit -- path/to/file.c path/to/folder/
Run Code Online (Sandbox Code Playgroud)
我写git difft
。安装说明如下。
meld
如果您只想查看哪些提交更改了文件,以便您可以git difftool
对它们进行图形化查看更改meld
(正如我在此处解释的那样),请改为执行以下操作:
git log --follow --oneline -- path/to/file.c
Run Code Online (Sandbox Code Playgroud)
示例运行和输出:
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
Run Code Online (Sandbox Code Playgroud)
现在我可以像这样以图形方式查看最后的更改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
Run Code Online (Sandbox Code Playgroud)
如果您需要指定文件名,只需这样做:
git difftool 04b67fb~..04b67fb -- path/to/file.c
Run Code Online (Sandbox Code Playgroud)
git difft
meld
git difftool
按照我的指示安装为您的。
从我的eRCaGuy_dotfiles存储库安装我的git difft
包装器。
在 Linux 中,它在终端中运行。在 Windows 中,它在 Git for Windows 附带的 Git Bash 终端中运行。
git diffn
根据我的安装说明修改后的安装说明是:
mkdir -p ~/bin
cd ~/bin
curl -LO https://raw.githubusercontent.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/master/useful_scripts/git-difft.sh
chmod +x git-diffn.sh
mv git-diffn.sh git-diffn
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
. ~/.bashrc
Run Code Online (Sandbox Code Playgroud)
另请参阅文件顶部的说明git-difft.sh
。
用法:
# Look at changes from just one commit at at time, going backwards from the
# current commit
git difft
# Look at changes starting from commit `commit`
git difft commit
Run Code Online (Sandbox Code Playgroud)
您还可以指定一组要跟踪的文件或文件夹,如下所示:
git difft commit -- path/to/file.c path/to/folder/
git difft commit -- path/to/file1 path/to/file2 path/to/file3
git difft -- path/to/some/dir
# etc/
Run Code Online (Sandbox Code Playgroud)
按Ctrl+ C,然后Enter跳过当前提交并及时返回到下一个提交。
或者,按Ctrl+C两次退出程序。
这是完整的帮助菜单,如下所示git difft -h
:
'git-difft' version 0.2.0
Iterate through all commits going backwards from HEAD to the first commit, one commit at a time,
running 'git difftool' on each one to see the changes it introduced.
- Press Ctrl + C once, then Enter, to break out of the current 'git difftool' command, but continue
on with the previous commit.
- Press Ctrl + C twice to exit out of the whole program.
USAGE:
git-difft [OPTIONS] [[commit] [commit_start~..commit_end]] -- [file1 file2 file3 ...]
OPTIONS
-h, -?
Print help menu
-v, --version
Print version information.
--
Lists of files or directories go after this point.
EXAMPLE USAGES:
git-difft -h
Print help menu.
git-difft
Start running 'git difftool' on the commit starting at HEAD (the currently-checked-out
commit).
git-difft HEAD
Same as above.
git-difft HEAD~
Start running 'git difftool' on the commit starting at HEAD~ (one before HEAD).
git-difft HEAD~2
Start running 'git difftool' on the commit starting at HEAD~2 (two before HEAD).
git-difft abcdefg
Start running 'git difftool' on commit hash abcdefg.
git-difft my_branch
Start running 'git difftool' on the commit at the tip of branch 'my_branch'.
git-difft commit1~..commit2
Run 'git difftool' on all commits between commit1 and commit2, inclusive.
git-difft commit1..commit2
Run 'git difftool' on all commits between commit1 and commit2, including commit2 but
NOT including commit1.
git-difft commit1~..commit2 -- file1 file2 file3
Run 'git difftool' on all commits between commit1 and commit2, inclusive, but only
for the files file1, file2, and file3.
git-difft -- path/to/file1
Start running 'git difftool' on the commit starting at HEAD, but only for the file
"path/to/file1".
git-difft -- path/to/dir1
Start running 'git difftool' on the commit starting at HEAD, but only for the files
in the directory "path/to/dir1/".
This program is part of eRCaGuy_dotfiles: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles
by Gabriel Staples.
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1557462 次 |
最近记录: |