1

Let's consider this situation where I work on a particular feature on separate branch:

* 456789 bar (origin/master, origin/HEAD, master)
| * 3456789 foo (HEAD -> feature)
| *   2345678 Merge origin/master in feature
| |\
| |/
|/|
* | 1234567 

Some time ago I merged the master, but since then I did foo on my feature while somebody did bar on the master.

Now, I would like to rebase bar right before my last merge:

* 3456789 foo (HEAD -> feature)
* 2345678 Merge origin/master in feature
|\
| |  
| |
* | 456789 bar (origin/master, origin/HEAD, master) 

I tried a git rebase master from feature. The problem is that Git wants to replay all my changes on the top of the master which can be a lot of work.

One possible solution would be this:

$ git checkout -b temp feature~3
$ git merge master
$ git cherry-pick foo -n2
$ git branch -D feature
$ git branch -m temp feature
$ git checkout feature
$ git branch -D temp

I am sure there is an easier way to do it.

nowox
  • 25,978
  • 39
  • 143
  • 293

1 Answers1

1

What you described at the end of your question is a good way to do it.

Here you can figure and understand what is going on.
How to move HEAD back to a previous location? (Detached head)


Another option is to do a sqaush

As squash is an interactive rebase which allow you to modify any commit you wish in any way. Using the edit command you can edit and add any commits at any given point of the history.


In order to do a git squash follow those steps:

// X is the number of commits you wish to squash, in your case 6
git rebase -i HEAD~X

Once you squash your commits - choose the e for edit and the manipulate the desired commits.

If you wish to keep the commit as is so you should use what you have suggested with the git cherry-pick

enter image description here

Community
  • 1
  • 1
CodeWizard
  • 128,036
  • 21
  • 144
  • 167
  • I don't really understand how `git rebase -i` will help me in this case. In my example `2345678` has two parents. I want its second parent `1234567` to become `456789`. – nowox Mar 03 '16 at 22:27
  • edit the desired commit before the merge and set the new content you want it to contain, merge has 2 parents, you can can checkout any of the parents. – CodeWizard Mar 04 '16 at 05:13