Skip to content

Instantly share code, notes, and snippets.

@Amit0617
Created May 17, 2022 05:18
Show Gist options
  • Save Amit0617/c44a4bd45abc6daabc85deff526bb00f to your computer and use it in GitHub Desktop.
Save Amit0617/c44a4bd45abc6daabc85deff526bb00f to your computer and use it in GitHub Desktop.
Squash multiple commits from commit history and make them one single commit
git rebase -i HEAD~2    #assumed last two commits need to be squashed

Similarly HEAD~3 for squashing last three and so on As a result vi or your any other text editor opens up top two lines(because we are squashing two commits) will be

pick [commit-sha] [commit-message]
pick [commit-sha] [commit-message] 

There will be more lines below them but all would be commented Replace pick with squash and leave only one pick at the end, so that commit only will be visible and all squash commits will go inside that commit. For example

pick [commit-sha] [commit-message]
squash [commit-sha] [commit-message]

Save and close the editor. Git will again fire up your text editor to change commit message this time. Comment everything and leave or maybe modify the one commit message in the editor and then save and close. Then simply verify if it went the way you want using git log and then git push --force to make similar commit history on remote.

@Amit0617
Copy link
Author

there might be possibility of in between merge conflicts that needs to be resolved but that's separate story.

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