Search code examples
gitgit-rebase

rebase feature branch - but commit ids had changed


Starting point:

Dev: A -- B -- C
Feat:            \ -- D

Then pulled some new commits to Dev:

Dev: A -- B -- C -- NEW
Feat:            \ -- D 

Then accidentally caused commits hashes on Dev to change* (and pushed with--force) Uppercase and lowercase letters refer to the same patches - but commit hash are different:

Dev: a -- b -- c -- NEW
Feat:  A -- B -- C -- D

Now I want to rebase Feat from Dev, but the same patches are recognized as different commits, and rebase starts from A instead of D:

behavior: a -- b -- c -- NEW -- A -- B -- C -- D
expected: a -- b -- c -- NEW -- D (and recognize that A and a are actually the same)

Please help!!!

*Maybe you can tell me how to change them back: I ran an interactive rebase on Dev in order to only change commit messages with the r option. Then I realized that commit hashes changed, So I reset back - before the interactive rebase using reflog. But now commit messages are the old ones, but commit hashes are still different!!!


Solution

  • In this case you should use cherry-pick instead of rebase

    Revert the changes on dev (From where you want to rebase, the NEW changes).

    git reset --hard <last NEW changes commit>
    

    Sync with your remote dev with your local dev branch

    Now cherry-pick the commits from feature branch and apply to dev

    git cherry-pick <commit-ids from feat branch> //commit ids seprate with space