`git diff –staged`

huangapple go评论60阅读模式
英文:

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:

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个点的差异都将完全正确:

  1. 源分支与目标分支完全同步。
  2. 源分支未涉及与目标分支相同的任何文件,因为这两个分支的合并基点不同。

当这两个规则不成立时,有可能同一行在两侧都进行了修改(通常是由于挑选),然后差异中显示的某些更改实际上不会包含在合并中。

侧面说明: 大多数情况下,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:

  1. The source branch is fully up to date with the target branch.
  2. 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

huangapple
  • 本文由 发表于 2023年4月13日 20:23:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定