Ankur Garg
Ankur Garg

Reputation: 2853

Resolving git rebase deleted by Us

I have a file say a.java which I have modified.

In the meantime before I push my changes to master, Another developer deleted that file (basically renamed the package).

Now when I rebase my changes, I see

Unmerged paths:
   deletedbyus: a.java

Now since I want to keep the changes,I made ..I tried git add a.java but it does not seem to work i still see

Unmerged path:
 deltedbyus: a.java

If i do git rm or rm,the file will be staged but it will show as deleted thereby removing the file alltogether.

What should be correct way to restore the file with my changes intact.

Upvotes: 5

Views: 1411

Answers (1)

Manuel Schmidt
Manuel Schmidt

Reputation: 2417

Assuming you are on master branch, and someone else pushed the the package change of a.java file to master and you are now doing git rebase on master branch after committing your changes, let's clarify what it exactly means:

In this situation git is resetting the HEAD of your master branch to the one of the remote origin/master branch. And then git is taking each of actually your commits and rewriting them on top of the new master's HEAD.

Actually they are your commits which are being rewritten, but from git's point of view they are the changes of them because they belong to the branch git is rebasing on top of your current branch (which aren't actually your changes, but it's now our branch, therefore our changes).

In this context, when git tells you deletedbyus it means that in the commit git is rewriting, the file exists again, because it has been already deleted in our branch (by us), but what should I (git) do with the changes to a file that no longer exists? Someone deleted it intentionally and I (git) cannot create the file again without confirmation (and that is correct, indeed).

So, what should you do to fix this situation? Actually you should include your changes (theirs from git's point of view during the rebase) in the location where the file now exists (on the new package, assuming you want to keep the package change). Once you have done this, you should do

# Manually carry the changes you made to 
# file/on/old/package/a.java 
# to file/moved/to/new/package/a.java

git add file/moved/to/new/package/a.java # To confirm your changes
git rm file/on/old/package/a.java # To confirm deletion
git rebase --continue # To continue with your rebase

But, which changes did you make to the file? You can see it with git diff HEAD:file/moved/to/new/package/a.java file/on/old/package/a.java

Upvotes: 6

Related Questions