0

We are 2 developers working on the same branch. Let's say, dev1 working on login and dev2 working on registration.

Here are the commits:

  • dev1 commit 1
  • dev1 commit 2
  • dev2 commit 1
  • dev2 commit 2
  • dev1 commit 3
  • dev2 commit 3

I would like to know if there's a possibility to squash commits and merge them by the user

final commits list:

  • dev1 login (combining dev1's commit 1, 2, 3)
  • dev2 registration (combining dev2's commit 1, 2, 3)
3
  • 1
    This is a pair of rebases, however you want to do them. git rebase -i oldest~.. and pick squash squash the two series, or git reset oldest~; git cherry-pick -n d11 d12 d13; git commit; git cherry-piick -n d21 d22 d23; git commit.
    – jthill
    Commented Sep 22, 2021 at 17:38
  • This answer might be useful stackoverflow.com/a/56262851/12974735
    – Sercan
    Commented Sep 22, 2021 at 19:03
  • @Sercan That thread will squash them to previous commit rather than rearranging them with a new commit.
    – Digvijay
    Commented Sep 23, 2021 at 2:17

1 Answer 1

0

How to rearrange and squash commits

OP: I would like to know if there's a possibility to squash commits and merge them by the user

In order to achieve what you are asking for, the easiest approach I'd say would be to do an interactive rebase, e.g.

$ git rebase master feature-1 --interactive

See the illustration below for a detailed explanation.

Illustration of an interactive rebase

Step by step

  • (1) Select which two branches to involve in the rebase (don't forget the --intertactive flag as it will allow you to rearrange your commits)

  • (2.1) Re-order your commits in the desired order (note that the commits are displayed in reversed order)

  • (2.1) Edit the action infront of the commits to squash from pick to squash or fixup (depending on if you'd like to edit the commit message or not)

  • (3) Save and close the file and watch Git work its magic

Important

As rebase rewrites your history, make sure to take a backup of your branch first. That way you're also able to easily compare the before and after state of the two branches.

Further more, re-arranging commits assumes that the intertwined developers have been working on the same files as this could potentially cause conflicts while rebasing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.