Skip to content

Instantly share code, notes, and snippets.

@keernel
Created June 3, 2016 11:57
Show Gist options
  • Select an option

  • Save keernel/7c2d2fefd2d6b7b620f480994f59d94d to your computer and use it in GitHub Desktop.

Select an option

Save keernel/7c2d2fefd2d6b7b620f480994f59d94d to your computer and use it in GitHub Desktop.
Squash git commits
Let's say you have two commits in your branch:
$ git log --oneline origin/master..new-feature
73bbc09 Hack some more
f33b240 Hack hack
What the project maintainer wants is a single commit in the diff between your new-feature branch and the project's master branch. What we need here is the interactive rebase feature of Git:
$ git rebase -i origin/master
This will open your editor with these contents:
pick f33b240 Hack hack
pick 73bbc09 Hack some more
# Rebase e54a9a9..73bbc09 onto e54a9a9
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# ...
Change the pick on the second like to squash (or just s), save the file and exit. You'll then get another editor with a commit message to edit:
# This is a combination of 2 commits.
# The first commit's message is:
Hack hack
# This is the 2nd commit message:
Hack some more
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# ...
This is the time to create a single descriptive commit message for your pull request. Save and exit. The log will now show:
$ git log --oneline origin/master..new-feature
e020524 <the first line of your modified commit message>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment