简体   繁体   English

如何使用命令行推送“撤消推送提交”?

[英]How to push "undo pushed commit" using command line?

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 .它应该是git push -f origin card-editor而不是git push -f origin/card-editor Thanks.谢谢。


The majority of time I work with git, I use sourcetree.大多数时间我使用 git,我使用 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.我成功地reset --hard到最后一次好的提交。 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".我的功能分支远程名称是“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.但是我收到错误消息说“card-editor”似乎不是 git 存储库。

I listed my list of branch with git branch -r .我用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 .好吧,所以我认为分支名称是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.但它仍然显示“origin/card-editor”似乎不是 git 存储库。

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.我不是特别精通Git,尤其是控制台命令,因为通常我使用sourcetree来管理它。

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.命令git revert添加了一个的提交,它在功能上撤消了您的提交引入的任何更改。 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.这种方法比git reset更安全,因为后者会重写历史记录,因此一旦提交已经被推送到远程,就不适用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM