Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebasing after squash merge?

Tags:

git

git-rebase

I have a 'master' branch and several topic branches. Assume that the master branch is used primarily as a release candidate and no development work happens on this branch.

The topic branches are several and are shared by the team. Some of the branches have more than one developer working on them. All topic branches are rebased from the master branch regularly.

To clean up the history in the 'master' branch, I did a 'git merge --squash' when merging code from topic to master branches. This worked perfectly fine.

Now - when topic branches are rebased -- the commits are getting duplicated. Is there a way to clean up the commits on the topic branches after the 'git merge --squash' has been successful?

like image 448
mustard Avatar asked Mar 29 '12 06:03

mustard


People also ask

Does squash and merge also rebase?

What is the squash rebase workflow? It's simple – before you merge a feature branch back into your main branch (often master or develop ), your feature branch should be squashed down to a single buildable commit, and then rebased from the up-to-date main branch.

Should you squash before rebasing?

Before rebasing such branches, you may want to squash your commits together, and then rebase that single commit, so you can handle all conflicts at once. Here's how to do that. Imagine you've been working on the feature branch show_birthday , and you want to squash and rebase it onto main .

Should I squash and merge or rebase and merge?

When should I rebase and when should I squash? It does not matter which you use but I recommend rebase. Rebase changes the parent node of the feature branch but merge does not and I recommend it because it keeps the commit structure simpler but as a git user, it makes not different.

What happens when you squash and merge?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.


2 Answers

Lets assume you have the following scenario:

A - B - C (master)
 \
  D - E (topic)

If you merge topic into master with --squash you will get something like

A - B - C - F (master)
 \
  D - E (topic)

Where F contains all changes from D and E. Rebasing topic on master makes no sense since the topic branch is already in master (through F). Instead of rebasing you could move the topic branch to F, e.g.

git checkout master
git branch -f topic F

Which yields:

A - B - C - F (master/topic)

All you need to do now is to push up the moved topic branch:

git push -f origin topic
like image 165
ralphtheninja Avatar answered Sep 20 '22 18:09

ralphtheninja


I used to do same thing as Magnus just with couple more commands:

git checkout master
git merge --squash topic
git commit -m "Add topic feature"

git branch -D topic
git checkout -b topic

git push -f origin topic
like image 43
Wojtek Kruszewski Avatar answered Sep 21 '22 18:09

Wojtek Kruszewski