git rebase dry-运行 的差异

diff for git rebase dry-run

我想看看 topic 分支的哪些部分已经包含在 upstream 分支中,以及可能出现的冲突变基。

它应该是 git-rebase 的一种干燥-运行,显示 topic 之间的差异上游,不包括与主题.

无关的更改

一种解决方案是对差异应用差异。思路是:

  1. 获取 forkpointtopic
  2. 之间的差异
  3. 获取 upstreamtopic
  4. 之间的差异
  5. 比较这些差异

git-diff-rebase脚本文件:

#!/bin/bash

upstream=""
topic=""

root=$( git rev-parse --show-toplevel )
forkpoint=$( git merge-base $upstream $topic )

if [ -z "$forkpoint" ] ; then
  echo "Merge base is not found" 1>&2
  exit 1
fi

# list of the changed files (space separated)
files=$( git -C "$root" diff --name-only $forkpoint $topic | paste -s -d ' ' )

diff -t --tabsize=4 -W $(tput cols) -y --left-column \
  <( git -C "$root" diff $forkpoint $topic ) \
  <( git -C "$root" diff $upstream $topic -- $files ) \
  | grep -vP '(^ *> )' \
  # skip changes that do not interfere with topic (empty left part)

注意:此脚本不适用于名称中包含空格的文件。

用法:git-diff-rebase master topic | less

如何解读结果:

  • * * <     —(右侧空白部分)已包含在 upstream
  • + * | * —(相同的文本)已包含在 upstream
  • - * | + * — 冲突
  • + * | - * — 可能存在冲突
  • + * ( —(右侧空白)无冲突,上游不存在
  • - * ( —(右侧空白)无冲突,存在于上游
  • * ( —(右侧空白部分)出现在主题和上游

其中 * 是任何文本的占位符。

I want to see what parts of a topic branch already included into upstream branch, and what possible conflicts can arise on rebase.

如果我错了请纠正我,但听起来你想要的补丁只是包含 topic 中包含的冲突更改(如果有的话)。

由于 rebase 是一种合并,我认为最简单的方法是在不创建提交的情况下进行合并(您可以将其称为“干合并") 并检查未合并(即冲突)文件的差异:

git checkout topic
git merge upstream --no-commit --no-ff

一旦您的工作目录中有了来自 develop 的合并文件,您所要做的就是检查所有未合并文件的差异:

git diff --diff-filter=U

这里的一种方法是复制目标分支 (git checkout -b tmpbranch),在那里进行变基,然后将其与未修改的分支进行比较。