将推送的分支恢复为具体提交

时间:2012-04-26 09:46:24

标签: git git-reset git-revert

我已将dev分支(具有常量,有时不稳定的更改)合并到我们的主分支(我们存储已发布的稳定代码)。我想将master分支恢复到之前的状态,就像从未发生与dev分支的合并一样(并且在将来我们合并dev分支时,我们现在将丢弃的所有更改将再次“合并”)。

这是主分支的当前状态,我希望它在HEAD上有'professional-1.1.2'提交/标记。

status of the master branch

我试过了:

$ git revert -n professional-1.1.2..HEAD
fatal: Commit 9167e846a387c793edbc089c7ab6bd9eb8260456 is a merge but no -m option was given.
$ git revert -n -m 1 professional-1.1.2..HEAD
fatal: Mainline was specified but commit 380169097f35d07466640bc0db1b639278cd25fa is not a merge.
$ git revert -n -m 2 professional-1.1.2..HEAD
fatal: Mainline was specified but commit 380169097f35d07466640bc0db1b639278cd25fa is not a merge.

经过一些研究后,我认为更好的选择是git reset --hard professional-1.1.2git push --force作为Git: How to ignore fast forward and revert origin [branch] to earlier commit?reverting push'd git commit的答案。其他开发人员在同一个办公室,他们永远不应该承诺任何东西(因为我不应该,但是......是的,我们没有每个分支的权限),所以告诉他们并做任何事都不是一个大问题需要采取行动。

所以最后问题是:git revert somethinggit reset --hard <TAG>&amp;&amp; git push --force?如果git revert,我应该使用哪个命令行?

3 个答案:

答案 0 :(得分:6)

-m number选项指定要还原到哪个父项(因为合并有多个父项)。

所以你想git revert -m 1 HEADgit revert -m 1 SHA_OF_MERGE_COMMIT(假设你做git checkout master; git merge devel;

答案 1 :(得分:6)

如果你只想使master的状态与professional-1.1.2完全相同,同时避免重写历史记录和强制推送,你可以在master上创建一个代表与professional-1.1.2相同的项目状态。您可以通过以下步骤完成此操作:

# Check that "git status" is clean, since the steps that follow will throw
# way uncommitted changes:
git status

# Set the index (staging area) to be as it was at professional-1.1.2:
git read-tree professional-1.1.2

# Create a commit based on that index:
git commit -m "Reverting to the state at professional-1.1.2"

# Your working tree will still be as it was when you started, so
# you'll want to reset that to the new commit:
git reset --hard

作为替代方案,您可以按照this answer by Charles Bailey中建议的步骤执行相同的操作,但是我认为(尽管我建议的步骤涉及“plumbing”命令{ {3}})。

答案 2 :(得分:2)

如果你是一个胆大妄为(从上游的rebase恢复可能是所有其他提交者必需的)

git checkout yourbranch
git reset HEAD <commit-hash>
git push origin yourbranch -f