Skip to content

Instantly share code, notes, and snippets.

@jeffjohnson9046
Created March 24, 2015 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffjohnson9046/57a17a534c7d4829d0b5 to your computer and use it in GitHub Desktop.
Save jeffjohnson9046/57a17a534c7d4829d0b5 to your computer and use it in GitHub Desktop.
Git: Squashing [x] commits into a single commit
# Came from the second answer on here: http://stackoverflow.com/questions/5189560/squash-my-last-x-commits-together-using-git
# but writing it out myself helps get it through the metal plate in my head.
#
# Assumes the following situation:
# You have a feature branch that you're developing against, e.g. feature/diaper-horse.
# You have a remote branch origin/feature/diaper-horse.
# feature/diaper-horse is set up to track origin/feature/diaper-horse
#
# Let's say you have 8 commits total (of your own) for the branch feature/diaper-horse.
# Reset to x - 1, where x is the number of commits you're ahead of the remote branch. Like so:
git reset --soft HEAD~7
# This undoes the commits, but leaves the working directory intact. This means your changes are still in place, just git doesn't know about them.
# Commit just like you normally would:
git commit -am "All of my various commits in one swell foop."
# Now, all of your individual changes are effectively rolled up into one commit. Now push to the remote branch:
git push origin feature/diaper-horse -f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment