git 如何删除推送到远程存储库的提交?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12305668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:28:02  来源:igfitidea点击:

How to delete commit that is pushed to the remote repository?

git

提问by user1327064

A local branch:-

当地分行:-

'feature/100'

'功能/100'

And Remote branches:-

和远程分支:-

'master'

'Version2'

'掌握'

'版本2'

Accidently,

一不小心,

  1. I have merged my feature branch 'feature/100' to the master
  2. Also pushed it to the remote repository.
  1. 我已将我的功能分支 'feature/100' 合并到 master
  2. 还将其推送到远程存储库。

But in real 'feature/100' should have been merged into remote branch 'Version2'

但在真正的“功能/ 100”应该已经合并到远程分支“版本2

How I fixed it (partially):-

我是如何修复它的(部分):-

i have merged the feature branch 'feature/100' to the remote branch 'Version2' and pushed it to the server.

我已将功能分支“ feature/100”合并到远程分支“ Version2”并将其推送到服务器。

git checkout Version2
git merge --squash feature/100
git add .
git commit -m 'New message'

But I want to delete the last push I have merged and commit to the master branch. How?

但是我想删除我合并并提交到主分支的最后一个推送。如何?

Side NoteI am only one working on this project.. So even if pushed commit is deleted it won't harm anyone else

旁注我只是一个在这个项目上工作的人..所以即使推送提交被删除它也不会伤害其他人

回答by austen

You can either:

您可以:

Revert your change

还原您的更改

git revert HEAD

git revert HEAD

This will create a new commit that reverts the changes that you just pushed up to master. This is the safest option because other people may have already pulled down the change that you have pushed up.

这将创建一个新的提交,用于还原您刚刚推送到 master 的更改。这是最安全的选择,因为其他人可能已经取消了您提出的更改。

Change your commit history and force push the change

更改您的提交历史并强制推送更改

You can remove the commit that you just pushed up with:

您可以删除刚刚推送的提交:

git reset --hard HEAD~1

git reset --hard HEAD~1

git push origin master --force

git push origin master --force

You don't want to do this unless you're absolutely sure that no one has pulled down your changes from master.

除非您绝对确定没有人从 master 撤下您的更改,否则您不会想要这样做。

For more info, see Delete commits from a branch in Git

有关更多信息,请参阅从 Git 中的分支删除提交

回答by robinst

You can revert the merge commit on master. When you later really want to merge the branch, you will have to revert the revert first. For a detailed explanation, see here:

您可以在 master 上恢复合并提交。当您以后真的想合并分支时,您将必须先还原还原。有关详细说明,请参见此处:

http://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.txt

http://www.kernel.org/pub/software/scm/git/docs/howto/revert-a-faulty-merge.txt

回答by knittl

It's probably too late, but if you want to rewind your pushed branch (master?) by one commit, issue the following command:

现在可能为时已晚,但如果您想将推送的分支 ( master?)回滚一次,请发出以下命令:

git push origin +master^:master

+makes the push forced, master^describes the previous-last commit. :masterpushes to the remote master branch.

+强制推送,master^描述上一次提交。:master推送到远程主分支。