在git的不同分支中仅检查新文件(未修改) [英] Check for new files only (not modified) in different branches in git
问题描述
我在
- branch1
-releases
-dev
file1.txt
file2.text
- branch2
-releases
-stg
file1.txt
file3.text
现在,我只想知道branch2中不存在的文件.在这种情况下,file2.txt
.即使file1.txt
不同,我也不想显示它.
Now, I want to know only the file which is not present in branch2. In this case file2.txt
. Even if file1.txt
is different, I don't want it to be displayed.
我尝试过类似的事情
git diff --name-only branch1:releases/dev branch2:releases/stg
这将显示所有已更改的文件,即也显示file1.txt
如何获得理想的结果?
This displays all the files that are changed i.e. it also displays file1.txt
How can I get the desired result?
推荐答案
文件既不在分支机构中,也不在分支机构中. Git中的文件存储在 commits 中.也就是说,每个提交都具有其所有文件的完整快照,因此两个不同的提交中可以包含两组不同的文件.
Files are neither in, nor not-in, branches. Files, in Git, are stored in commits. That is, each commit has a complete snapshot of all of its files—so two different commits can have two different sets of files in them.
分支名称仅用于标识提交.因此,分支名称branch1
和branch2
各自标识一些提交.提交的 real 名称是其哈希ID,但是人类无法使用哈希ID,因此Git为我们提供了其他命名提交的方法.
Branch names simply serve to identify commits. So branch names branch1
and branch2
each identify some commit. The real name of a commit is its hash ID, but hash IDs are impossible for humans to work with, so Git gives us other ways to name commits.
要比较任意两个提交,可以尝试使用git diff
:
To compare any two commits, you can use git diff
as you were trying to do:
git diff <left-side-commit-specifier> <right-side-commit-specifier>
-但这并不能满足您的需要,即使两个提交说明符可以只是分支名称:
—but this doesn't get you want you want, even though the two commit specifiers can just be branch names:
git diff branch1 branch2
尤其是,您不能对branch1
命名的提交中的releases/dev/*
与branch2
命名的提交中的releases/stg/*
进行部分比较. Git将比较整个快照,由于releases/dev/
与releases/stg/
不同,因此它们很可能会命名不同的文件. 1
In particular, you can't do a partial comparison of releases/dev/*
in the commit named by branch1
vs releases/stg/*
in the commit named by branch2
. Git will compare the entire snapshots, and since releases/dev/
is different from releases/stg/
, these are quite likely to name different files.1
这意味着您将必须提取两个提交,或者至少提取两个提交的有趣部分.然后,您可以使用任何diff程序-如果愿意,可以使用包含的git diff
-比较这些提取的子树.例如:
What this means is that you will have to extract both commits, or at least, the interesting parts of both commits. You can then use any diff program—git diff
included, if you prefer it—to compare these extracted sub-trees. For instance:
# first, make two directories to compare the trees
mkdir /tmp/left /tmp/right
# now extract releases/dev/ from branch1's tip commit, into /tmp/left
git archive --format=tar branch1 -- releases/dev |
tar -C /tmp/left --strip-components=2 xf -
# and extract releases/stg/ from branch2's tip commit
git archive --format=tar branch1 -- releases/stg |
tar -C /tmp/right --strip-components=2 xf -
这时您可以git diff /tmp/left /tmp/right
.您可以根据需要添加--name-only
或添加--name-status
以获取状态字母,并且可以添加--diff-filter=A
来丢弃状态不为A
的所有列表(已添加新文件).
At this point you can git diff /tmp/left /tmp/right
. You can add --name-only
if you like, or --name-status
to get status letters, and you can add --diff-filter=A
to discard any listing whose status would not be A
(added, new file).
完成后,请记住要清理(删除diff目录的临时左侧和右侧).供生产使用,将硬编码名称/tmp/left
和/tmp/right
更改为使用mktemp -d
.
Remember to clean up (remove the temporary left and right side of diff directories) when you're done. For production use, change the hardcoded names /tmp/left
and /tmp/right
to use mktemp -d
.
1 这使我们进入了侧面讨论.由于每个提交都是其文件 all 的独立快照,因此声明左侧提交中的releases/dev/file1.txt
是同一个文件"是什么意思?如releases/stg/file1.txt
在右侧提交?这些文件显然是完全不同的文件,因为它们具有不同的名称.但是也许它们是同一个文件,因为它们可能具有相同或非常相似的 content .
1This gets us into a side discussion. Since each commit is a standalone snapshot of all of its files, what does it mean to claim that releases/dev/file1.txt
in the left side commit is "the same file" as releases/stg/file1.txt
in the right side commit? These are obviously totally different files, because they have different names. But maybe they are the same file, because maybe they have the same, or very similar, content.
当您在两个 commits 上使用git diff
时,Git会将提交视为一个整体.左侧提交包含文件,并且每个文件都有一个名称,例如releases/dev/file1.txt
.右侧提交包含文件,并且每个文件也都有一个名称.即使名称已更改,也许文件是相同的.也许它们不一样,因为名称已更改.
When you use git diff
on two commits, Git considers the commits as a whole. The left side commit has files, and each of those files has a name, such as releases/dev/file1.txt
. The right side commit has files, and each of those files has a name too. Maybe the files are the same even if the names have changed. Or maybe they aren't the same, because the names have changed.
Git在这里为您提供了一个选择:
Git offers you an option here:
-
您可以决定,如果名称不同,则文件肯定是 不同.为此,只需确保关闭Git的重命名检测.
You can decree that if the names are different, the files are definitely different. To do this, simply make sure that Git's rename detection is turned off.
或者,您可以决定,如果左侧提交中的文件根本没有出现在右侧提交中,而右侧有一些文件的名称却没有出现在左侧中在侧面提交时,Git应该尝试检测文件是否已被重命名.为此,请确保已打开Git的重命名检测,并设置相似性索引值.这种相似性索引是Git决定文件left/side/name.ext
的方式,该文件显然与right/newname.newext
不同,但是无论如何都可以是相同的.如果这两个名称不同的文件的内容匹配到某种程度的相似性,则Git将声明它们是相同文件".毕竟.
Or, you can decree that if the left side commit has a file whose name doesn't appear at all in the right side commit, and the right side has some file whose name doesn't appear in the left side commit, Git should try to detect whether the file has been renamed. To do this, make sure that Git's rename detection is turned on, and set a similarity index value. This similarity index is Git's way of deciding that file left/side/name.ext
, which is obviously not the same as right/newname.newext
, could be the same anyway. If the contents of those two differently-named files match, to some degree of similarity, Git will declare that those are "the same file" after all.
默认相似性阈值设置为50%,但是您可以选择其他任意值. Git没有记录它如何计算其相似性指数,因此您在此处输入的任何内容都将是猜测,但通常,两个文件匹配得越多,相似性指数就越高.
The default similarity threshold is set to 50%, but you can choose any other value you like. Git does not document how it computes its similarity index so anything you put in here is going to be guesswork anyway, but in general, the more the two files match up, the higher the similarity index will be.
这篇关于在git的不同分支中仅检查新文件(未修改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!