Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git: redo an old commit only

Tags:

git

revert

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?

like image 783
Peregring-lk Avatar asked Oct 19 '25 07:10

Peregring-lk


2 Answers

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.

like image 166
Romain Valeri Avatar answered Oct 20 '25 22:10

Romain Valeri


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>

like image 45
Gonzalo Matheu Avatar answered Oct 20 '25 20:10

Gonzalo Matheu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!