Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git rebase, "would be overwritten", and "No changes - did you forget to use 'git add'?"

Tags:

git

git-rebase

git rebase does not appear to work properly in certain cases where a file is added to the repository, then removed from the repository, then added to the working directory (but not the repository).

Here's a more specific description of my problem:

  • if a branch is created and switched to from some trunk,

  • and a file X is added and committed in the branch,

  • and subsequently X is removed and committed in the branch,

  • and X is again created in the working directory, but not added or committed,

  • and the trunk branch advances,

  • then

  • a rebase performed using the advanced trunk as the base will fail because it will refuse to overwrite X,

  • and the rebase cannot be continued even if the working directory X is removed or moved out of the way.

Here's a script to reproduce my problem on the command line:

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".

I know I can abort the rebase using git rebase --abort, remove bar.txt, and then git rebase master again. But how can I continue the rebase without aborting it first?

like image 960
Russell Silva Avatar asked Sep 15 '10 19:09

Russell Silva


2 Answers

I found a solution: apply the troubled commit's patch manually and add it to the index.

$ 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
like image 97
Russell Silva Avatar answered Oct 12 '22 23:10

Russell Silva


You need to commit before attempting to rebase. It will work sometimes if you don't, but you shouldn't do that.

If you're uncomfortable committing for some reason, you could at least use stash (which does roughly the same thing).

like image 42
Dustin Avatar answered Oct 13 '22 00:10

Dustin