在Git中回滚 [英] Rolling back in Git

查看:81
本文介绍了在Git中回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经承诺并推出了几个补丁:
A1 - > A2 - > A3 - > A4(HEAD)

这些变化集合到他们的本地副本中。



现在我们要回滚到A2,并从那里继续开发 - 基本上扔掉A3和A4。

解决方案

从您的工作副本的根目录中执行

  git checkout A2  - 。 
git commit -m'返回A2'






使用 git revert 为这个目的会很麻烦,因为你想摆脱一系列的提交和 revert 时间。



您不需要 git reset 。这只会改变你的 master 分支指针:你没有记录错误的方向。协调也是一件痛苦的事情:因为您将 master 更改为不是远程仓库 master 的子项,分支指针,推送将失败 - 除非在远程仓库中添加 -f (强制)或删除 master 分支首先通过推送重新创建它。但是,那么每个试图拉的人都会在他们的本地 master 分支中保留旧的历史记录,所以一旦 origin / master 不同的是, git pull 会尝试执行合并。这不是世界的终结:他们可以通过执行 git rebase --to origin / master $ old_origin_master_commit master 来摆脱这种情况(也就是将他们的本地提交在旧的 origin / master 之上放置在新的 origin / master 的顶部)。但Git不会自动执行此操作,因此您必须与每个协作者协调。总之,不要那样做。


I have committed, and pushed, several patches: A1-->A2-->A3-->A4 (HEAD)

Everyone's pulled these changesets into their local copy.

Now we want to "roll back" to A2, and continue developing from there - essentially throwing away A3 and A4. What's the best way to do this?

解决方案

From the root directory of your working copy just do

git checkout A2 -- .  
git commit -m 'going back to A2'


Using git revert for this purpose would be cumbersome, since you want to get rid of a whole series of commits and revert undoes them one at a time.

You do not want git reset either. That will merely change your master branch pointer: you are left with no record of the mistaken direction. It is also a pain to coordinate: since the commit you changed master to is not a child of the remote repository’s master branch pointer, pushing will fail – unless you add -f (force) or delete the master branch in the remote repository first and recreate it by pushing. But then everyone who tries to pull will still have the old history in their local master branch, so once origin/master diverges, git pull will try to perform a merge. This is not the end of the world: they can get out of this situation by doing git rebase --onto origin/master $old_origin_master_commit master (ie. rebase their local commits made on top of the old origin/master onto the top of the new origin/master). But Git will not know to do this automatically so you have to coordinate with every collaborator. In short, don’t do that.

这篇关于在Git中回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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