Git. Most used commands

It happens at least once to any developer. Like any motocyrclist drops his/her bike at some point.

Imagine, you merged your awesome code into the master branch - life is wonderful…

At some point you’re noticing that something is wrong - you’re application is not behaving as expected. You have to make a decision as soon as possible either you push a hotfix or revert your merge immediately.

How to revert a single merge in Git

If you decide to revert your changes then this the flow that you need to follow

First, create a new branch/pr from master.

Run the command git log and find the hash of the merge commit.

The next step is

git revert -m 1 <hash>

Replace <hash> with the hash of the merge commit.

After that - add/commit/push.

Get all approvals that are required and merge into the master branch.

Now your code is in the same state as it was before your initial merge.

How to revert multiple merges in Git

What if you decided to push a hotfix or two and all your new merges didn’t fix the problem and now you need to revert all your merges?

The process is almost same:

Create a new branch/pr

Get a commit hash from the most recent merge from git log

Revert the first merge with

git revert -m 1 <hash>

Get the commit hash from the next most recent merge from git log

Revert the next merge git revert -m 1 <hash>

Repeat reverting merges as many as you need.

After that - add/commit/push.

Get all approvals that are required and merge into the master branch.

Done.


You may also find these posts interesting: