Home > database >  How to push "undo pushed commit" using command line?
How to push "undo pushed commit" using command line?

Time:03-16

EDIT: Ok so I work it out. The problem is how I stated the branch name. It's supposed to be git push -f origin card-editor instead of git push -f origin/card-editor. Thanks.


The majority of time I work with git, I use sourcetree. Except when "major disaster" things happened. This is one of the situation (for me). Long story short, I do a commit and push, but now I want to undo it on the local, as well as undo it on the remote.

I follow tutorial from here. I successfully reset --hard the head to the last good commit. Now it's just the matter of pushing it to the server.

The way the tutorial does it looks very simple.

git push -f branchname

My feature branch remote name is "card-editor". So I did it.

git push -f card-editor

But I got error that says "card-editor" does not appear to be a git repository.

I listed my list of branch with git branch -r.

origin/HEAD -> origin/master
origin/card-editor
origin/development
origin/staging
origin/production

Alright, so I think the branch name is origin/card-editor. So I retry it.

git push -f origin/card-editor

But it still showing "origin/card-editor" does not appear to be a git repository.

What's wrong with my approach? I don't think there's something special that needs to be done here? I'm not particularly versed with Git, especially with console command, as usually I use sourcetree to manage it.

CodePudding user response:

If you have really already pushed the unwanted commit to the remote, then the safest approach here would be to revert the commit:

# assuming the unwanted commit is on the top of the branch
git revert HEAD
git push origin master

The command git revert adds a new commit which functionally undoes whatever change your commit introduced. This approach is safer than git reset because the latter rewrites history, and so is not suitable once the commit has already been pushed to the remote.

  •  Tags:  
  • git
  • Related