Revert many commits in git

Log­i­cally speak­ing, the sim­plest way to revert sev­eral com­mits in git with­out los­ing com­mit his­tory is by cre­at­ing a new com­mit that rep­re­sents the revert.

Note that its NOT a good prac­tice to revert changes that have been pushed upstream if oth­ers on the team have already based their work on the changes that were pushed.

That said, the eas­i­est way to revert changes fol­lows three steps
1. Force check­out the com­mit that has the code you want your head to be at.
2. Com­mit
3. Push

This way, your com­mit his­tory retains all the pre­vi­ous changes, but you have still effec­tively reverted back to an old commit.

Sce­nario

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

Solu­tion

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 a UX engineer living in San Francisco, California.

You may also like...

Leave a Reply

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