背景
我的一个队友错误地将一些提交推送到了我们的主开发分支。我们是一个小型的面对面团队。我们的远程仓库托管在内部服务器上。
以下是我们提交记录的顶部(所有这些提交已经被推送):
$ 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
git log --graph --oneline develop
的输出。另外,假设faada93
是develop
分支合并的地方,你只需要还原那个提交,所有不属于当前分支的子提交也将被还原,无需还原其他任何提交。你所做的看起来比必要的复杂得多。 - user456814