I have the following situation:

  • I created a clone(Y) from a main repository(X), because there were many people working on Y we didn't do any rebase but only merges. When we want to deliver(push) Y to X we would like to do a rebase in order to have things nice and clean

The problem is that when doing rebase we are asked to do all the merges that we already did in the previous merge steps. Is there a solution to this, beside the one that means actually re-doing the merges?

I expected it to be pretty straightforward since we already solved the conflicting merges.

share|improve this question
    
At: "Because there were many people working on Y we didn't do any rebase but only merges", you mean merge with the upstream is that it? – Ciro Santilli 华涌低端人口 六四事件 法轮功 Jan 11 '14 at 22:02
up vote 69 down vote accepted

Rebasing to get a "clean" history is overrated. The best way if you want to preserve history is just to do the merge instead of a rebase. That way if you ever need to go back to a revision, it is exactly the same as the one you tested during development. That also solves your issue about the previously solved merge conflicts.

If you don't care about preserving history, you can create a new branch off of master, check it out, then do a git read-tree -u -m dev to update your working tree to match the dev branch. Then you can commit everything into one big commit and merge it into master as normal.

share|improve this answer
1  
What you've effectively done here is a merge. The new branch is unnecessary. – isak gilbert Aug 13 '14 at 1:43
    
@isakgilbert It's only the same if you merge with --squash. A regular merge will add N or N + 1 commits to master if there were N commits on the branch. The suggestion above, or merge --squash, will always add only a single commit to master. – peterflynn Mar 17 '15 at 17:40
2  
@ytpete, yes exactly. I feel the above is just a roundabout way of doing a merge --squash from dev to master. "Creating a new branch" is just pointing at the same place as master is. "Commit everything into one big commit" is just doing the squash. – isak gilbert Mar 17 '15 at 22:18

git merge --squash is now my preferred way of rebasing after a large amount of work and many merges (see this answer). If the branch you're working on is called my-branch and you want to rebase from master then just do the following:

git checkout my-branch
git branch -m my-branch-old
git checkout master
git checkout -b my-branch
git merge --squash my-branch-old
git commit
share|improve this answer

Two remarks:

  • you can rebase your own (non yet pushed) work as many time as you want on top of newly fetched commits.
  • You could avoid the merge conflicts (during rebase) if you had activated git rerere, which is done for this kind of situation.
    http://git-scm.com/images/rerere2.png See more at git rerere.
share|improve this answer
    
unfortunately I didn't have rerere activated... – INS Jun 6 '11 at 7:47
    
@lulian: in that case, if you have to rebase your work, I think you will have to do those merge conflict resolutions again. – VonC Jun 6 '11 at 8:09
    
Link to activation of rerere doesn't work. – krlmlr Dec 11 '13 at 12:49
    
@krlmlr Thank you. I have restored the link, added an illustration and a reference to the man page of that command. – VonC Dec 11 '13 at 12:56

You can take all of the changes in your branch and put them into a new commit in master with the following:

git diff master > my_branch.patch
git checkout master
patch -p1 < my_branch.patch

Then stage your files and commit.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.