Darren Sim Darren Sim - 6 months ago 41
Git Question

Git Squash all commits in origin/master into single commit within origin/master

I was playing around with my git repository and made quite a number of commits earlier on to master branch. Now I realised that it is a bit too noisy, and will like to merge all of that into a single commit.

102381 commit z
176232 commit a
891273 initial commit

to simply

198273 initial commit (after rebase)
891273 initial commit

I have looked around and many of the solutions seems to be around squashing. Unfortunately that might be possible given that everything is on the master branch already.

One of my initial thoughts was to

  1. create a new branch from commit #891273

  2. squash everything from master into the new branch

  3. delete the old master, and replace it with this new branch

I am not sure if this might be a good way to do it; thought it will be wiser to get some ideas, guidance and discussions about it.


Find an easy way to achieve your requirement:

  1. git checkout –b branch1. Create a new branch from commit #891273,

  2. git rebase –i master

  3. waiting for editor to input (type i to start input):

    pick 176232 commit a

    squash ****** commit b


    squash 102381 commit z

note: type ‘Esc’ button to stop input

  1. input :wq to exit the editor, then you will find what you want

Note: when squashing commit a to z, it may have conflict. You need to fix the conflicted file, and use git add filename, git rebase --continue to continue squashing

Reference: https://makandracards.com/makandra/527-squash-several-git-commits-into-a-single-commit