Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Git merge commits into one

Interactively

git rebase --interactive HEAD~2
pick bf7416a commit-2
pick fbc0ef9 commit-3

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
  • d, drop = remove commit

Edit two lines replace pick with squash or s for short, save and quit (x or wq if use vim)

s bf7416a commit-2
s fbc0ef9 commit-3

Non-interactively

In most cases, you want is just simply merge several recent commits into one.

git reset --soft HEAD~n

Assuming ~n is number of commits to softly un-commit (i.e. ~1, ~2,...)

git reset --soft HEAD~2
git commit --amend

From:

commit-1
commit-2
commit-3

To:

commit-1 # this will include commit-2 and commit-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.