是否有一个快速的 Git 命令来查看文件的旧版本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/338436/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:00:07  来源:igfitidea点击:

Is there a quick Git command to see an old version of a file?

gitgit-commandsgit-history-graph

提问by mike

Is there a command in Git to see (either dumped to stdout, or in $PAGERor $EDITOR) a particular version of a particular file?

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

回答by mipadi

You can use git showwith a path from the root of the repository (./or ../for relative pathing):

您可以使用git show来自存储库根目录的路径(./../用于相对路径):

$ git show REVISION:path/to/file

Replace REVISIONwith your actual revision (could be a Git commit SHA, a tag name, a branch name, a relative commit name, or any other way of identifying a commit in Git)

替换REVISION为您的实际修订(可以是 Git 提交 SHA、标签名称、分支名称、相对提交名称或任何其他在 Git 中标识提交的方式)

For example, to view the version of file <repository-root>/src/main.cfrom 4 commits ago, use:

例如,要查看<repository-root>/src/main.c4 次提交前的文件版本,请使用:

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

Git for Windows requires forward slasheseven in paths relative to the current directory. For more information, check out the man page for git-show.

即使在相对于当前目录的路径中,Windows 版 Git 也需要正斜杠。有关更多信息,请查看 的手册页git-show

回答by Jim Hunziker

Doing this by date looks like this:

按日期执行此操作如下所示:

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

Note that HEAD@{2013-02-25}means "where HEAD was on 2013-02-25" in this repository (using the reflog), not "the last commit before 2013-02-25 in this branch in history".

请注意,这HEAD@{2013-02-25}意味着此存储库中的“HEAD 在 2013-02-25 上的位置”(使用reflog),而不是“历史上此分支中 2013-02-25 之前的最后一次提交”。

回答by Trausti Kristjansson

If you like GUIs, you can use gitk:

如果您喜欢 GUI,可以使用 gitk:

  1. start gitk with:

    gitk /path/to/file
    
  2. Choose the revision in the top part of the screen, e.g. by description or date. By default, the lower part of the screen shows the diff for that revision, (corresponding to the "patch" radio button).

  3. To see the file for the selected revision:

    • Click on the "tree" radio button. This will show the root of the file tree at that revision.
    • Drill down to your file.
  1. 用以下命令启动 gitk:

    gitk /path/to/file
    
  2. 在屏幕顶部选择修订版本,例如按说明或日期。默认情况下,屏幕的下部显示该版本的差异(对应于“补丁”单选按钮)。

  3. 要查看所选修订的文件:

    • 单击“树”单选按钮。这将显示该版本的文件树的根。
    • 深入到您的文件。

回答by Adrien Be

You can also specify a commit hash(often also called commit ID) with the git showcommand.

您还可以使用命令指定 a commit hash(通常也称为commit ID)。git show



In a nutshell

简而言之

git show <commitHash>:/path/to/file

git show <commitHash>:/path/to/file



Step by step

一步步

  1. Show the log of all the changes for a given file with git log /path/to/file
  2. In the list of changes shown, it shows the commit hashsuch as commit 06c98...(06c98... being the commit hash)
  3. Copy the commit hash
  4. Run the command git show <commitHash>:/path/to/fileusing the commit hashof step 3 & the path/to/fileof step 1.
  1. 显示给定文件的所有更改的日志 git log /path/to/file
  2. 在显示的更改列表中,它显示commit hash诸如commit 06c98...(06c98... 是提交哈希)
  3. 复制 commit hash
  4. git show <commitHash>:/path/to/file使用commit hash第 3 步和path/to/file第 1步的命令运行命令。

Note:adding the ./when specifying a relative path seems important, i.e. git show b2f8be577166577c59b55e11cfff1404baf63a84:./flight-simulation/src/main/components/nav-horiz.html.

注意:./在指定相对路径时添加似乎很重要,即git show b2f8be577166577c59b55e11cfff1404baf63a84:./flight-simulation/src/main/components/nav-horiz.html.

回答by Ijas Ameenudeen

In addition to Jim Hunziker's answer,

除了Jim Hunziker的回答,

you can export the file from the revision as,

您可以从修订版导出文件,

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

Hope this helps :)

希望这可以帮助 :)

回答by sachin_ur

To quickly see the differences with older revisions of a file:

要快速查看与旧版本文件的差异:

git show -1 filename.txt> to compare against the last revision of file

git show -2 filename.txt> to compare against the 2nd last revision

git show -3 fielname.txt> to compare against the last 3rd last revision

git show -1 filename.txt> 与文件的最后修订版进行比较

git show -2 filename.txt> 与第 2 次修订进行比较

git show -3 fielname.txt> 与最近的第三次修订进行比较

回答by sanbor

git log -pwill show you not just the commit logs but also the diff of each commit (except merge commits). Then you can press /, enter filename and press enter. Press nor pto go to the next/previous occurrence. This way you will not just see the changes in the file but also the commit information.

git log -p不仅会显示提交日志,还会显示每个提交的差异(合并提交除外)。然后您可以按/,输入文件名并按enter。按np转到下一个/上一个事件。这样,您不仅会看到文件中的更改,还会看到提交信息。

回答by Brad Parks

You can use a script like this to dump all the versions of a file to separate files:

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

e.g.

例如

git_dump_all_versions_of_a_file.sh path/to/somefile.txt

Get the script hereas an answer to another similar question

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

回答by Andrew

WAY 1:

方式一:

  1. Find commit idwith: git reflog
  2. make hard reset to this commit: git reset --hard %commit ID%
  1. 使用以下命令查找提交 IDgit reflog
  2. 对此提交进行硬重置: git reset --hard %commit ID%

git reset --hard c14809fa

git reset --hard c14809fa

  1. make any unnecessary change and do fresh commit into needed branch :)
  1. 进行任何不必要的更改并重新提交到所需的分支:)

WAY 2:(I prefer this way)

方式2:(我更喜欢这种方式)

  1. Find commit idwith: git reflog
  2. List files from commit git diff-tree --no-commit-id --name-only -r <commitHash>
  1. 使用以下命令查找提交 IDgit reflog
  2. 列出提交中的文件 git diff-tree --no-commit-id --name-only -r <commitHash>

example: git diff-tree --no-commit-id --name-only -r d2f9ba4//"d2f9ba4" is commit id from "1."

示例:git diff-tree --no-commit-id --name-only -r d2f9ba4//“d2f9ba4”是来自“1”的提交ID。

  1. Open needed file with command:
  1. 使用命令打开需要的文件:

git show <commitHash>:/path/to/file

git show <commitHash>:/path/to/file

example: git show d2f9ba4:Src/Ext/MoreSwiftUI/ListCustom.swift// "Src/..." is file path from "2."

示例:git show d2f9ba4:Src/Ext/MoreSwiftUI/ListCustom.swift// "Src/..." 是来自 "2." 的文件路径。