Git中是否有命令可以查看(转储到stdout,或在$PAGER或$EDITOR中)特定文件的特定版本?


当前回答

您可以使用git show和存储库根目录的路径(./或../表示相对路径):

$ git show REVISION:path/to/file

用实际版本替换REVISION(可以是Git提交SHA、标记名称、分支名称、相对提交名称或任何其他标识Git中提交的方式)

例如,要查看4次提交之前的文件<repositoryroot>/src/main.c版本,请使用:

$ git show HEAD~4:src/main.c

Git for Windows甚至要求在相对于当前目录的路径中使用正斜杠。有关更多信息,请查看gitshow的手册页。

其他回答

要快速查看与文件旧版本的差异,请执行以下操作:

git show-1 filename.txt>以与文件的最新版本进行比较git show-2 filename.txt>与第二个最新版本进行比较git show-3 fielname.txt>与上一个第三个最新版本进行比较

帮助程序从给定修订中获取多个文件

在尝试解决合并冲突时,此助手非常有用:

#!/usr/bin/env python3

import argparse
import os
import subprocess

parser = argparse.ArgumentParser()
parser.add_argument('revision')
parser.add_argument('files', nargs='+')
args = parser.parse_args()
toplevel = subprocess.check_output(['git', 'rev-parse', '--show-toplevel']).rstrip().decode()
for path in args.files:
    file_relative = os.path.relpath(os.path.abspath(path), toplevel)
    base, ext = os.path.splitext(path)
    new_path = base + '.old' + ext
    with open(new_path, 'w') as f:
        subprocess.call(['git', 'show', '{}:./{}'.format(args.revision, path)], stdout=f)

GitHub上游。

用法:

git-show-save other-branch file1.c path/to/file2.cpp

结果:以下包含文件的备用版本:

file1.old.c
path/to/file2.old.cpp

这样,您就保留了文件扩展名,这样您的编辑就不会抱怨,并且可以轻松地找到新文件旁边的旧文件。

您可以使用这样的脚本将文件的所有版本转储到单独的文件中:

e.g.

git_dump_all_versions_of_a_file.sh path/to/somefile.txt

在此处获取脚本,作为另一个类似问题的答案

您可以使用git show和存储库根目录的路径(./或../表示相对路径):

$ git show REVISION:path/to/file

用实际版本替换REVISION(可以是Git提交SHA、标记名称、分支名称、相对提交名称或任何其他标识Git中提交的方式)

例如,要查看4次提交之前的文件<repositoryroot>/src/main.c版本,请使用:

$ git show HEAD~4:src/main.c

Git for Windows甚至要求在相对于当前目录的路径中使用正斜杠。有关更多信息,请查看gitshow的手册页。

除了吉姆·亨齐克的回答,

可以将修订中的文件导出为,

git show HEAD@{2013-02-25}:./fileInCurrentDirectory.txt > old_fileInCurrentDirectory.txt

希望这有帮助:)