语境
我的一个队友错误地将一些提交推送到我们的主要开发分支。我们是一个小型的、并置的团队。我们的远程存储库托管在内部服务器上。
这是我们提交日志的顶部(所有这些提交都已被推送):
$ 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
以继续在“功能分支”中工作。
在这个答案和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