Skip to content

Instantly share code, notes, and snippets.

@alebaffa
Last active November 30, 2023 21:28
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 alebaffa/cd61f177a79a4d857bcc55ba7c535f3a to your computer and use it in GitHub Desktop.
Save alebaffa/cd61f177a79a4d857bcc55ba7c535f3a to your computer and use it in GitHub Desktop.
Git squash alternatives

Git Squash alternatives:

  • git rebase -i HEAD~X - this allows you to merge interactively several commits
    • then fixup OR pick OR reword AND push --force
  • git reset --hard HEAD~X - this rollbacks the index back to X commits (check git status before doing this. Commit first)
    • commit again using just one commit
  • git reset --hard COMMIT_ID - this rollbacks the index back to the commit_id
@duchainer
Copy link

duchainer commented May 26, 2022

Thanks for the reminder about interactive rebasing rebase -i. 😄

A quick tip for the others finding this gist when searching for alternative to git squash:
I would recommend using --keep instead of --hard most of the time, since the first keeps your working directory as-is from what I gather, it does not make you lose any work.

https://git-scm.com/docs/git-reset#Documentation/git-reset.txt---keep

A bit like the difference between --force-with-lease and plain --force, you won't be losing any work when using the first one, worse case, it will prevent the operation if it could be destructive.

https://git-scm.com/docs/git-push#Documentation/git-push.txt---no-force-with-lease

@uyilmaz
Copy link

uyilmaz commented Nov 30, 2023

Does the second method work when changes are already pushed to the remote?

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