是否有一个快速的 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
Is there a quick Git command to see an old version of a file?
提问by mike
Is there a command in Git to see (either dumped to stdout, or in $PAGER
or $EDITOR
) a particular version of a particular file?
Git 中是否有命令可以查看(转储到 stdout,或在$PAGER
或 中$EDITOR
)特定文件的特定版本?
回答by mipadi
You can use git show
with a path from the root of the repository (./
or ../
for relative pathing):
您可以使用git show
来自存储库根目录的路径(./
或../
用于相对路径):
$ git show REVISION:path/to/file
Replace REVISION
with 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.c
from 4 commits ago, use:
例如,要查看<repository-root>/src/main.c
4 次提交前的文件版本,请使用:
$ 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:
start gitk with:
gitk /path/to/file
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).
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.
用以下命令启动 gitk:
gitk /path/to/file
在屏幕顶部选择修订版本,例如按说明或日期。默认情况下,屏幕的下部显示该版本的差异(对应于“补丁”单选按钮)。
要查看所选修订的文件:
- 单击“树”单选按钮。这将显示该版本的文件树的根。
- 深入到您的文件。
回答by Adrien Be
You can also specify a commit hash
(often also called commit ID
) with the git show
command.
您还可以使用命令指定 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
一步步
- Show the log of all the changes for a given file with
git log /path/to/file
- In the list of changes shown, it shows the
commit hash
such ascommit 06c98...
(06c98... being the commit hash) - Copy the
commit hash
- Run the command
git show <commitHash>:/path/to/file
using thecommit hash
of step 3 & thepath/to/file
of step 1.
- 显示给定文件的所有更改的日志
git log /path/to/file
- 在显示的更改列表中,它显示
commit hash
诸如commit 06c98...
(06c98... 是提交哈希) - 复制
commit hash
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 -p
will 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 n
or p
to 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
。按n
或p
转到下一个/上一个事件。这样,您不仅会看到文件中的更改,还会看到提交信息。
回答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:
方式一:
- Find commit idwith:
git reflog
- make hard reset to this commit:
git reset --hard %commit ID%
- 使用以下命令查找提交 ID:
git reflog
- 对此提交进行硬重置:
git reset --hard %commit ID%
git reset --hard c14809fa
git reset --hard c14809fa
- make any unnecessary change and do fresh commit into needed branch :)
- 进行任何不必要的更改并重新提交到所需的分支:)
WAY 2:(I prefer this way)
方式2:(我更喜欢这种方式)
- Find commit idwith:
git reflog
- List files from commit
git diff-tree --no-commit-id --name-only -r <commitHash>
- 使用以下命令查找提交 ID:
git reflog
- 列出提交中的文件
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。
- Open needed file with command:
- 使用命令打开需要的文件:
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." 的文件路径。