Skip to content

Instantly share code, notes, and snippets.

@janjongboom
Last active December 26, 2015 18:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janjongboom/7194742 to your computer and use it in GitHub Desktop.
Save janjongboom/7194742 to your computer and use it in GitHub Desktop.
Squash commits in git

This is a short guide on how to get your changes into gaia (or any other repo that has these rules). What we need before we can merge something is:

  • One single commit
  • Commit message: Bug 123456 - Good explanation of the change

So let's say you have the following commits in your pull request:

yourname	first try at something	5cf862e
yourname	moar of the same    	f3f6d47
yourname	fixing setting	        c73352c
yourname	fixing setting	        abd74fb
yourname	fixing setting	        da7c9ef

In your shell now do (replace ~5 by the number of commits in this PR):

git rebase -i HEAD~5

You'll get into a screen that says something like:

pick a1f2439 bla
pick 58c9f68 zus

Change all pick's into s (from squash), except the first one. So it'll say:

pick a1f2439 bla
s 58c9f68 zus

Now save and close the file (:wq in VIM if that's your default editor in git). And you'll get into a second screen:

# This is a combination of 2 commits.
# The first commit's message is:

bla

# This is the 2nd commit message:

zus

Comment the uncommented lines, and make a new line where you put your real commit message:

Bug 123456 - Some meaningful message

# This is a combination of 2 commits.
# The first commit's message is:

#bla

# This is the 2nd commit message:

#zus

Now you have one nice commit! Good for you! If you want to update the date/time to current moment (instead of moment of first commit), run:

git commit --amend --date="$(date -R)"

Almost there. Make sure you rebase against master, so builds don't fail on already fixed tests:

git fetch upstream
git rebase upstream/master

# No conflicts? Great! Otherwise, fix them, and then run:
git rebase --continue

Done. And now do a force push to your branch to update it on GitHub:

git push origin mybranch -f

Appending to your PR

Let's say all wasn't well and you need to make a change:

git add some-file                 # like you'd do normally
git commit --amend                # add to previous commit
git push origin mybranch -f       # yay force push

Merging master

Simple rule. Don't merge master. Always rebase against upstream/master. If you have merged master at one point, you can still do the interactive rebase, but you'll get a way longer list of commits. Just change everything from pick to squash and all will be well. It might be that the commit author is wrong. In that case, run after the rebase:

git commit --amend --author "New Author Name <email@address.com>" 
# dont forget to force push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment