Its surprisingly easy and not as complicated as some people might say. You just need to know which one is the 'bad' commit, then revert to one commit before the 'bad' one. In this example, person 'M' accidentally pulled from the dev branch instead of main. So to revert this, just follow these steps:
Find the 'bad' commit
In this case it's git message have something like 'pulled dev into branch...'. This is the bad commit, do not copy the git hash of this commit. Instead, get the last 'good' commit. Copy the commit hash of the one before the 'bad' one. Paste it somewhere safe, we will get back to this later. Do not revert right now.
Backup your commits after the 'bad' one
After pulling from dev, 'M' pushed a few more commits afterwards. If we've already reverted, finding the committed changes would be a bit harder. So now is the best chance to copy all the commit hashes for cherry-picking later.
Revert to last 'good' commit
Now that you have all the relevant commit hashes, it's time to revert to before the bad commit happens.
git reset --hard <commit_hash>
Cherry-pick the committed changes
You will notice that your commit history has changed. Bad commit is no more, and now is the time to cherry-pick back all the good commits you saved earlier. Make sure to cherry-pick them in chronological order.
git cherry-pick <commit_hashes>
Push the new commit history
You local branch has been fixed. Next is to push the new commit history using the provided command. This will overwrite the remote branch, ensuring it follows the fixed local branch.
git push --force-with-lease
That's it. Reverting is not that hard as long as you remember to follow the steps. Hope this is as useful to you as it is to me.
Comments
Post a Comment