Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Revert a commit on remote branch

Tags:

I've worked on a local branch adding many commits.
Then i've pushed it to the remote staging branch.

Now I have to undo the last commit already pushed to remote staging that is the merge of my local branch to remote staging

What I've understood looking on the other answers is that I have to use revert and not reset to do it in a clean way, isn't it?

So what I have to do is:

  1. create a new local branch called for example cleaning having master as parent (that is behind staging)
  2. pull the remote staging into cleaning
  3. use git revert {last good commit hash in staging}
  4. now cleaning should be in the good commit and in the same state of remote staging before my bad push, isn't it?
  5. now I should push cleaning into remote staging to having the remote branch reverted. With which flags?

Am I correct? Because git status being at point 4 tells me that i'm up to date with staging

like image 668
lellefood Avatar asked May 22 '18 17:05

lellefood


People also ask

How do you revert a commit on a remote branch?

When you have pushed some commits to the remote repository and would like to undo those changes, you need to use the revert command to create a new commit, undoing all those changes. Note that history will not be rewritten in case of a revert.


1 Answers

Don't make it complicated.

First you need to do a git log to find out which commit ID you want to revert. For example it is commit abc123. If you know that it's the last one, you can use a special identifier "HEAD".

Then you first revert it locally in your local "staging" branch:

git checkout staging git revert abc123 

Note: for the last commit in the log you would write git revert HEAD.

And then you update your remote "staging":

git push 

Explanation: In git if you have a remote you are dealing with 2 distinct repositories (local and remote). After you do git checkout staging, you actually create a distinct local name that represents the remote branch. git revert actually doesn't delete your commit, but it creates a new commit on top, that undoes all the changes (if you added a file - the new commit will remove it, if you removed a line - the new commit will add it back etc.), i.e. it is an addition to the log, that's why the push will be clean.

If you want to really make it gone, so that nobody can blame you, you can do at your risk:

git checkout staging git reset --hard HEAD^ git push -f 

(the reset line retargets the local "staging" branch so that it points to a commit that is right before your top commit)

In general the forced push is a bad practice, but keeping useless commits and reverts around is not nice as well, so another solution would be to actually create a new branch "staging2" and do your testing in that branch instead:

git checkout staging git checkout -b staging2 git reset --hard HEAD^ git push 
like image 87
battlmonstr Avatar answered Sep 20 '22 13:09

battlmonstr