deshmukh deshmukh - 2 months ago 18
Git Question

Completely remove an earlier commit

Consider the following commit history:

* 627f412 (HEAD -> master) final commit
* 1425cf8 added some file
* 2c8bb78 initial commit


The repository is neither pushed not pulled during these commits.

The second commit 1425cf8 is an unnecessary commit. And I want to completely remove that commit.

So, the new log will look like:

* 627f412 (HEAD -> master) final commit
* 2c8bb78 initial commit


How do I achieve that?

Is
git reset --hard HEAD~1
the correct thing to do in this situation?

Answer

Is git reset --hard HEAD~1 the correct thing to do in this situation?

No: git rebase -i 2c8bb78 is.

You can drop the extra commit in the interactive rebase session. (if you want to drop completely what 1425cf8 introduced)
Or you can squash the two commits.

For that, in the interactive session (see "Git Tools - Rewriting History"):

  • add a s for squash in front of 627f412

Note the end result will not be 627f412: the new commit will have a different SHA1, as its content will differ.

Comments