简体   繁体   English

如何列出特定分支中的所有已更改文件?

[英]How to list all changed files in a particular branch?

I have created one branch xyz from master. 我已经从master创建了一个分支xyz。 I have done approximately 1000 commits and probably modified 20 files in xyz branch. 我已经完成了大约1000次提交,并可能在xyz分支中修改了20个文件。 Now I want to list all the files which I have modified in xyz branch. 现在,我要列出在xyz分支中修改过的所有文件。

Following command lists files modified in both branches. 以下命令列出了两个分支中修改的文件。

git diff --name-only master...xyz

The fundamental problem here is that git diff compares two specific commits. 这里的根本问题是git diff比较两个特定的提交。 1 No matter what arguments you give it, it's still going to choose two specific commits, and compare those two. 1无论你给什么样的参数,它仍然要选择两个具体提交,并比较这两个。 2 2

What this means is that to get git diff to show you what you have done in some branch, you must pick two commits within that branch: one to call a "starting point" and one to call an "ending point". 这意味着要使git diff向您显示您在某个分支中所做的事情,您必须在该分支中选择两个提交:一个称为“起点”,另一个称为“终点”。 If you choose to do this, it usually helps to draw the commit graph. 如果选择这样做,通常有助于绘制提交图。 You can do this manually, or use git log --oneline --graph to help out. 您可以手动执行此操作,也可以使用git log --oneline --graph进行帮助。 (A nice way to see this graph for your two branches in comparison with each other is git log --oneline --graph --decorate --boundary master...xyz .) Another way is to try drawing your graph yourself, which can let you represent it more compactly, or to use gitk or some other graphical viewer that will draw the graph. (查看这两个分支相互比较的图形的一种好方法是git log --oneline --graph --decorate --boundary master...xyz 。)另一种方法是尝试自己绘制图形,这可以让您更紧凑地表示它,或者使用gitk或其他可以绘制图形的图形查看器。

If there is a single merge base commit between the tip commit on master and the tip commit on xyz , then: 如果master的tip提交和xyz上的tip提交之间只有一个合并基础提交,则:

git diff --name-only master...xyz

will compare (and list the names of files modified) that particular merge base, vs the tip-most commit of branch XYZ. 将比较(并列出修改的文件的名称)特定合并基础与分支XYZ的最尖端提交。 That is, if the graph looks something like: 也就是说,如果图形看起来像:

          o--o--o--o--...--o--Z   <-- master
         /
...--o--*
         \
          F--G-...--X             <-- xyz

then * is the merge base commit, Z is the tip commit on master , 3 X is the tip commit on xyz , and git diff master...xyz will compare commit * vs commit X . 然后*是合并基础提交, Zmaster上的xyz提交, 3 Xxyz上的xyz提交, git diff master...xyz将比较commit *与commit X (By contrast, git diff master..xyz —note the two dots instead of three, here—will compare commit Z vs commit X .) (通过对比, git diff master..xyz (在这里注意两个点而不是三个点)将比较提交Z与提交X

That said, given that git diff --name-only master...xyz is not showing you what you want, you might want git log --name-only master..xyz (probably also with --oneline ; I see gucce added this as a comment as well ), to show you what happens in each commit that is in xyz that is not also in master . 就是说,鉴于git diff --name-only master...xyz没有显示您想要的内容,您可能希望git log --name-only master..xyz --oneline (可能也与--oneline一起--oneline ;我看到了gucce也将其添加为注释 )),以向您展示xyz中的每次提交(而不是master中的每次提交)发生了什么。 That is, given the above graph, this will diff commit * against commit F , then F against G , and so on up to the comparison against X . 也就是说,在上述的曲线图中,这将DIFF提交*针对提交F ,然后F针对G ,并依此类推,直到针对所述比较X Note that this is using git log 's built-in ability to diff each commit against its parent. 请注意,这是使用git log的内置功能来将每个提交与其父目录进行比较。 4 4


1 It can also compare one commit to the index, or one commit to the work-tree, or the index to the work-tree. 1它也可以比较对索引的一次提交,或对工作树的一次提交,或对工作树的索引。 In another mode it can be used to compare two specific files, either or both of which need not be stored in the repository at all, but that's even more different from the way you want to use it. 在另一种模式下,它可以用于比较两个特定的文件,这两个文件中的一个或两个都不需要存储在存储库中,但这与您要使用它的方式甚至更大。

2 This, too, is a bit of an overstatement: by giving git diff more than one pair of commits, you can get it to produce what it calls a combined diff . 2这也有点夸大其词:通过给git diff多个提交,您可以得到它来产生所谓的diff组合 This is normally used for merge commits, to show points where the merged contents differ from all parent commits, ie, where a merge conflict was resolved by choosing something other than "what was in one branch". 这通常用于合并提交,以显示合并内容不同于所有父提交的点,即,通过选择“分支中的内容”以外的内容解决了合并冲突的点。 But this, too, goes quite a bit in the wrong direction, compared to what you want to achieve. 但是,与您要实现的目标相比,这也朝着错误的方向发展。

3 I had this as T , for tip-of-master, for a moment, but then realized that T is alphabetically between F and X . 3我暂时将其作为T表示为T ,但后来意识到T按字母顺序在FX之间。 X is for xyz , so I changed T to Z here so that it comes after X . Xxyz ,因此我在这里将T更改为Z ,以便它紧随X之后。 The general idea is that we're not interested in most of the commits exclusively on master , except for the tip-most one that the name master selects. 一般的想法是,除了master选择的最尖端的提交之外,我们对大多数仅对master的提交不感兴趣。

4 If any of the commits in the F -through- X chain is a merge commit, git log won't even bother to diff it at all unless you add some additional options. 4如果F X链中的任何提交都是合并提交,则git log甚至不会费心将其与diff进行比较,除非您添加一些其他选项。 That is, git log skips over merges when it comes time to diff them, unless you either ask for combined diffs ( -c or --cc ) or to split merges ( -m ). 也就是说,当需要比较git log时, git log跳过合并,除非您要求组合的diff( -c--cc )或拆分合并( -m )。 All three of these options have drawbacks as well, so your best bet is to work with a no-merges situation in the first place. 所有这三个选项也都有缺点,因此,最好的选择是首先在没有合并的情况下工作。

As pointed out by @torek, the git diff command ignores the dot notation completely. 正如@torek指出的那样, git diff命令完全忽略了点符号。

Therefore, the following command should lead to the desired results: 因此,以下命令应导致预期的结果:

git log --oneline --name-only develop..xyz | sort | uniq

Which prints out something like this, with the modified files at the end: 它将打印出类似以下内容的文件,并在文件末尾添加修改后的文件:

0871be6 commit 1
9caad09 commit 2
bb714f0 commit 3
...
path/to/file1
path/to/file2
path/to/file3
path/to/file4

Notice, there are only two dots. 注意,只有两个点。 This means, take into consideration all revisions which are in xyz but not in develop . 这意味着,请考虑xyz但未develop所有修订。

Using three dots uses all revisions which are exclusively in one of the two branches, but not in both. 使用三个点将使用所有修订,这些修订专门在两个分支之一中,但不能同时在两个分支中。

Reference: https://git-scm.com/book/tr/v2/Git-Tools-Revision-Selection#Commit-Ranges 参考: https : //git-scm.com/book/tr/v2/Git-Tools-Revision-Selection#Commit-Ranges

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM