Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: how to squash multiple merge commits

I'm working on a git repository which I forked from an upstream. There was a feature I wanted to implement and I created a new branch uiTests and did changes there and made a pull request.

The PR was there for a while and in the meantime there were some other PRs got merged and came conflicts. I resolved them in my fork and there were two merge commits at the end.

When I check the git log, the top of the list looks like this;

commit 70db4de884d5e4b64ef3a2c903f310c901dd68e2
Merge: ef5dfc2 5b7a827
Author: Padmal
Date:   Sat May 26 18:53:33 2018 +0530

    Merge branch 'CloudyPadmal-uiTest' into uiTests

commit 5b7a827763c35a3605daed6c717004700582eede
Merge: e815867 ef5dfc2
Author: Padmal
Date:   Sat May 26 18:52:52 2018 +0530

    Merge branch 'uiTests' of git://github.com/CloudyPadmal/pslab-android into CloudyPadmal-uiTest

commit ef5dfc2f4af7431b2bc5efa356540bd616669706
Author: Padmal
Date:   Thu May 24 20:06:27 2018 +0530

    test: removed Travis build time consuming UI tests

    removed dummy test files

So I wanted to squash 70db4de884d5e4b64ef3a2c903f310c901dd68e2 and 5b7a827763c35a3605daed6c717004700582eede onto ef5dfc2f4af7431b2bc5efa356540bd616669706 and update the PR with one commit.

For more information, git log --oneline results is as follows;

70db4de Merge branch 'CloudyPadmal-uiTest' into uiTests
5b7a827 Merge branch 'uiTests' of git://github.com/CloudyPadmal/pslab-android into CloudyPadmal-uiTest
ef5dfc2 test: removed Travis build time consuming UI tests

But when I tried git rebase -i HEAD~3, the console looks like this;

pick 9b97740 Changed Help and Feedback Activity to Fragment (#882)
pick 7e93db6 chore: Update app version (#922)
pick ca155b6 Added card view for instruments section (#884)
pick e815867 Removed AboutUs activity (#914)
pick ef5dfc2 test: removed Travis build time consuming UI tests

It doesn't show me the merge commits. For more information, following is the version log from Android Studio;

git log view

Am I following the correct method to squash these merge commits? Or is it possible to squash these commits at all? These kind of squashing merge commits been a little problem for me for a while. Thanks a lot for any help and tips! :)

like image 380
Padmal Avatar asked May 26 '18 14:05

Padmal


People also ask

How would you squash multiple commits together using Git merge -- squash?

In case you are using the Tower Git client, using Interactive Rebase to squash some commits is very simple: just select the commits you want to combine, right-click any of them, and select the "Squash Revisions..." option from the contextual menu.

Can you squash merged commits?

To enable commit squashing as the default option in your repository: Navigate to your chosen repository and open the Settings sub-tab. Open the General Settings page. Check the box for Squash commits on merge default enabled.

How do I squash multiple commits without using Git merge?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.


1 Answers

Let me assume that you have an upstream repo branch called "development" and your own branch called "uiTests". And you have created a PR from fork/uiTests to upstream/development (so you have 2 remotes: fork and upstream).

After conflict resolution you have some mess in the log, but you have tested that your working copy state is good, the app works, your changes work, all good, and all you want is to have your work all in 1 commit and update the PR.

One simple solution is to run:

git reset --soft upstream/development

And then re-commit all your changes again:

git add .
git commit

After this you are going to have just 1 commit with your changes on top of the new upstream branch.

Assuming that you do all this in the "uiTests" branch, all you need to update the PR is to:

git push -f fork
like image 131
battlmonstr Avatar answered Oct 19 '22 02:10

battlmonstr