git rebase,"会被覆盖","没有变化 - 你忘了使用'git add'吗?"

Rus*_*lva 9 git git-rebase

git rebase 在将某个文件添加到存储库,然后从存储库中删除,然后添加到工作目录(但不是存储库)的某些情况下,似乎无法正常工作.

这是我的问题的更具体的描述:

  • 如果创建分支并从某个主干切换到

  • 并在分支中添加并提交文件X,

  • 然后X被删除并在分支中提交,

  • 并且X再次在工作目录中创建,但未添加或提交,

  • 并且树干分支前进,

  • 然后

  • 使用高级主干作为基础执行的rebase将失败,因为它将拒绝覆盖X,

  • 即使工作目录X被移除或移开,也无法继续使用rebase.

这是一个在命令行上重现我的问题的脚本:

git init
echo foo > foo.txt
git add .
git commit -m 'foo'
echo foo >> foo.txt
git add .
git commit -m 'foo foo'
git checkout -b topic HEAD^
git log
echo bar > bar.txt
echo baz > baz.txt
git add .
git commit -m 'bar baz'
git rm bar.txt
git commit -m '-bar' 
echo bar > bar.txt
git rebase master 
# the following output is emitted:
# First, rewinding head to replay your work on top of it...
# Applying: bar baz
# Using index info to reconstruct a base tree...
# Falling back to patching base and 3-way merge...
# error: Untracked working tree file 'bar.txt' would be overwritten by merge.  Aborting
# Failed to merge in the changes.
# Patch failed at 0001 bar baz
# 
# When you have resolved this problem run "git rebase --continue".
rm bar.txt
git rebase --continue
# the following output is emitted:
# Applying: bar baz
# No changes - did you forget to use 'git add'?
# 
# When you have resolved this problem run "git rebase --continue".
# If you would prefer to skip this patch, instead run "git rebase --skip".
# To restore the original branch and stop rebasing run "git rebase --abort".
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用git rebase --abort,删除bar.txt,然后git rebase master再次中止rebase .但是,我怎样才能在不中止它的情况下继续使用rebase?

Rus*_*lva 4

我找到了一个解决方案:手动应用有问题的提交补丁并将其添加到索引中。

$ patch -p1 < .git/rebase-apply/patch 
patching file bar.txt
patching file baz.txt
$ git add bar.txt baz.txt
$ git rebase --continue
Applying: bar baz
Applying: -bar
Run Code Online (Sandbox Code Playgroud)