I want to combine my last two commits. Here is the result of git log
:
Now I want to merge those last two commits (which are into read box) into one commit. How can I do that?
There are many ways to do it (using rebase
or reset
).
The approach using git reset
:
git status
to find out. If there are uncommitted changes either save them in a stash (git stash
or commit them on a temporary branch.git branch backup
to create a backup branch on the current commit.reflog
or by writing down the hash of the current commit) but it is the easiest way to restore the current status if something goes wrong.git reset --soft HEAD~2
.HEAD
two commits in the past (on commit e8de117
) without changing the working tree or the index. The index and the working tree look now like they were just before you created the commit 6aa74e4
. All the files changed in the last two commits will be already added to the index. Because HEAD
is on e8de117
, the next commit will be created on top of e8de117
(it will "replace" commits 6aa74e4
and 77c15d6
).git commit
. You'll have to enter a new commit message.git diff backup
should not report any difference) or if you have changed your mind, run git reset --hard backup
to go back where you started from (actually, right after step #2).backup
branch created at step 2 (git branch -D backup
).