Git & Github - Overview, Commands
Squash the Last 3 Commits (Replace 3 with ):
-
Start an Interactive Rebase: Run the following command
git rebase -i HEAD~3
- This tells Git to interactively rebase the last 3 commits.
- Edit the Commit List: You will see a list of the last 3 commits in your editor.
pick abc123 Commit message 1 pick def456 Commit message 2 pick ghi789 Commit message 3
- The pick keyword means “keep this commit as is.”
- Modify the List to Squash Commits: Change the second and third commits from pick to squash (or just s):
pick abc123 Commit message 1 squash def456 Commit message 2 squash ghi789 Commit message 3
-
Save and Close the Editor: After modifying, save and close the editor. Git will squash the commits into one.
- Edit the Commit Message: Git will then prompt you to edit the new commit message. You will see something like this:
# This is a combination of 3 commits. # The first commit's message is: Commit message 1 # The following commit messages will also be included: Commit message 2 Commit message 3
- Edit this message as desired, keeping only the relevant parts. For example:
Combined commit message
- Save and close the editor.
- Edit this message as desired, keeping only the relevant parts. For example:
-
Finish the Rebase: Git will complete the rebase, and the last 3 commits will be combined into one.
- Pushed commits: If you’ve already pushed these commits to a remote repository (before starting the squash process), use
git push --force
to update the branch after squashing. For example:git push origin branch-name --force
Leave a comment