The most flexible way to do this is to do an interactive rebase to squash together or amend your commits. You need to be careful not to rewrite any history that's already public, however. For example, if you're working on the master
branch, and you want to rewrite and linearize all your commits that aren't in origin/master
(which typically means the work that you haven't pushed yet) you can do:
git rebase -i origin/master
That will launch a text editor where each commit you have that isn't in origin/master is shown, from oldest to newest. If you want want to combine any commit with the previous one, you can just change the start of the line from pick
to squash
. Or, if you want to amend that commit somehow, you can change pick
to edit
.
Using interactive rebase is the most flexible way of doing what you ask for, and it's a very useful tool to get to know, but in the exact situation you describe, it's probably overkill. First, check that the output of git status
is clean, so that you're sure that your staging area represents the same state as HEAD
. You can then just do a "soft reset" back to 0
, which resets HEAD
to that commit, but leaves your working tree and index (i.e. the staging area) intact. If you then do a commit, your index will be used to create a new commit with the same state as 6
, but with 0
as a parent. i.e.
git status # Check that your status is clean
git reset --soft 0
git commit -m 'Work that is totally flawless'