通过 Git 比较当前文件的提交更改
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1303315/
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
Comparing changes in commits to the current file by Git
提问by Léo Léopold Hertz ??
How can you show the differences of a file in the last 5 commits to the current uncommitted file by Git-show?
如何通过 Git-show 显示最近 5 次提交的文件与当前未提交文件的差异?
I made a change to my file which breaks my code. However, I do not know where the change is.
我对文件进行了更改,这破坏了我的代码。但是,我不知道变化在哪里。
I would like to compare the current uncommitted files to the recent commit (HEAD), to the previous commit (^HEAD) and at least 3 commits deeper.
我想将当前未提交的文件与最近的提交 (HEAD)、先前的提交 (^HEAD) 和至少 3 个更深的提交进行比较。
However, I do not know how you can do it efficiently.
但是,我不知道您如何有效地做到这一点。
In trying to see the changes of the five last commits of one file to the current file in the given branch, I unsuccessfully ran
在尝试查看给定分支中一个文件的最后五次提交对当前文件的更改时,我没有成功运行
git show next~5:handle_questions.php
回答by db_
Here is my cheat-sheet:
这是我的备忘单:
# uncommited file to HEAD
git diff <path>
# uncommited file to before last commit
git diff HEAD^ -- <path>
#last commit to before last commit
git diff HEAD^ HEAD -- <path>
#difference between HEAD and n-th grandparent
git diff HEAD~n HEAD -- <path>
#Another cool feature is whatchanged command
git whatchanged -- <path>
回答by William Pursell
To see the diff between handle_questions.php in the working directory and in the repository 5 commits back, use:
要查看工作目录中的 handle_questions.php 和存储库中 5 次提交之间的差异,请使用:
$ git diff HEAD~5 handle_questions.php
回答by Cody Caughlan
You can use git bisect to track down the commit which introduced a bug.
您可以使用 git bisect 来追踪引入错误的提交。
回答by Mike Tierney
If you know the file that the change was made in, you can also use git blame <path>
- this will give you ahistory of each line of code in the following format:
如果您知道进行更改的文件,您还可以使用git blame <path>
- 这将以以下格式为您提供每行代码的历史记录:
SHA (Author Timestamp Line Number) code
回答by zzapper
From @db_ answer (but shown as an actual example)
来自@db_ 答案(但显示为实际示例)
I just want to see what difference there is between the current file and the last commit for just a single file.
我只想看看当前文件和上次提交的单个文件之间有什么区别。
git diff scripts/processQueue.php
- if( filemtime( $filename ) < time() - 10 ) {
+ $filemtime=filemtime($filename);
+ if( $filemtime < time() - 10 && $filemtime> (time() - 86400))