1

I had a series of commits like this:

A-->B-->C-->D-->E-->F

Commits D and later were a new feature, so I updated to make commits D-->E-->F a branch off of C:

A-->B-->C \ -->D-->E-->F

Now I've made additional commits on top of C, so my repo currently looks like this:

A-->B-->C-->C'-->C'' \ -->D-->E-->F

I'd like to replay the commits in my feature branch (D-->E-->F) on top of C'', so the result is:

A-->B-->C-->C'-->C'' \ -->D-->E-->F

How might I do this?

I acknowledge there may be conflicts (which I'm happy to resolve), but figure there has be a better way than manually copying over changes.

Thanks!

anon_swe
  • 8,791
  • 24
  • 85
  • 145

2 Answers2

1

The rebase (git rebase) will replay those commits:

git checkout feature
git rebase branchC

That will give you:

A-->B-->C-->C'-->C''
                  \
                   -->D'-->E'-->F'

Those commits will be different in that:

  • their parent will have changed
  • their content could have changed if you have to resolve some conflicts.

If you end up regularly doing that rebase, don't forget to activate rerere.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This is super helpful. I did what you suggested, worked through the merge conflicts, and completed the rebase. When I try to push my local `feature` branch to the remote branch, it tells me `error: failed to push some refs...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.` I'm a bit confused because, after the rebase, my local branch should be ahead of the remote one, I think? – anon_swe Nov 07 '18 at 18:07
  • 2
    @bclayman No: the rebase has changed the history of your feature branch: you need to force push it (`git push --force`). Which is OK if you are the only one working on that branch. – VonC Nov 07 '18 at 18:09
0

This is essentially rebasing the branched code to the parent.

Ref: https://git-scm.com/docs/git-rebase

First need to checkout the branch which needs to be rebased

git checkout branchD

Then need to rebase using "rebase" option of git command

git rebase parentBranch

Note that with/without conflicts, new commits will happen and new SHA will be created for the commits.

A_P
  • 21
  • 5
  • But... that is essentially what I *already* mentioned in my answer, isn't it? – VonC Nov 07 '18 at 07:31
  • Wanted to provide the doc link and the note that SHA will get updated after rebase. Just added the commands for more clarity. – A_P Nov 08 '18 at 16:28