Skip to content

Instantly share code, notes, and snippets.

@russelldb
Forked from jlevon/git-squashing.md
Created June 27, 2019 13:20
Show Gist options
  • Save russelldb/e67c6c989e150473dd7488f1676edaf7 to your computer and use it in GitHub Desktop.
Save russelldb/e67c6c989e150473dd7488f1676edaf7 to your computer and use it in GitHub Desktop.
Forcing git to squash without rebasing

Let's say I have a particular commit (for example, a gerrit patchset), with a whole bunch of merging history. I want a single commit of all its changes against master, without having to walk through a huge rebase of the intermediate commits.

First let's get it as a branch:

~/src/grot/gerrit-pull 6225/9
git checkout -b gerrit

Now, let's start a new branch from the current master:

git checkout master
git checkout -b tmp

The trick now is to totally reset the current staged set of changes to whatever the exact difference between 'tmp' and 'gerrit' is, ignoring all the commit history:

git checkout gerrit .
git commit -m 'your single commit'

Note that "." is necessary: git checkout can mean both "switch branch" as well as "reset the current cached changes". Wonderful stuff there git. This will give us:

* commit 391e1c6cc417f09dab72d841b83d82a96fe877d5 (HEAD -> tmp)
| Author: John Levon <[email protected]>
| Date:   Thu Jun 27 11:43:21 2019 +0000
| 
|     your single commit
|  
* commit f38f9bbd3eb3e1c313f818381d65274846d40485 (master)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment