deshmukh
deshmukh

Reputation: 383

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?

Upvotes: 1

Views: 66

Answers (2)

VonC
VonC

Reputation: 1323115

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.

Upvotes: 2

user4780495
user4780495

Reputation: 2912

You can reset (without --hard!) your HEAD to 2c8bb78 and then commit the changes. Or rebase on 2c8bb78 and squash to two other commits (627f412 and 1425cf8).

With reset --hard, you might lose your modifications.

Upvotes: 0

Related Questions