Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force a git revert

Tags:

git

My git log looks like this:

commit b265eb43abd7704ed2560cca7c635dbd47b677b0
Date:   Sun Feb 25 10:20:58 2018 -0500

    added C3

commit 536d071bf99059ff8b519ae96ce6e97d92fbc7d7
Date:   Sat Feb 24 20:50:15 2018 -0500

    added B2

commit 6e46e4ff4135b4df55866a5df9af963e3d6ff218
Date:   Sat Feb 24 11:07:58 2018 -0500

    added a

commit 93ad202a08660a62f76496d728a3a89d727350a9
Date:   Sat Feb 24 11:05:56 2018 -0500

    first commit

I want to undo my previous commits and go back to commit 6e46e4ff4135b4df55866a5df9af963e3d6ff218. However, I want to preserve the history and do not want to remove "added C3" and "added B2" from the git log, so that rules out git reset --hard 6e46e4ff4135b4df55866a5df9af963e3d6ff218.

I did some reading online and people say that the perfect tool for this is "git revert". I try "git revert 6e46e4ff4135b4df55866a5df9af963e3d6ff218" and see the following

error: could not revert 6e46e4f... added a
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

I opened up the file I am trying to revert and saw the following:

Test
<<<<<<< HEAD

adding A
adding B
adding C
=======
>>>>>>> parent of 6e46e4f... added a

Someone told me this means I have a "merge conflict". But I do not want to MERGE the current state of the project with the old state of the project - I want to REPLACE the current state of the project with the old state of the project.

Is there any way to simply replace the current state of the project with an old commit, and have that be the new commit? I could do this manually by doing 1) git checkout , 2) copying the checked out project to a backup directory, 3) doing git checkout master in the main folder, 3) deleting all the files in the main folder replacing them with the backup of the old commit. But I would prefer to learn how to do it the git way. I would also prefer to avoid a branching solution, because I want to learn to use "git revert" - if it can't be used in this very basic, straightforward, canonical example, what could it possibly be used for?

like image 743
john smith Avatar asked Jun 09 '26 09:06

john smith


1 Answers

Git Revert Commit: Undo 1 commit:

$ git reset --hard HEAD~1

OR

$ git reset --hard COMMIT

Remove last commit:

$ git push -f

This will destroy any local modifications. Don't do it if you have uncommitted work you want to keep.

$ git reset --hard COMMIT
like image 75
Cubiczx Avatar answered Jun 10 '26 23:06

Cubiczx