Skip to content

Instantly share code, notes, and snippets.

@gembin
Last active October 2, 2020 15:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gembin/e0fd1e176f978ecbf1da5baef26f5b2c to your computer and use it in GitHub Desktop.
Save gembin/e0fd1e176f978ecbf1da5baef26f5b2c to your computer and use it in GitHub Desktop.
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