Why git rebase overwrite my local changes? How to avoid overwrite?

Viewed 14221

I'm trying to rebase my branch with the master. But it is always overwrite my local changes after rebase.

I'm using the command git rebase and it will show one file conflict, then I manually resolved the conflicts. After tried git rebase --continue . Then I found my local changes are missing.

How to preserve the local changes and rebase with a master?

5 Answers

Without much information about your current state, it's possible that some of your local commits might already be part of the branch you're rebasing onto.

For example, let's say you're trying to rebase develop on top of master. The develop branch contains a number of commits, but you've merged develop into master a few commits back.

In this case, git rebase master may appear to lose some of the commits, but git has detected they're already part of master so they don't need to be reapplied.

You can use git rebase --interactive to be presented with the list of commits that are about to be rebased. If you don't see some commits you expected, or you see more commits than you expected, you might need to look at using --onto to change the starting point for the rebased commits.

Strange things will happen if you do something wrong in the face of conflicts:

WRONG:

$ editor path/to/conflicted/file # resolve conflicts in editor
$ git commit --amend # no!

$ editor path/to/conflicted/file # resolve conflicts in editor
$ git commit -a # no!

Follow the correct steps:

$ editor path/to/conflicted/file  # resolve conflicts in editor
$ git add path/to/conflicted/file # stage resolved file
$ git rebase --continue

You might be confused by interactive rebase workflow. When you interactively rebase and stop at some commit with edit, it looks similar to the conflict situation: a rebase has stopped partway through, and you can fix things and then git rebase --continue. But in the interactive rebase edit case, the edited commit is fully applied, so you can git commit --amend it, or add new commits on top of it, after which the rebase will continue.

I got tripped up by this a few times in the past; I'm so used to interactive rebasing, from doing it frequently, that just from "muscle memory" I've done silly things like git commit --amend after resolving conflicts.

Related