通过1次提交回滚本地和远程git存储库 [英] Rolling back local and remote git repository by 1 commit

查看:108
本文介绍了通过1次提交回滚本地和远程git存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读关于此主题的类似帖子,并且无法为我的生活弄清楚如何正确执行此操作。



我检查了一下1000个文件,我不想要,我宁愿不必通过1by1并从回购中删除它们。




  • 我有一个远程 主分支。

  • 我有本地的 master Branch。



它们都在同一版本中。



我想通过1次提交回滚远程计算机。



master A - B - C - D - E

我想回滚我的本地到 D

然后将它推到远程,所以我当前的散列将是D和远程和本地。

我遇到了问题。

我使用Git Tower,但对命令行感到满意。任何帮助?



更新:
下面的评论很棒。使用重置似乎部分不鼓励,特别是如果存储库与其他用户共享。
在不使用硬重置的情况下撤消上次提交更改的最佳方式?有没有办法?

解决方案

如果没有人拉你的远程回购,你可以改变你的分支HEAD并强制推送它说远程回购:

  git reset --hard HEAD ^ 
git push -f

(或者,如果您可以直接访问远程仓库,则可以更改其) / p>

请注意,正如外星科技在< a href =https://stackoverflow.com/questions/4647301/rolling-back-local-and-remote-git-repository-by-1-commit/4647362#comment64375482_4647362>下面的评论,关于Windows(CMD会话),你需要 ^^

  git重设 -  hard HEAD ^^ 
git push -f







如果有人已经撤回了回购计划,该怎么办?然后我会做什么?


然后我会建议一些不会重写历史记录的内容:




  • git revert 本地上次提交(创建一个新的提交,以反转前一次提交的内容)

  • 推送由 git revert 产生的'revert'。

I've read the similar posts on this topic, and can't for the life of me figure out how to do this properly.

I checked in about 1000 files that I don't want, and I'd rather not have to go through 1by1 and remove them all from the repo.

  • I have a remote master Branch.
  • I have the local master Branch.

They are both at the same revision.

I want to rollback my remote by 1 commit.

Say my history on master is A--B--C--D--E.
I want to rollback my local to D.
Then push it to remote so my current hash will be D both remote and local.

I'm having issues doing this.
I'm using Git Tower but am comfortable with the command line. Any help?

UPDATE: Great comments below. Using a reset seems to be partially discouraged especially if the repository is shared with other users. What's the best way to undo the previous commit's changes without using a hard reset? Is there a way?

解决方案

If nobody has pulled your remote repo yet, you can change your branch HEAD and force push it to said remote repo:

git reset --hard HEAD^ 
git push -f 

(or, if you have direct access to the remote repo, you can change its HEAD reference even though it is a bare repo)

Note, as commented by alien-technology in the comments below, on Windows (CMD session), you would need ^^:

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


What if somebody has already pulled the repo? What would I do then?

Then I would suggest something that doesn't rewrite the history:

  • git revert locally your last commit (creating a new commit that reverses what the previous commit did)
  • push the 'revert' generated by git revert.

这篇关于通过1次提交回滚本地和远程git存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆