使用Git版本控制查看文件的更改历史记录

Ric*_*ard 2920 git git-log

如何在Git中查看单个文件的更改历史记录,完整的详细信息?

我有:

git log -- [filename]
Run Code Online (Sandbox Code Playgroud)

它显示了文件的提交历史记录,但是如何获取每个文件更改的内容?

我正试图从MS SourceSafe过渡到过去那么简单right-clickshow history.

Cla*_*esi 2257

为此,我会使用:

gitk [filename]
Run Code Online (Sandbox Code Playgroud)

或者按照文件名过去重命名

gitk --follow [filename]
Run Code Online (Sandbox Code Playgroud)

  • 我也在寻找以前重命名过的文件的历史记录,并首先找到了这个帖子.解决方案是使用"git log --follow <filename>",如Phil指出的[这里](http://stackoverflow.com/questions/1430749/getting-git-to-acknowledge-previously-moved-files). (135认同)
  • 作者正在寻找命令行工具.虽然gitk带有GIT,但它既不是命令行应用程序,也不是特别好的GUI. (111认同)
  • 他在寻找命令行工具吗?"右击 - >显示历史"当然不暗示它. (68认同)
  • 但我甚至还有一个工具将上面的内容与'git blame'相结合,允许我随着时间的推移浏览文件的来源...... (27认同)
  • 不幸的是,这并不遵循过去重命名的文件的历史记录. (25认同)
  • `gitk`不是基本git包的一部分.您需要下载tk包才能使用gitk.@ VolkA的答案更好,`git log -p filename`无任何依赖性. (19认同)
  • 除了git之外没有寻找其他任何东西,这不应该是公认的答案 (5认同)
  • 注意Windows用户...文件名路径区分大小写 (4认同)
  • 问题是"使用Git版本查看文件的更改历史记录".这正是gitk所做的.它查找git提交历史记录以进行更改.我不在乎它是否是基本git包的一部分,只要它以快速而简单的方式完成工作. (4认同)
  • 不错的提示.在我的机器(git v1.7.10)中,我不得不使用`gitk - [filename]`. (3认同)
  • @DanMoulding:`gitk --follow [filename]`显示过去重命名的文件的历史记录.但是,它不会在重命名之前显示文件内容的更改.请参阅[this](/sf/ask/19473471/#answer-13730108)答案. (3认同)
  • GitWeb就是答案. (2认同)
  • @HenriquedeSousa:因为提问者认为这个答案对他来说是最有帮助的*. (2认同)
  • 由于使用了其他工具而无法投票,最好使用答案/sf/answers/19476971/中的标准cli (2认同)
  • 可接受的答案应考虑到并非每个人的系统上都安装了gitk。如果我在仅CLI的环境中,将没有可用的GUI。OP要求使用“ git”命令。使用gitweb,gitk或其他与说“只需转到GitHub并在此处检查历史记录”相同的方法。 (2认同)
  • 什么是“gitk”? (2认同)
  • 如果该答案建议使用第三方实用程序,为什么它会被标记为正确?正确答案如下(@DanMoulding 是作者) (2认同)

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)

以可视方式浏览更改.

  • git show HEAD显示所有文件,你知道如何跟踪单个文件(正如理查德所要求的那样)? (6认同)
  • 加上gitk看起来像是由布吉怪物建造的.这是一个很好的答案,最适合原始问题. (5认同)
  • 你使用:git show <revision> - filename,它将显示该修订的差异,如果存在的话. (4认同)
  • --stat也很有帮助.您可以将它与-p一起使用. (3认同)
  • 这很棒.指定不再存在的路径时,gitk表现不佳.我用git log -p-path. (3认同)
  • `-p` = `生成补丁` = 显示 **diff** 除了提交 **描述** (2认同)

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选项可确保每次更改都包含差异.

  • @Benjohn`--`选项告诉Git它已经到达选项的末尾,并且` - `后面的任何内容都应该被视为参数.对于`git log`,如果你的路径名以***开头,那么这只会有所不同.假设您想知道具有不幸名称"--follow"的文件的历史记录:`git log --follow -p - - follow` (38认同)
  • 我同意这是真实的答案.(1.)`--follow`确保您看到文件重命名(2.)` - p`确保您看到文件如何更改(3.)它只是命令行. (20认同)
  • --stat也很有帮助.您可以将它与-p一起使用. (16认同)
  • @Benjohn:通常,` - `很有用,因为它还可以防止任何与您输入的文件名匹配的`revision`名称,这实际上可能是可怕的.例如:如果你有一个分支和一个名为`foo`的文件,`git log -p foo`将显示git日志历史记录到`foo`,而不是*file*`foo`的历史记录.但@DanMoulding是正确的,因为`--follow`命令只接受一个文件名作为其参数,因此它不太必要,因为它不能是`revision`.我刚学会了这个.也许你把它从你的答案中删除是对的; 我不确定. (9认同)
  • @NHDaly我注意到` - `已被添加,但我不知道为什么这样做最好?它有什么作用? (3认同)
  • 在你的 `file` 前加一个 `--`,这绝对是最好的答案! (2认同)
  • 在 [git log](https://git-scm.com/docs/git-log) 的长页文档中,必须在“使用 -p 生成补丁”一节之前阅读多远真是太疯狂了。这个答案很好地解释了`--follow`,解释`-p`也很有用。 (2认同)

Fal*_*ken 167

如果您希望保持基于文本,则可能需要使用tig.

快速安装:

  • apt-get:# apt-get install tig
  • 自制软件(OS X):$ brew install tig

使用它来查看单个文件的历史记录:tig [filename]
或浏览详细的回购历史记录:tig

类似gitk但基于文本.支持终端颜色!

  • 优秀的基于文本的工具,很棒的答案.当我在无头服务器上看到gitk安装的依赖关系时,我吓坏了.将再次投票A +++ (22认同)

far*_*nix 109

git whatchanged -p filenamegit log -p filename在这种情况下也相当于.

您还可以查看文件中特定代码行的更改时间git blame filename.这将为文件中的每一行打印出一个简短的提交ID,作者,时间戳和完整的代码行.在您发现错误并且想知道它何时被引入(或者它是谁的错误)之后,这非常有用.

  • "鼓励新用户使用git-log.(...)该命令主要是出于历史原因;" (6认同)
  • +1,但````在命令`git blame filename`中不是可选的. (4认同)

Mar*_*Fox 99

SourceTree用户

如果您使用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.

  • 我认为这是一个很好的答案.也许你也没有得到投票,因为你回答其他方式(恕我直言更好)看到的变化,即通过gitk和tig除了git. (2认同)
  • `--all`适用于所有分支,其余部分在@ Dan的答案中解释 (2认同)

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输出几乎完全相同.请享用.

  • 也可以使用`git lg -p filename` - 它返回搜索文件的漂亮差异. (3认同)

lan*_*ng2 20

最近我发现tig并发现它非常有用.在某些情况下,我希望它能做到A或B,但大部分时间它都很整洁.

对于您的情况,tig <filename>可能是您正在寻找的.

http://jonas.nitro.dk/tig/


Jia*_*ian 15

我为这个目的编写了git-playback

pip install git-playback
git playback [filename]
Run Code Online (Sandbox Code Playgroud)

这样既可以在命令行中显示结果(例如git log -p),也可以使用箭头键(例如gitk)逐步执行每次提交.


Geo*_*son 13

要么:

gitx -- <path/to/filename>

如果你正在使用gitx

  • 出于某种原因,我的 gitx 打开空白。 (2认同)

小智 10

Sourcetree UI 中,您可以通过在右键单击上下文菜单中选择“Log Selected”选项来查找文件的历史记录:

在此处输入图片说明

它将显示所有提交的历史记录。


Adi*_*vit 9

如果要查看文件的整个历史记录,包括所有其他分支上使用:

gitk --all <filename>
Run Code Online (Sandbox Code Playgroud)


cor*_*ori 7

如果您在Repository菜单下使用git GUI(在Windows上),则可以使用"Visualize master's History".突出显示顶部窗格中的提交和右下角的文件,您将在左下方看到该提交的差异.

  • 这如何回答这个问题? (3认同)
  • 好吧,OP没有指定命令行,并且从SourceSafe(这是一个GUI)移动它似乎相关指出你可以做几乎与你在Windows上的Git GUI中的VSS中可以做的事情. (3认同)

Phi*_*Lho 7

使用优秀的Git Extensions,您可以访问文件仍然存在的历史记录中的一个点(如果它已被删除,否则只需转到HEAD),切换到File tree选项卡,右键单击该文件并选择File history.

默认情况下,它通过重命名跟随文件,Blame选项卡允许查看给定修订的名称.

它有一些小问题,比如fatal: Not a valid object nameView单击删除修订时在选项卡中显示,但我可以忍受.:-)

  • @EvanHahn不准确,通过mono也可以在Linux上使用GitExtension,我们在ubuntu上使用它并且非常高兴它.见http://git-extensions-documentation.readthedocs.org/en/latest/getting_started.html (3认同)

fox*_*ris 7

您可以将vscode与GitLens一起使用,这是一个非常强大的工具。安装GitLens后,转到GitLens选项卡,选择FILE HISTORY即可浏览。

在此处输入图片说明


Eng*_*hly 7

要获取特定文件的所有提交,请使用以下命令:

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

查看 gif 图片git 提交特定文件


jit*_*hit 6

您也可以尝试使用它列出更改了文件特定部分的提交(在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


Ant*_*ška 6

SmartGit

  1. 在菜单中启用以显示未更改的文件:查看/显示未更改的文件
  2. 右键单击该文件,然后选择“日志”或按“ Ctrl-L”


小智 5

我要找的答案不在这里。这是为了查看我为提交而暂存的文件中的更改。IE,

git diff --cached
Run Code Online (Sandbox Code Playgroud)


use*_*927 5

如果您使用 TortoiseGit,您应该可以右键单击该文件并执行TortoiseGit --> Show Log. 在弹出的窗口中,确保:

  • ' Show Whole Project' 选项未选中。

  • ' All Branches' 选项被选中。


Gab*_*les 5

这是我的偏好:在 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)

[推荐] 如何在 meld 中自动查看更改,一次提交一个,使用git difft

  1. meldgit difftool按照我的指示安装为您的。

  2. 从我的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

  3. 用法:

    # 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)