ABu
ABu

Reputation: 12299

git: redo an old commit only

I've a project under git where the last commit has correct changes, but the next-to-last commit must be completely reverted, so the applied changes from the third-to-last to next-to-last must be "ignored".

The last commit and the next-to-last commit has no modified files in common.

How can I do that?

Upvotes: 4

Views: 1869

Answers (2)

Romain Valeri
Romain Valeri

Reputation: 22047

The typical way to do this is

git revert HEAD^
# HEAD points to the last commit, so HEAD^ is the commit just before

It will create a new commit on top of the branch tip, containing the reverse changes that your faulty commit contains.

Note : this does not rewrite your history, no need to --force anything when you'll need to push.

Upvotes: 5

Gonzalo Matheu
Gonzalo Matheu

Reputation: 10074

If you are in a branch you could use cherry-pick.

That is:

  • Create a new branch from the last good commit (e.g: master)

  • Then cherry-pick the last commit: git cherry-pick <last-commit-sha>

Upvotes: 4

Related Questions