Squash your last two commits into one commit

To squash your last two commits into one commit, follow these steps:

  • Open your terminal and navigate to your repository.
  • Start an interactive rebase for the last two commits:
git rebase -i HEAD~2

Squash the commits: Your default text editor will open up with a list of commits. It’ll look something like this:

pick abc1234 Your more recent commit message
pick def5678 Your older commit message

To squash the newer commit into the older one, change pick to squash or simply s for the second line (the topmost commit):

pick def5678 Your older commit message
squash abc1234 Your more recent commit message
  • Save and close the editor.
  • Edit the commit message: Once you’ve chosen to squash, your editor will re-open to allow you to combine the commit messages. You can either edit it to a new message, keep one of the existing messages, or create a combination of both. Save and close the editor when you’re finished.
  • Complete the rebase: After you close the editor, Git will squash the two commits into one.
  • Push the changes to the remote repository: Since rebasing rewrites commit history, you’ll need to force push your changes to the remote repository. Be cautious with this, as it can overwrite changes on the remote that you don’t have locally, especially if you’re working with others.
git push origin HEAD:branch_name --force

Laisser un commentaire