[英]Viewing changes to deleted files in git
Let's say that when I branch off from master, there's a file colors.txt
on the master
branch of my repo with these contents: 假设当我从
master
分支出来时,我的repo的master
分支上有一个文件colors.txt
,其中包含以下内容:
red
green
blue
yellow
I then branch off into my-branch
, in which I make the following changes: 然后,我分支到
my-branch
,在其中进行以下更改:
colors.txt
colors.txt
Add red.txt
with these contents: 添加
red.txt
并包含以下内容:
red
Add green.txt
with these contents: 添加
green.txt
并包含以下内容:
green
Add blue.txt
with these contents: 添加具有以下内容的
blue.txt
:
blue
Add yellow.txt
with these contents: 添加带有以下内容的
yellow.txt
:
yellow
Now, there have been some changes on master that I need, so I want to merge. 现在,我需要对master进行一些更改,因此我想合并。 However, someone has also changed
colors.txt
to: 但是,也有人将
colors.txt
更改为:
red
green
blue
yellow
orange
purple
During my merge, the only information I get is that I deleted the file colors.txt
, so how can I see the changes that have been to the file on master so I can appropriately resolve the conflict (in this case, by adding the files orange.txt
and purple.txt
)? 在合并期间,我得到的唯一信息是删除了文件
colors.txt
,因此如何查看主文件上的更改,以便我可以适当地解决冲突(在这种情况下,通过添加文件) orange.txt
和purple.txt
)?
You can show all changes done to that file on master using this command: 您可以使用以下命令在master上显示对该文件所做的所有更改:
git diff HEAD...master -- colors.txt
This should lead to this output in your case: 在您的情况下,这应该导致以下输出:
red
green
blue
yellow
+orange
+purple
Using three dots for git diff
will show the changes of all commits which are parents of the second referenced commit but not of the first referenced commit. 对
git diff
使用三个点将显示所有提交的更改,这些更改是第二个引用提交的父级,而不是第一个引用提交的父级。 Therefore, using this you will see all changes which would be merged if the file had not been deleted. 因此,使用此操作,您将看到如果尚未删除文件则将合并的所有更改。
By using -- colors.txt
the changes shown by git diff
are limited to that file. 通过使用
-- colors.txt
, git diff
显示的更改仅限于该文件。
A more generic version of that command would be 该命令的更通用版本是
git diff HEAD...MERGE_HEAD -- colors.txt
MERGE_HEAD
is always set to the merged commit, thus it can replace the merged branch name. MERGE_HEAD
始终设置为合并的提交,因此它可以替换合并的分支名称。 Using this you could even set up an alias to reuse this command: 使用此方法,您甚至可以设置一个别名以重用此命令:
git config --global alias.merge-diff-theirs "diff HEAD...MERGE_HEAD"
Afterwards you can just do 之后你可以做
git merge-diff-theirs -- colors.txt
Tricky scenario, right? 棘手的场景,对不对? I think the best you can try to do is find out "when" color.txt was deleted.
我认为您可以尝试做的最好的就是找出“何时” color.txt被删除。 If you weren't merging, I'd say: use a bisect to find out when the file disappeared so you can see what happened but you are merging so that's no good.
如果您没有合并,我会说:使用二等分线找出文件消失的时间,这样您就可以看到发生了什么,但是您正在合并,所以这不好。
git blame --reverse
can help somewhat by telling you when was the last time the file was present on your working tree. git blame --reverse
可以通过告诉您文件何时最后一次出现在您的工作树上而有所帮助。 git log --name-status -- <file-path>
can also help you see when the file disappeared (and why, through a nicely written revision comment, right?). git log --name-status -- <file-path>
还可以帮助您查看文件何时消失(以及为什么要通过写得很好的修订注释,对吗?)。 I would tell you to give difflame a testdrive (A tool I worked on that by blending blame and diff output would be able to tell you the exact revision where the lines were deleted) but I think that because the file was deleted altogether, it will break.. not sure though. 我会告诉您给difflame一个testdrive(我通过混合blame和diff输出使用的工具可以告诉您删除行的确切版本),但是我认为因为文件被完全删除了,它将打破..虽然不确定。 Here's the link just in case: https://github.com/eantoranz/difflame .
这是链接,以防万一: https : //github.com/eantoranz/difflame 。 Now... if you are able to find out on which revision the file was deleted, you might find out why it was deleted and you should figure out that you have to create a file for the new color that was added to color.txt while keeping color.txt deleted so that the merge is "complete".
现在...如果您能够找出删除该文件的版本,您可能会发现为什么删除该文件,并且应该弄清楚必须为添加到color.txt的新颜色创建一个文件。 同时保持color.txt删除,以便合并“完成”。
Okay, I tested the following: 好的,我测试了以下内容:
For me, git diff colors-test.. -- colors.txt
gives the following output: 对我来说,
git diff colors-test.. -- colors.txt
提供以下输出:
-red
-green
-blue
-yellow
-orange
-purple
where color-test
is the branch where I added orange/purple and ..(nothing)
indicates the HEAD
. 其中
color-test
是我添加了橙色/紫色的分支,而..(nothing)
表示HEAD
。 This might be okay when the file is that small, however, a full diff might be better. 如果文件很小,这可能没问题,但是完整的差异可能会更好。
To find out, in which commit you deleted the file, you can try something like 要找出删除文件的提交方式,可以尝试类似
git log --name-status -- colors.txt
and you'll find a commit with D colors.txt
which is the commit in which you deleted the file, in my case the short hash is e1bb165
. 并且您将找到一个带有
D colors.txt
的提交,这是删除文件的提交,在我的情况下,短哈希为e1bb165
。 You might want to diff against the commit before that 您可能想在此之前与提交进行比较
git diff colors-test..e1bb165~1 -- colors.txt
which gives me 这给了我
green
blue
yellow
-orange
-purple
When you pass the -R
switch to git diff
, you will get +
instead of -
(the inputs are reversed). 当您将
-R
开关传递给git diff
,将得到+
而不是-
(输入是反向的)。
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.