Skip to content

Instantly share code, notes, and snippets.

@kirley
Forked from annaguidi/squash.md
Created November 11, 2016 15:23
Show Gist options
  • Save kirley/f22db04dddfcf30e8cf869c8e4a9c2eb to your computer and use it in GitHub Desktop.
Save kirley/f22db04dddfcf30e8cf869c8e4a9c2eb to your computer and use it in GitHub Desktop.
Phil's squash recipe

Step 1. Rebase

Before you run the command below, make sure you are in the right branch and have committed and pushed your most recent changes.

First, get the most recent version of master:

git fetch origin

Next, rebase:

git rebase origin/master

There might be some conflicts that you need to resolve before you move on.

Step 2. Squash

git rebase -i HEAD~n

Where n is the number of commits for that branch.

This step is faster to do if you associate a text editor with git: https://help.github.com/articles/associating-text-editors-with-git/

Say you did a total of 3 commits. You're going to see something like this:

pick aaa1111 A first commit
pick bbb2222 A second commit
pick ccc3333 A third commit

You're going to want to substitute all pick's EXCEPT the one of the first commit with an s for squash:

pick aaa1111 A first commit
s bbb2222 A second commit
s ccc3333 A third commit

boom! Save and close window (if you're working in a text editor). Next, you're going to see all of your previous commits with their respective messages. Feel free to just delete all of them except the first commit and its message, which you can modify if you want to. Save and close window.

Step 3. Push to Github!

git push origin head -f

You're all set.

Do you feel like this takes too long?

Check out autosquash: https://robots.thoughtbot.com/autosquashing-git-commits

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