英文:
What is the git diff command needed to show the changes a merge would make without performing the merge?
问题
The git diff
command needed to show the changes a merge would make without performing the merge is:
git diff --name-status main...feature/cool
英文:
What is the git diff
command needed to show the changes a merge would make without performing the merge?
I've done searches and not found what I'm looking for. For example, I'm on branch feature/cool
, and I run git diff main
. It shows me all of the new files I have created on feature/cool
that's not what would be merged. It is, however, a valid difference between the two branches.
I've seen how to do this with git merge
see this:
- This https://stackoverflow.com/questions/11727102/git-diff-to-see-what-a-merge-would-introduce is the same/similar question but the answer is to use git merge.
I would accept that git doesn't have a way to do that with the git diff
command but I thought I found the command to do this a few days ago.
答案1
得分: 2
以下是翻译好的内容:
"在合并之前,确切地无法知道合并后会发生什么变化,在所有情况下。 话虽如此,大多数情况下,3个点的差异语法会接近如下:
git diff <target-branch>...<source-branch>
在以下任何一种情况下,3个点的差异都将完全正确:
- 源分支与目标分支完全同步。
- 源分支未涉及与目标分支相同的任何文件,因为这两个分支的合并基点不同。
当这两个规则不成立时,有可能同一行在两侧都进行了修改(通常是由于挑选),然后差异中显示的某些更改实际上不会包含在合并中。
侧面说明: 大多数情况下,3个点的差异通常用作某些工具中Pull Requests的默认视图(例如GitHub和Azure DevOps)。
建议: 如果您真的想知道会发生什么变化,只需执行以下操作并查看:
git switch --detach <target-branch> # 解除关联以避免影响现有分支
git merge <source-branch> --no-ff # 强制生成合并提交以防止快进合并
git diff @~1 @ # 查看合并提交之前和之后的差异
英文:
It's not possible to know before the merge, exactly what will change after the merge, in all scenarios. That being said, most of the time the 3 dot diff syntax will be close:
git diff <target-branch>...<source-branch>
The 3 dot diff will be exactly correct in either of the following scenarios:
- The source branch is fully up to date with the target branch.
- The source branch doesn't touch any of the same files as the target branch, since the merge-base of the two branches.
When those 2 rules aren't true, it's possible that the same lines have been modified on both sides (oftentimes due to a cherry-pick) and then some of the changes that show up in the diff won't actually be included in the merge.
Side Note: The 3 dot diff, being useful most of the time, is oftentimes used as the default view for Pull Requests in some tools (e.g. GitHub and Azure DevOps).
Suggestion: If you really want to know what will change, just do it and look:
git switch --detach <target-branch> # detach to avoid touching existing branches
git merge <source-branch> --no-ff # force a merge commit to prevent fast-forward
git diff @~1 @ # look at the difference between before and after the merge commit
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论