对于您非常具体的情况,例如支持所有更改,包括在您的分支上删除文件,您可以按照以下方式进行操作。一般来说,尽管总是手动解决变基更好。
# attempt a rebase, expect it to fail
$ git rebase their_branch
# take our side of all files that changed on their_branch and master branch,
# expect resolution to fail for renames (renames include file deletions) still;
# also note the meaning of `theirs` and ‘ours’ is reversed for a rebase.
$ git checkout --theirs -- .
# tell git that conflicts have been resolved (after we took our side)
$ git add -A
# explicitly take our side of all deletes
$ git diff --name-only --diff-filter=UD | xargs -n1 git rm
# proceed to rebase our changes, expect this to succeed
$ git rebase –continue && echo “SUCCESS!”
所有这些都可以安排在一个命令、别名或 shell 函数中:
$ git rebase their_branch || ((git checkout --theirs -- . \
|| (git diff --name-only --diff-filter=UD | xargs -n1 git rm) && git add -A) \
&& git rebase --continue)
如果你的 rebase 碰巧成功了,||
将会避免多余的脚本解析,在你的 rebase 成功的第一个状态停止,视情况而定。
这是一个启动的玩具示例,用于说明文件删除的变基,
# their_branch = master, our dev branch = foo
$ cd /tmp/toy-repo
$ git init . && touch README && git add README && git commit -m "add README"
$ git checkout -b foo && git rm README && git commit -m "rm README"
$ git checkout master && echo "changing README" >> README && git add README && git commit -m "modify README"
$ git checkout foo && git rebase master || ((git checkout --theirs -- . || (git diff --name-only --diff-filter=UD | xargs -n1 git rm) && git add -A) && git rebase --continue)
$ git log
a3730c5 rm README // on foo, rebased
55c9074 modify README // on master
1fa1398 add README // on master