使用 git 查看对特定文件的更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8048584/
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
See changes to a specific file using git
提问by Mellon
I know that I can use the git diff
command to check the changes, but, as far as I understood, it is directory based. This means it gives all the changes of all files on the current directory.
我知道我可以使用该git diff
命令来检查更改,但据我所知,它是基于目录的。这意味着它给出了当前目录中所有文件的所有更改。
How can I check only the changes in one specific file?Say, I have changed files file_1.rb
, file_2.rb
, ..., file_N.rb
, but I am only interested in the changes in the file file_2.rb
. How do I check these changes then (before I commit)?
如何仅检查一个特定文件中的更改?比如说,我已经更改了文件file_1.rb
, file_2.rb
, ..., file_N.rb
,但我只对文件中的更改感兴趣file_2.rb
。我如何检查这些更改(在我提交之前)?
回答by Greg Hewgill
Use a command like:
使用如下命令:
git diff file_2.rb
See the git diff
documentationfor full information on the kinds of things you can get differences for.
有关您可以获得差异的类型的完整信息,请参阅git diff
文档。
Normally, git diff
by itself shows all the changes in the whole repository(not just the current directory).
通常,git diff
它本身会显示整个存储库中的所有更改(不仅仅是当前目录)。
回答by AJ Zane
Another method (mentioned in this SO answer) will keep the history in the terminal and give you a very deep track record of the file itself:
另一种方法(在这个 SO answer 中提到)将在终端中保留历史记录,并为您提供文件本身的非常深入的跟踪记录:
git log --follow -p -- file
git log --follow -p -- file
This will show the entire history of the file (including history beyond renames and with diffs for each change).
In other words, if the file named bar was once named foo, then git log -p bar (without the --follow option) will only show the file's history up to the point where it was renamed -- it won't show the file's history when it was known as foo. Using git log --follow -p bar will show the file's entire history, including any changes to the file when it was known as foo.
这将显示文件的整个历史记录(包括重命名以外的历史记录以及每次更改的差异)。
换句话说,如果名为 bar 的文件曾经被命名为 foo,那么 git log -p bar(没有 --follow 选项)只会显示文件的历史记录到它被重命名的点——它不会显示文件被称为 foo 时的历史记录。使用 git log --follow -p bar 将显示文件的整个历史记录,包括文件被称为 foo 时的任何更改。
回答by da Rocha Pires
You can use gitk [filename]
to see the changes log
您可以使用gitk [filename]
查看更改日志
回答by Vineet
You can use below command to see who have changed what in a file.
您可以使用以下命令查看谁更改了文件中的内容。
git blame <filename>
git blame <filename>
回答by Tejas Pawar
You can execute
你可以执行
git status -s
This will show modified files name and then by copying the interested file path you can see changes using git diff
这将显示修改后的文件名,然后通过复制感兴趣的文件路径,您可以使用以下命令查看更改 git diff
git diff <filepath + filename>
回答by Mohideen bin Mohammed
to list only commits details for specific file changes,
仅列出特定文件更改的提交详细信息,
git log --follow file_1.rb
to list difference among various commits for same file,
列出同一文件的各种提交之间的差异,
git log -p file_1.rb
to list only commit and its message,
仅列出提交及其消息,
git log --follow --oneline file_1.rb
回答by LuisBento
you can also try
你也可以试试
git show <filename>
For commits, git showwill show the log message and textual diff (between your file and the commited version of the file).
对于提交,git show将显示日志消息和文本差异(在您的文件和文件的提交版本之间)。
You can check git show Documentationfor more info.
您可以查看git show 文档以获取更多信息。
回答by MrMins
Totally agree with @Greg Hewgill
完全同意@Greg Hewgill
And if you path includes spaces, you might use, try adding with apostrophe '
or `
如果您的路径包含空格,您可能会使用,尝试添加撇号'
或`
git diff 'MyProject/My Folder/My Sub Folder/file_2.rb'
git diff '我的项目/我的文件夹/我的子文件夹/file_2.rb'
回答by ElLocoCocoLoco
Or if you prefer to use your own gui tool:
或者,如果您更喜欢使用自己的 gui 工具:
git difftool ./filepath
You can set your gui tool guided by this post: How do I view 'git diff' output with a visual diff program?
您可以按照这篇文章的指导设置您的 gui 工具: How do I view 'git diff' output with a visual diff program?