【发布时间】:2013-08-07 14:47:21
【问题描述】:
上下文
我的一个队友错误地将一些提交推送到我们的主要开发分支。我们是一个小型的、并置的团队。我们的远程存储库托管在内部服务器上。
这是我们提交日志的顶部(所有这些提交都已被推送):
$ git log develop -6 --pretty=oneline --abbrev-commit
faada93 Merge branch 'develop' of <our_repo_path>.git
244d174 Support classes again
a97a877 Pruned all unused references (again).
8c29252 Merge branch 'develop' of <our_repo_path>.git
a78b993 Support models & methods - product types & categories
da8b496 Resolved JIRA issue PPF-182
da8b496 是我们想要保留在 develop 分支中的最后一个提交,因此我们需要恢复最后 5 个提交。我们从 8c29252 创建了一个新分支,以继续在“功能分支”中工作。
在this answer 和this post from Linus 的指导下,我尝试了很多事情,最终完成了您可以在下面的终端历史记录中看到的内容。但我不确定我最终做的是否是“正确的方式”。我发现的信息很复杂;对于这个特定问题,我无法找出“最佳解决方案”。
问题
我选择的方法(见下文)是恢复这 5 次提交而不损害我们历史记录的好方法吗?有没有更简单或更“正确”的方法来完成同样的事情?
除此之外,我考虑过从da8b496 (git checkout -b new-develop da8b496) 创建一个新分支并放弃我们当前的develop 分支,但感觉不对。
我最终做了什么(详情)
首先,我为提交 a78b993 和 8c29252 创建了一个新分支,因为这些提交包含我们想要保留并最终合并回我们的主要开发分支的工作。
$ git checkout -b new-feature-brach 8c29252
然后我开始在我们的开发分支中恢复有问题的提交。
我先尝试了这个,但没有成功(可能是因为某些提交是合并):
$ git revert a78b993..HEAD
error: a cherry-pick or revert is already in progress
hint: try "git cherry-pick (--continue | --quit | --abort)"
fatal: revert failed
所以……我改为手动还原每个提交;一一:
$ git revert -m 1 faada93
[develop 40965a5] Revert "Merge branch 'develop' of <our_repo_path>.git"
8 files changed, 167 insertions(+), 3 deletions(-)
$ git revert 244d174
[develop 3cebd68] Revert "Support classes again"
45 files changed, 557 insertions(+), 1572 deletions(-)
(list of affected files)
$ git revert a97a877
error: could not revert a97a877... Pruned all unused references (again).
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ git mergetool
Merging:
exampleFile1.cs
exampleFile2.cs
Deleted merge conflict for 'exampleFile1.cs':
{local}: deleted
{remote}: modified file
Use (m)odified or (d)eleted file, or (a)bort? m
Deleted merge conflict for 'exampleFile2.cs':
{local}: deleted
{remote}: modified file
Use (m)odified or (d)eleted file, or (a)bort? m
$ git commit -m "Adding files to be reverted along with the next commit."
[develop 15bc02b] Adding files to be able to revert the next commit in line.
2 files changed, 239 insertions(+)
(list of affected files here)
$ git revert -m 1 8c29252
# On branch develop
# Your branch is ahead of 'origin/develop' by 3 commits.
# (use "git push" to publish your local commits)
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# exampleFile1.cs.orig
# exampleFile2.cs.orig
nothing added to commit but untracked files present (use "git add" to track)
$ git revert a78b993
[develop 841e77c] Revert "Support models & methods - product types & categories"
2 files changed, 239 deletions(-)
(list of affected files here)
所有还原完成后提交日志:
$ git log develop -10 --pretty=oneline --abbrev-commit
841e77c Revert "Support models & methods - product types & categories"
15bc02b Adding files to be able to revert the next commit in line.
3cebd68 Revert "Support classes again"
40965a5 Revert "Merge branch 'develop' of <our_repo_path>.git"
faada93 Merge branch 'develop' of <our_repo_path>.git
244d174 Support classes again
a97a877 Pruned all unused references (again).
8c29252 Merge branch 'develop' of <our_repo_path>.git
a78b993 Support models & methods - product types & categories
da8b496 Resolved JIRA issue PPF-182
还原后的图表:
$ git log --graph --oneline -8 develop
* 841e77c Revert "Support models & methods - product types & categories"
* 15bc02b Adding files to be able to revert the next commit in line.
* 3cebd68 Revert "Support classes again"
* 40965a5 Revert "Merge branch 'develop' of <our_repo_path>.git"
* faada93 Merge branch 'develop' of <our_repo_path>.git
|\
| * a97a877 Pruned all unused references (again).
| * 8c29252 Merge branch 'develop' of <our_repo_path>.git
| |\
| | * da8b496 Resolved JIRA issue PPF-182
对我来说似乎是正确的。最后,我删除了一些我不想保留的备份文件:
$ git clean -fd
(list of affected files here)
当前状态是干净的:
$ git status
# On branch develop
# Your branch is ahead of 'origin/develop' by 4 commits.
# (use "git push" to publish your local commits)
#
nothing to commit, working directory clean
然后我将所有内容推回遥控器:
git push origin develop
【问题讨论】:
-
请显示
git log --graph --oneline develop的输出。此外,假设faada93是合并develop的位置,您需要做的就是还原一次提交,并且其所有不属于当前分支的子项也将被还原,您不需要恢复任何其他的。你所做的看起来比必要的要复杂得多。 -
谢谢,@Cupcake - 我将图表添加到问题中。我对问题中的哈希值犯了一些令人困惑的错误;我现在会更正它们。
-
这个仓库是公开的,还是一个小团队共享的私有仓库?如果是后一种情况,是否有理由不将分支硬重置到合并前的点?
-
这是一个私有存储库,托管在内部服务器上,由一小群开发人员使用。是否可以使用“硬重置”,而不会对我们的分支历史产生不利影响?我们都是 Git 的新手,只是觉得还原是一种更安全、“更正确”的方式。我们发现的信息充其量是令人困惑的,因此这个问题:)
-
我在Git chat room,如果您想加入我,我们可以实时讨论您的问题。哦,等等,对不起,那个房间真的冻坏了,让我试试别的。
标签: git git-revert