How to revert multiple commits from feature as well as remote branch

Viewed 28

I have a feature branch named 'feature123' and I had four commits and pushes on it as follows.

bad commit4 #abcd4 1 min ago
bad commit3 #abcd3 2 mins ago
bad commit2 #abcd2 3 mins ago
bad commit1 #abcd1 4 mins ago
last good commit #efgh5 5 mins ago

Later it got merged to a remote branch named 'develop' for dev environment. I have to revert all bad commits (keep till last good commit) from develop and 'feature123' branches. Please suggest me a good set of commands.

1 Answers

Do note that some operations can be destructive and should be considered with caution or applied after getting a backup.

Depending on your service provider for the remote, it might be hard to apply the simplest solution:

If nothing valuable has been pushed after these commits, my preferred way is to overwrite the branches as it leaves the tree in a clean state.

git checkout feature123
git reset --hard efgh5
git push --force-with-lease
git checkout develop
git reset --hard efgh5
git push --force-with-lease

If other commits have been pushed that should be kept, of it the service provider won't allow forceful push, you'll want to use git revert command to "additively" rollback your changes. This can also be useful if you think that part of the code you're trying to remove might still be useful, as it will not be removed from tree.

There are many ways to use it more or less interactively and you can use the --help option to learn about them, but in your case I'd simply "unstack" the changes and push.

git revert abcd4
git revert abcd3
...
git revert abcd1
git push
Related