Skip to content

Instantly share code, notes, and snippets.

@taichikuji
Last active February 15, 2026 11:53
Show Gist options
  • Select an option

  • Save taichikuji/94fb8a562860c14d15568d2aaa04e4c8 to your computer and use it in GitHub Desktop.

Select an option

Save taichikuji/94fb8a562860c14d15568d2aaa04e4c8 to your computer and use it in GitHub Desktop.
How to Squash Commits in Git (After Push)

Introduction

How to squash commits in git after they have been pushed?

This guide provides a solution for users who need to combine (squash) multiple commits into a single one after they have already been pushed to a remote repository.

Important: Rebasing pushed commits changes the history. Do not follow this guide if you are working on a shared branch that others have pulled from, as it will break their history.

Before anything else, here's the part that I always rely on, so I will just copy and paste it, then explain it;

Best Answer

Squash commits on the remote with:

git rebase -i origin/master~4 master

where ~4 means the last 4 commits.

and then force push with :

git push origin +master

Steps

1. Start an Interactive Rebase

Open your terminal and run the rebase command. Replace N with the number of commits you want to review (e.g., 4 for the last 4 commits).

git rebase -i HEAD~N
# Or targeting a specific branch
git rebase -i origin/master

2. Mark Commits to Squash

Your default text editor will open with a list of commits.

  • Keep the first commit as pick.
  • Change the commands for the subsequent commits you want to combine from pick to squash (or s).
pick 1fc6c95 New feature A
squash 6b2481b Fix typo in A
squash dd1475d Fix bug in A
  • Save and close the file.

3. Update Commit Message

A new editor window will open allowing you to modify the commit message for the new combined commit. Edit the message as desired, then save and close.

4. Force Push the Changes

Since you have rewritten the history, a standard push will fail. You must force push the changes to the remote.

# Using + syntax (safer, only forces the specific branch)
git push origin +master

# Or using the force flag
git push --force origin master

Source: StackOverflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment