git 如何列出文件的所有不同版本,并对其进行区分?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1964142/
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
how can I list all the different versions of a file, and diff them also?
提问by mrblah
using git, I want to list all the different revisions of a given file.
使用 git,我想列出给定文件的所有不同版本。
Then, I want to choose a particular version and compare it when another.
然后,我想选择一个特定的版本并将其与另一个版本进行比较。
How can I do this?
我怎样才能做到这一点?
回答by daf
To show a history of changes to a particular file, you can use git log
:
要显示对特定文件的更改历史记录,您可以使用git log
:
git log -p -- path/to/file
The -p
tells it to show the diff between each revision and its parent. To get a cumulative diff between two revisions, take the ID of the two revisions, and pass them to git diff
:
该-p
告知将显示每个修订与其父之间的差异。要获得两个修订版之间的累积差异,请获取两个修订版的 ID,并将它们传递给git diff
:
git diff abc123 def456 -- path/to/file.
Where abc123
and def456
are the revision IDs.
修订 ID在哪里abc123
和def456
在哪里。
回答by Brad Parks
You can use a script like this to dump all the versions of a file to a separate file:
您可以使用这样的脚本将文件的所有版本转储到单独的文件中:
e.g.
例如
git_all_versions_of path/to/somefile.txt
It will generate a bunch of files in the same folder as the original file, named like the following, with the most recent change postfixed with "1". Note that it will also dump another file ending in .logmsg
that has the log message of the commit as well.
它将在与原始文件相同的文件夹中生成一堆文件,命名如下,最近的更改后缀为“1”。请注意,它还会转储另一个.logmsg
以提交日志消息结尾的文件。
path/to/somefile.txt.1.0dea419
path/to/somefile.txt.1.0dea419.logmsg
path/to/somefile.txt.2.cdea8s9
path/to/somefile.txt.2.cdea8s9.logmsg
path/to/somefile.txt.3.fdsf2d
path/to/somefile.txt.3.fdsf2d.logmsg
etc...
After I've dumped all the files, I just run a grep -r DELETED_METHOD_NAME somefile.txt.*
to find what I'm looking for.
转储所有文件后,我只需运行 agrep -r DELETED_METHOD_NAME somefile.txt.*
即可找到我要查找的内容。
git_all_versions_of
git_all_versions_of
#!/bin/sh
if [ "$#" -ne 1 ] || [ "" == "help" ]
then
echo "dump all git versions of a file to separate files"
echo
echo "usage: gitk --all -- path/to/file
FILENAME";
echo
echo "e.g."
echo
echo "$ ##代码## path/to/somefile.txt"
echo
echo "path/to/somefile.txt.1.0dea419"
echo "path/to/somefile.txt.1.0dea419.logmsg"
echo "path/to/somefile.txt.2.cdea8s9"
echo "path/to/somefile.txt.2.cdea8s9.logmsg"
echo "path/to/somefile.txt.3.fdsf2d"
echo "path/to/somefile.txt.3.fdsf2d.logmsg"
echo "..."
exit 1
fi
index=1
for commit in $(git log --pretty=format:%h "")
do
padindex=$(printf %03d "$index")
out=".$padindex.$commit"
log="$out.logmsg"
echo "saving version $index to file $out for commit $commit"
echo "*******************************************************" > "$log"
git log -1 --pretty=format:"%s%nAuthored by %an at %ai%n%n%b%n" "$commit" >> "$log"
echo "*******************************************************" >> "$log"
git show "$commit:./" > "$out"
let index++
done
回答by hagello
Use the standard GUI tool of git:
使用 git 的标准 GUI 工具:
##代码##It displays the version history stripped down to the commits that affect path/to/file
.
它显示了剥离到影响path/to/file
.
As display mode for the comparison between the current (new) version and the preceding (old) version you can choose:
作为当前(新)版本与上一个(旧)版本比较的显示方式,您可以选择:
- old versionor
- new versionor
- diff
- 旧版本或
- 新版本或
- 差异
Additionally, you can select a commit and then right click another commit and choose Diff this -> selectedin the context menu. Everything that you wished for.
此外,您可以选择一个提交,然后右键单击另一个提交并在上下文菜单中选择Diff this -> selected。你想要的一切。
回答by RyanWilcox
I wrote a tool that would get you most of the way there (printing out the entire contents of a file, as it was in SHA-1-WHATEVER.
我编写了一个工具,可以让您完成大部分工作(打印出文件的全部内容,就像在 SHA-1-WHATEVER 中一样。
You could either put a little shell script over that to do everything automatically, or the README.markdown file in that repository also gives references to where I learned all the stuff I needed to write the command.
您可以在上面放一个小 shell 脚本来自动执行所有操作,或者该存储库中的 README.markdown 文件还提供了我在何处学习编写命令所需的所有内容的参考。