git: how to squash multiple merge commits
Asked Answered
F

1

8

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! :)

Ferrin answered 26/5, 2018 at 14:1 Comment(0)
S
9

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
Sweptwing answered 26/5, 2018 at 16:23 Comment(1)
This worked! :D I need to dig deep into reset --soft! Thank you! :)Ferrin

© 2022 - 2024 — McMap. All rights reserved.