在git中查看对已删除文件的更改 [英] Viewing changes to deleted files in git
问题描述
假设当我从master分支出来时,我的仓库的master
分支上有一个文件colors.txt
,其中包含以下内容:
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:
red
green
blue
yellow
然后我分支到my-branch
,在其中进行以下更改:
I then branch off into my-branch
, in which I make the following changes:
- 删除
colors.txt
-
在
red.txt
中添加以下内容:
- Delete
colors.txt
Add
red.txt
with these contents:
red
在green.txt
中添加以下内容:
Add green.txt
with these contents:
green
在blue.txt
中添加以下内容:
Add blue.txt
with these contents:
blue
在yellow.txt
中添加以下内容:
Add yellow.txt
with these contents:
yellow
现在,我需要对master进行一些更改,因此我想合并.但是,有人也将colors.txt
更改为:
Now, there have been some changes on master that I need, so I want to merge. However, someone has also changed colors.txt
to:
red
green
blue
yellow
orange
purple
在合并期间,我得到的唯一信息是我删除了文件colors.txt
,因此如何查看主文件上的更改,以便我可以适当地解决冲突(在这种情况下,通过添加文件orange.txt
和purple.txt
)?
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
)?
推荐答案
您可以使用以下命令在master上显示对该文件所做的所有更改:
You can show all changes done to that file on master using this command:
git diff HEAD...master -- colors.txt
在您的情况下,这应该导致以下输出:
This should lead to this output in your case:
red
green
blue
yellow
+orange
+purple
在git diff
中使用三个点将显示所有提交的更改,这些更改是第二个引用提交的父级,而不是第一个引用提交的父级.因此,使用此方法,您将看到如果未删除文件则将合并的所有更改.
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. Therefore, using this you will see all changes which would be merged if the file had not been deleted.
通过-- colors.txt
使用git diff
显示的更改仅限于该文件.
By using -- colors.txt
the changes shown by git diff
are limited to that file.
该命令的通用版本将是
git diff HEAD...MERGE_HEAD -- colors.txt
MERGE_HEAD
始终设置为合并的提交,因此可以替换合并的分支名称.使用此功能,您甚至可以设置别名以重用此命令:
MERGE_HEAD
is always set to the merged commit, thus it can replace the merged branch name. Using this you could even set up an alias to reuse this command:
git config --global alias.merge-diff-theirs "diff HEAD...MERGE_HEAD"
之后您就可以做
git merge-diff-theirs -- colors.txt
这篇关于在git中查看对已删除文件的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!