2

I am currently converting a SVN repository to Git and during this process I want to make the history as linear as possible. The problem is that I have some feature branches in SVN which merges from the master branch during the process. This merging pattern makes rebaseing a pain and I cannot figure out how to do it properly.

My goal is to replace the merge of the feature branch with a squash commits: enter image description here

I have studied the answer to this question but none of the suggested answers seems to work for my case: git remove merge commit from history

How should I go about squashing my feature branch so that I can rebase it on master at the correct point in time?

2
  • Why is this so desired? Commented Mar 20, 2019 at 0:12
  • I would say that the desire to do this is uncommon. In this case I was importing an old SVN repo and wanted to clean up the repo a bit before starting using it as a Git repo. This cleanup entailed squashing together related commits, fixing commit messages. To be able to do squashing with interactive rebase I needed to get rid of these merge commits. Please note that this Git repo was not active yet, so I was fine with re-writing history. Commented Mar 21, 2019 at 7:51

1 Answer 1

2

Let's say that M is the topmost merge commit (the one between G and H):

git checkout -b cleaned-branch M git reset --soft G git commit -m "Get rid of merge" git rebase M `git rev-parse master` --onto cleaned-branch git checkout -B cleaned-branch 

git reset --soft G will make git think that G is your current base commit, but it does that without touching your files (unlike checkout), so you'll be left with all the changes from the merge in the form of outstanding changes, and the subsequent commit will just have G as its parent. Then, we rebase the remaining commits and move cleaned-branch up to the resulting tip commit.

Sign up to request clarification or add additional context in comments.

4 Comments

I have tried this proposed solution and there are two problems: 1. The commit will have todays time/date instead of the time/date from the history of merge M. 2. When I am done I have a new branch 'cleaned-branch' which is parallel to the master branch. I want to only have the master branch.
Ok.. I have figured out how to change the date/time of the new commit to match the old date/time. i.e. by using the flag --date='2014-05-05T10:08:00' in the commit command.
Just finish of with git checkout -B master on the last line instead, which will move master to the rebased tip commit. Note that the resulting master commit will have a different hash than the original one, but that's inherent in git and impossible to avoid: any manipulation of old commits will change the hash of the modified commits and all their descendant commits.
Ok.. I solved it by doing: git checkout master and then git rebase --onto 'git rev-parse cleaned-branch' M 'git rev-parse master'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.