Skip to content

Instantly share code, notes, and snippets.

@bruno-uy
Last active March 23, 2023 02:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bruno-uy/d4f1beac73f2de87d8082524963c071b to your computer and use it in GitHub Desktop.
Save bruno-uy/d4f1beac73f2de87d8082524963c071b to your computer and use it in GitHub Desktop.
Squash git commits into one before pushing to origin

Squash commits

Definition: combine multiple commits into one. More related to get tidy commits than a technical problem about not doing that.

You need to first figure out how many commits do you have to squash. To check that you can use:

git log

Imagine you wanna combine the last 3 commits into one. You'll do a soft reset from HEAD minus 3 commits:

git reset --soft HEAD~3

After that you have all the files staged to commit (added like if you ran git add ...), you need to do the combined commit:

git commit -m 'New message for the combined commit'

Now you have all the commits in one and you can push it to origin.

If you already created a pull request for example, you will have conflicts, so you may need to force push the commit using:

git push --force-with-lease
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment