Search code examples
gitgit-branchgit-rebase

Rebasing a branch with another parent branch


I have my master branch as commitA <- HEAD

dev branch as commitA -> commitB -> commitC<- HEAD

feature branch taken from master as commitA -> commitD <- HEAD and pushed it to remote.

I have created the feature branch wrongly from master instead of creating it from dev.

Now I want to make my feature branch to look like commitA -> commitB -> commitC -> commitD<- HEAD, So I have done a rebase:

git rebase dev

The local branch looks like what I need now. But when I push it to remote, It is getting rejected with

! [rejected]          feature -> feature (non-fast-forward)
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So I tried a git pull, Which makes the local branch looks something like:

commitA -> commitB <- HEAD. and it does not make any sense to me. So How can I fix my original problem of making my feature branch in remote to be commitA -> commitB -> commitC -> commitD<- HEAD


Solution

  • Short answer
    Instead of pulling, you should have pushed with --force-with-lease to replace the previous history (A - D) with the new one (A - B - C - D').