Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

What is best way to revert commit?

Solved! Go to Solution.

I have all sources of project but main.cpp in one directory and main.cpp in another directory. This caused problem with Doxygen to document all files, and I moved all but main() from main.cpp to another file.

Next I commit and push to github.

But my doxygened source are in one branch, I want merge to doxygen branch from other branches and I must revert commit to merge.

My revert must not only make me previous source state, but also delete commit history to do correct merge.

1 Reply
Community Manager
Message 2 of 2

Re: What is best way to revert commit?

There are a bunch of different ways to revert a commit, depending on what the desired end result is. It sounds like you want to:


  1. Remove the last commit on the current branch from the repository
  2. Revert the state of the local repository to the commit prior to the commit named in item 1
  3. Update the current branch on the `origin` remote to the local state of the current branch




Executing these commands will lose history (though Git does its best to make even that recoverable). Use these commands with caution.


In order to achieve items 1 and 2, you can execute:


git reset --hard HEAD^

In order to achive item 3, you can execute:


git push --force

Any time you use `git push --force` though, you may cause problems for other people working on the same branch because you're rewriting history. So use it with caution as well.


I hope that helps!