git rebase after previous git merge
Solution 1
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.
Solution 2
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
Solution 3
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.
See more atgit rerere
.
Solution 4
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.
Solution 5
Regarding the replay of merge conflicts, you can use git rerere to maintain a database of how merge conflicts have already been solved, so that performing a rebase that results in the same conflicts will have the laborious parts done for you automatically.
https://hackernoon.com/fix-conflicts-only-once-with-git-rerere-7d116b2cec67
git config --global rerere.enabled true
The one thing to look out for is that if you resolved something incorrectly it will be automatically borked for you next time too, and you may not really realize it.
More formal documentation here: https://git-scm.com/docs/git-rerere
INS
Updated on March 06, 2020Comments
-
INS about 4 years
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 anyrebase
but onlymerge
s. When we want to deliver(push
) Y to X we would like to do arebase
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 previousmerge
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.
- I created a
-
VonC almost 13 years@lulian: in that case, if you have to rebase your work, I think you will have to do those merge conflict resolutions again.
-
VonC over 10 years@krlmlr Thank you. I have restored the link, added an illustration and a reference to the man page of that command.
-
isak gilbert over 9 yearsWhat you've effectively done here is a merge. The new branch is unnecessary.
-
peterflynn about 9 years@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, ormerge --squash
, will always add only a single commit to master. -
isak gilbert about 9 years@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.
-
Ajax about 5 yearsmerge commits are noisy when trying to see the full set of changes on a single branch. It's a pain to extract the diff and look at it when a clean history makes it simple.
-
basickarl about 4 yearsI couldn't disagree more with with a "clean" history is overrated. It's not overrated it saves time and makes things less confusing.
-
thewoolleyman almost 4 yearsI also could not disagree more. I want to see clean, atomic commits, with the logical intent of the change contained all in one commit, not spread over multiple commits.
-
Fai Ng over 3 yearsI hate it when someone does a commit, then do a local merge with some latest commit from master, and then the remote git(like github) does yet another merge on the pull request.
-
Matt over 2 yearsThis was much easier than squashing with rebasing. It just worked.
-
Ini almost 2 yearsYeah rabasing is simply a horrible idea, thx for the solution