c0D3l0g1c
c0D3l0g1c

Reputation: 3164

Git Revert Remote Branch

I am stuck with this problem: there have been a few commits, including some merged commits which have corrupted the remote development branch.

I have the commit where everything was fine. I want to revert all changes post this commit. Effectively, I want the commit where everything was fine to be the active branch locally and remotely.

Upvotes: 2

Views: 1320

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

The safest way to approach this would be to git revert the bad commits locally, and then just push your branch to the remote as your normally would. Because you have merge commits in there, you will probably have to handle each revert commit one-by-one. For non merge commits, just use this:

git revert <SHA-1>

where you would replace <SHA-1> with the actual hash of the commit you want to revert. For merge commits, it gets trickier, because you need to tell Git which of the two parents paths you want to follow. You will be doing one of the following two options:

git revert <SHA-1> -m 1
git revert <SHA-1> -m 2

The -m 1 option tells Git to follow the path of the first parent, and the -m 2 option tells Git to follow the second parent. To be safe, you should inspect the git log of the branch in question, and find the merge commit which is a candidate for reverting. You should see something like this:

commit 599ee3d46d61d60a7ebdf584b06f78eba647526d
Merge: ece921f 57945bb
Author: Jon Skeet <[email protected]>
Date:   Wed Mar 29 10:01:29 2017 +0000

The ece921f is the first parent, and the 57945bb is the second parent. Decide which one you want, and then revert accordingly. If, for example, you wanted to follow the first parent, you would revert using this:

git revert 599ee3d -m 1

If your branch is not being shared by anyone else, then perhaps the fastest way to get what you want would be to just hard reset your local branch, and then force push it to the remote. Something like this:

git checkout your_branch
git reset --hard HEAD~5    # assuming you want to nuke the last 5 bad commits
git push --force origin your_branch

But this option should only be considered if the branch is not shared with other people, or if it is, with knowledge that doing this force push could cause problems and confusion for others sharing this branch.

Upvotes: 4

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72735

Assume the local branch is X, the remote is origin and the branch there is also X. There are 2 scenarios. The first is if someone has modified origin/X (i.e. the X branch on the remote server) after your push and you care about those modifications. The second is if no one has modified it or you don't care about the modifications.

In the first scenario, origin/X looks something like this

1 - 2 - 3 - 4 - 5 - 6

X currently points to 6, your "good commit" is at 2 and your bad commits are 3 and 4, 5 and 6 are pushed by someone else and are necessary for the project. In this case you can do the following on your own branch

git pull # This will fetch `5` and `6`
git revert `3` `4` # Will apply reverse patches "removing" `3` and `4`

Your tree will be like so now

1 - 2 - 3 - 4 - 5 - 6 - 7 - 8

where 7 is the opposite of 3 and 8 is the opposite of 4. This will change your code to look like it has 1, 2, 5 and 6. Then you just git push origin X your local repo.

In the second scenario, you don't care about 5 and 6 (or they don't exist). This allows you to reset your branch. Simply do

 git reset --hard `2` 
 git push --force origin X

And you'll be done. The extra commits after 2 will be completely destroyed.

Upvotes: 0

Sajib Khan
Sajib Khan

Reputation: 24136

I have the commit where everything was fine. I want to revert all changes post this commit.

Revert all the commits after the working-commit. You can revert a range of commit like:

$ git revert <fromCommit>..<toCommit>

$ git add .
$ git commit -m 'Revert some commits'
$ git push origin HEAD 

Upvotes: 0

Related Questions