Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save tuanphpvn/a3cc5e35bbcbc3e796c84b54f5f2720a to your computer and use it in GitHub Desktop.

How to use "git rebase" to squash many commits into one

Step 0

Run git log --pretty=oneline to log your commits history, assume after run it, you have this:

aedb68e3f87cf3df01b73a18e71fbcbbb5d30071 commit 3
efd0274052ab449bfe6c6ac64f595b095717d680 commit 2
53ece1e4c61e74a4d44371e2dd3215af3ba3a146 commit 1
6d618ffbb605e686b6f9c4da8c169e8e5d091c9a commit 0

Now you want to squash commit 1, 2, 3 into one commit.

Step 1

  • Run git rebase -i 6d618ffbb605e686b6f9c4da8c169e8e5d091c9a (6d618f... is sha of commit 0).
  • Vim editor will show like below:
pick 53ece1e commit 1
pick efd0274 commit 2
pick aedb68e commit 3

#...

Step 2

  • Press i to make Vim editor change to INSERT mode, then change pick to squash (or s) in front of commit 2 and 3:
pick 53ece1e commit 1
squash efd0274 commit 2
squash aedb68e commit 3

#...

Step 3

  • Press esc to escape INSERT mode.
  • Press :wq to write and quit Vim. The next editor will be shown to edit new comment for the only one commit after squashed.
  • Press i to update new commit comment:
# This is a combination of 3 commits.
commit 1 2 3 --> 1 commit
# This is the 1st commit message:
commit 1

# This is the commit message #2:

commit 2

# This is the commit message #3:

commit 3

#...
  • Press esc then :wq

Step 4

  • Commit 1, 2, 3 should be squash into one commit now. If you run git log --pretty=oneline, you should have:
5392b85dc130433ec9b3d85dab2616efadcb9655 commit 1 2 3 --> 1 commit
6d618ffbb605e686b6f9c4da8c169e8e5d091c9a commit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment