Sammy I.
Sammy I.

Reputation: 2735

In Git, how do you rebase from your feature branch when the source branch history has been changed?

In my case, I branched off from branch master and I have done work on it since. However, then master squashed a lot of commits and now the last common commit - the one that I originally branched off from - doesn't exist anymore.

Is there a way to rebase the master branch into my own with the least amount of pain possible? Ideally, I'd like my feature branch history to be

Current HEAD of master branch -> my changes (squashed or not)

Currently, trying to simply do git rebase origin/master brings up a ton of file changes that happened in master but not in my local branch.

Upvotes: 1

Views: 58

Answers (1)

grg
grg

Reputation: 5849

The issue is that your feature branch contains commits no longer on master, so a rebase ‘attributes’ these commits to your feature branch and tries to apply them on to master. Therefore you need to limit the range of commits that should be applied.

  • If the point at which you created your feature branch is still in your reflog, you can use --fork-point to let Git automatically work out the correct rebase.

    git rebase --fork-point origin/master
    
  • You can manually specify how far along the branch should be being rebased onto master.

    git rebase <hash-of-first-feature-commit> <your-feature-branch> --onto origin/master
    

Upvotes: 1

Related Questions