Home git How to rollback a commit?

How to rollback a commit? [duplicate]

Author

Date

Category

This means that I had to go back a few commits (git checkout v2.0 without creating a new branch) to check something in the previous version, then I forgot that I rolled back and made a few changes and committed (git commit -m 'commit' ), now I’m thinking how to cut this last commit?


Answer 1, authority 100%

git revert [commit name] – will roll back changes as the next commit.
That is, a “Mistake” commit has been created, when using revert, another “Revert mistake” commit will be created.


Answer 2, authority 40%

If you still need the changes made in the most recent commit *, then
you can restore it after switching back to the desired branch. Bye
that you need to remember the hash of this commit with the command git show --format =% H HEAD .
Let’s designate its output as $ HASH .

Then just switch back to the previous branch with git chechout - . If you didn’t go where you wanted to, specify your branch explicitly.
Also, you can see a hint from git with a commit hash where you are from
just left with a suggestion to create a new branch pointing to it.

Then apply that commit on top of the new state with git cherry-pick $ HASH . As a result, your changes should be in
branch you need.

Same description, but as a shell script.

HASH = $ (git show --format =% H HEAD)
git checkout -
git cherry-pick $ HASH

* translation the words commit in the context of IT

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions