Skip to content

Instantly share code, notes, and snippets.

@gringoh
Created March 21, 2024 16:13
Show Gist options
  • Save gringoh/c4e47b0abd29695907a9a5b2bd9efee1 to your computer and use it in GitHub Desktop.
Save gringoh/c4e47b0abd29695907a9a5b2bd9efee1 to your computer and use it in GitHub Desktop.
[Git: Squash multiple commits into one] #git

Squash multiple commits into one

To squash multiple commits into one, you can use Git's interactive rebase feature. Here's how you can do it:

  • Open your terminal or command prompt.
  • Navigate to your Git repository using the cd command if you're not already in the repository.
  • Run the following command to start an interactive rebase:
git rebase -i HEAD~9
  • Replace 9 with the number of commits you want to squash. This command will open a text editor with a list of the last 9 commits (in this case).
  • In the text editor that opens, you'll see something like this:
pick <commit-hash-1> Commit message 1
pick <commit-hash-2> Commit message 2
...
pick <commit-hash-9> Commit message 9
  • To squash commits, change the word pick to squash (or just s) for all but the first commit. Leave the first commit as pick.
pick <commit-hash-1> Commit message 1
squash <commit-hash-2> Commit message 2
squash <commit-hash-3> Commit message 3
...
squash <commit-hash-9> Commit message 9
  • Save and close the editor.
  • Git will then open another editor window allowing you to edit the commit message for the squashed commits. You can keep the message of the first commit or modify it as you like. Once you're done, save and close the editor.
  • Git will squash the commits and apply the changes. You might encounter merge conflicts during this process that you'll need to resolve.
  • After resolving conflicts (if any), you may need to force push the changes to your branch:
git push origin <branch-name> --force
  • Replace with the name of your branch.
  • hat's it! Your commits are now squashed into one. Make sure to communicate with your team if you're working in a collaborative environment before force pushing changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment