Revert many commits in git

Logically speaking, the simplest way to revert several commits in git without losing commit history is by creating a new commit that represents the revert.

Note that its NOT a good practice to revert changes that have been pushed upstream if others on the team have already based their work on the changes that were pushed.

That said, the easiest way to revert changes follows three steps
1. Force checkout the commit that has the code you want your head to be at.
2. Commit
3. Push

This way, your commit history retains all the previous changes, but you have still effectively reverted back to an old commit.

Scenario

Lets say you have a branch with commits A -> B -> C -> D -> E.
Right now your HEAD points to E.
You want revert commits C and D and E. In other words, commit B is the commit you want you start over from.

Solution

git checkout -f B -- .
git status # You dont need to do this but its helps to cross check
git commit -m "your commit message"

Ryan Sukale

Ryan is just a regular guy next door trying to manage his life and finances.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *