Search code examples
gitrebasegit-merge-conflict

How to resolve a rebase conflict when file was removed from master?


From master, the A branch was branched. Then, a few commits (to master) later, B branch was branched.

The owner of A made changes to file fname. The owner of B removed fname altogether.

Then, B was rebased and merged into master. Now, A is to be rebased and merged. But now A's owner gets a conflict with file fname, saying that:

CONFLICT (modify/delete): fname deleted in HEAD and modified in "commit msg". Version "commit msg" of fname left in tree.

Normally, conflicts are resolved by editing the intermediate file, git add fname it and then git rebase --continue the process.

But how to resolve a conflict of a removed file? The end result should be that the files is removed.


Solution

  • In this case you can just delete the file not with git rm but just with rm.

    rm fname
    git add fname
    #Fix any other conflict
    git rebase --continue
    

    Whichever way you leave your file it will be used in the new commit that is being generated.


    What's going on:

    1. The rebase was interrupted because of a conflict.
    2. If you check the log, your current HEAD is the head of master.
    3. You have a file that does not exist in this HEAD. That means that you can't ask git rm because that file is not under version control.
    4. We just remove the file as if you would do with a new file that you don't want to commit.

    If the only change in the current commit was on this deleted file you'll get this error:

    `No changes - did you forget to use 'git add'?`
    

    This is because there is nothing to commit (You have just deleted a file that did not exist in git) In that case you can git rebase --skip instead of --continue.