简体   繁体   English

查找在分支中专门更改的文件

[英]Find files that changed specifically in a branch

I have a git repository in which I usually work on two branches, a public branch, and a private branch, the latter following closely the public branch, but with a few extra additions. 我有一个git存储库,我通常在其中工作两个分支,一个public分支和一个private分支,后者紧随公共分支,但还有一些额外的添加。

Is there a way to find the changes since a given commit that are specific to the private branch ? 自给定提交以来,是否存在找到专用于私有分支的更改的方法?

The closest I can think of is to do something on the lines of: 我能想到的最接近的方法是:

git diff base..public > pub.diff
git diff base..private > private.diff
interdiff pub.diff private.diff

This works, but it's not that robust, and I'm sure it's possible to do that with git in a better way. 这行得通,但并不是那么健壮,而且我确信可以用更好的方式用git做到这一点。

This is essentially how the branches look (with many more merges): 本质上是分支的外观(具有更多合并):

...A---B---C---D---E---F---G---H public
    \           \           \
     A'--I---J---D'--K---L---G'--M private

For instance, let's assume that base above is D in this scheme. 例如,假设在此方案中,上面的baseD Just running 刚跑步

git diff D'..M

will also give me the results of the merge commit G' . 还会给我合并提交G'的结果。 I'd like to avoid that, and have something robust across any numbers of merges. 我想避免这种情况,并且在任何数量的合并中都具有强大的功能。

Because git diff 因为git diff

is about comparing two endpoints, not ranges 关于比较两个端点,而不是范围

This might not be possible ( git diff will not detect were those changes came from). 这可能是不可能的( git diff将无法检测到这些更改来自何处)。

You could try using 您可以尝试使用


Option A 选项A

$ git log -p --no-merges --first-parent D'..M
  • -p gives you a diff (patch) for the commits listed -p给您列出的提交的差异(补丁)
  • --no-merges will exclude merges from the list --no-merges将排除列表中的合并
  • --first-parent will only follow the first parent (the one you merged into eg private ) --first-parent将仅跟随第一个父母(您合并为例如private父母)
  • D'..M as explained in git revisision will git revisision中解释的D'..M

    Include commits that are reachable from <rev2> but exclude those that are reachable from <rev1> 包括从可到达的提交<rev2>但排除那些可达从<rev1>


Option B 选项B

A different approach would be to follow @torek's suggestion and create a temporary branch, cherry-pick all the commits onto it and then look at the diffs from this branch. 一种不同的方法是遵循@torek的建议并创建一个临时分支, cherry-pick所有提交cherry-pick ,然后查看该分支的差异。

Because AFAIK git cherry-pick is not able to skip commits when specifying commit ranges you would first need to get a list of commits for cherry-picking (Here Option A is useful) 由于在指定提交范围时AFAIK git cherry-pick无法跳过提交,因此您首先需要获取用于樱桃选择的提交列表 (在这里, 选项A很有用)

$ git log --no-merges --first-parent D'..M --pretty=format:%H
  • --pretty=format:%H will print all the commit hashes in long form --pretty=format:%H将以长格式打印所有提交哈希

Create the temporal branch (which needs to have the correct starting point) 创建时间分支(需要具有正确的起点)

$ git checkout -b temp D'

And then cherry-pick those commits manually or try using this command (exchanged for a better command suggested by @torek) 然后手动选择那些提交尝试使用此命令(已替换为@torek建议的更好的命令)

$ git cherry-pick $(git rev-list --reverse --topo-order --no-merges --first-parent D'..M)
  • --reverse is used to reverse the order of commits printed (oldest to newest instead of the other way around) --reverse用于反转打印的提交顺序(从最旧到最新,而不是相反)
  • --topo-order this will avoid problems with commits that have weird or wrong timestamps --topo-order这样可以避免时间戳奇怪或错误的提交问题
  • --no-merges skip merge commits --no-merges跳过合并提交
  • --first-parent follow the mainline branch (eg the branch the merges were merged into ) --first-parent跟随主线分支(例如合并的分支)

This approach might fail if commits are based on changes which were merged in 如果提交基于合并到其中的更改,则此方法可能会失败

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

相关问题 git - 查找已创建的分支中已更改的文件 - git - find files changed in branch, since created 有没有办法从某个路径找到 git 分支中更改的所有文件? - Is there a way to find all the files changed in a git branch from a certain path? 当另一个分支合并到一个分支中时,如何查找更改的文件(忽略合并分支中的更改)? - How to find the files changed in one branch when another has been merged into it (ignoring changes from the merged branch)? 查找无法与另一个分支进行比较的当前分支已更改的文件 - Find files that have changed of current branch without being able to compare to another branch 在git中创建分支后,如何找出更改了哪些文件? - How to find out what files were changed after a branch was made in git? 如何找出git分支中更改的文件(不是两个分支之间的差异) - How to find out what files were changed in a git branch (not the difference between two branches) git diff在分支的x提交中更改了文件 - git diff changed files in x commits of a branch 如何列出特定分支中的所有已更改文件? - How to list all changed files in a particular branch? 更改的文件始终显示在主分支中 - Changed files always showing in main branch 在提交到分支之前取回更改的文件 - Getting changed files back before commiting to branch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM