What command is used to squash multiple commits into one?
Posted by LeoRobs
Last Updated: June 23, 2024
To squash multiple commits into one in Git, you typically use the interactive rebase command. Here's how you can do it: 1. Run the following command, specifying how many commits you want to squash (for example, if you want to squash the last 3 commits, you would use 3):
git rebase -i HEAD~3
2. This will open a text editor showing the last 3 commits. The commits will be listed in reverse order (the most recent commit at the top). 3. In the editor, you will see lines starting with pick. Change the word pick to squash (or just s) for the commits you want to squash into the first one. Leave the first commit as pick. For example, if your commits look like this:
pick 1234567 Commit message #3
   pick 89abcde Commit message #2
   pick fedcba9 Commit message #1
You would change it to:
pick 1234567 Commit message #3
   squash 89abcde Commit message #2
   squash fedcba9 Commit message #1
4. Save and close the editor. 5. Another editor window will pop up, allowing you to modify the commit message for the new squashed commit. Edit the message as needed, then save and close. 6. After completing these steps, your specified commits will be squashed into a single commit. Remember to use this only on commits that have not been shared with others, as rewriting commit history can lead to problems for collaborators.
Related Content