Merge multiple Git commits into one

Nov 15, 2022 · 1 min read · Post a comment

Merging or if you will, squashing N commits into a single one could be a handy practice though before pushing anything to remote. This is done with the almighty interactive rebase Git feature in five steps or so.


  • Git


Step 1. Checkout the branch you want to do the merging from.

git checkout <some_feat_branch_name>

Step 2. Once you decide on which commits you want to combine, run the following command:

git rebase -i HEAD~<number_of_N_last_commits>


  • Your default text editor (Nano, Vim, Emacs lol, etc.) will open with the metadata of the last N number of commits as the result of the interactive rebasing.
  • If you wish to go to a certain commit, find the commit ID (SHA1) first and then run: git rebase -i <commit-SHA1>.

Step 3. Replace pick with squash. For instance:

pick c3a51d6 fix: Update dockerfile port
squash 2569960 feat: Add .dockerignore file
squash bb612fa feat: Update file

Step 4. Save it. Next, you’ll be prompted to update the commit messages as well. Whatever you choose to do so or not, save the file and exit.

Related: Git: update commit before and after push.

Step 5. Push changes to remote.

git push --force-with-lease origin HEAD

Note(s): In case you can’t push to remote origin for any reason, try replacing --force-with-lease with just --force. Keep in mind that the former is recommended safer option than the latter one.


To find more neat Git commands and hacks, simply browse the Git category. Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.