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.
- 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
#...
- Press
i
to make Vim editor change to INSERT mode, then changepick
tosquash
(ors
) in front of commit 2 and 3:
pick 53ece1e commit 1
squash efd0274 commit 2
squash aedb68e commit 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
- 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