在Git中还原一系列推送合并和提交(不重写历史记录) [英] Reverting a series of pushed merges and commits in Git (without rewriting history)

查看:110
本文介绍了在Git中还原一系列推送合并和提交(不重写历史记录)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文



我的一个队友错误地将一些承诺交给了我们的主要开发分支。我们是一个小型的,并置的团队。我们的远程仓库托管在内部服务器上。



这是我们提交日志的顶部(所有这些提交都已被推送):

  $ git log develop -6 --pretty = oneline --abbrev-commit 
faada93合并分支'develop'< our_repo_path> .git
244d174再次支持类
a97a877删除所有未使用的引用(再次)。
8c29252< our_repo_path> .git
的合并分支'develop'a78b993支持模型&方法 - 产品类型&类别
da8b496已解决JIRA问题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 

然后我开始在开发分支中恢复违规提交。 b

我先试了一下,但没有用(可能是因为有些提交是合并的):

  $ git回复a78b993..HEAD 
错误:樱桃挑选或还原已经在pr progress
提示:尝试git cherry-pick(--continue | --quit | --abort)
致命:恢复失败

所以...我手动还原了每个提交;一个接一个:

  $ git revert -m 1 faada93 
[develop 40965a5]恢复合并分支开发'< our_repo_path> .git
更改了8个文件,167个插入(+),3个删除( - )

$ git还原244d174
[develop 3cebd68]还原 再次支持类
45个文件已更改,557个插入(+),1572个删除( - )
(受影响文件列表)

$ git还原a97a877
错误:无法恢复a97a877 ...删除所有未使用的引用(再次)
提示:解决冲突后,标记修正的路径
提示:用'git add <路径>'或'git rm <路径>'
提示:并用'git commit'提交结果

$ git mergetool
合并:
exampleFile1.cs
exampleFile2。 cs

'exampleFile1.cs'的已删除合并冲突:
{local}:已删除
{remote}:已修改fi le
使用(m)修饰或(d)已选文件,或(a)bort? m

'exampleFile2.cs'的已删除合并冲突:
{local}:已删除
{remote}:已修改文件
使用(m)odified或(d )选择文件,或(a)bort? m

$ git commit -m添加要与下一次提交一起恢复的文件。
[develop 15bc02b]添加文件以便能够恢复下一次提交。
2个文件已更改,239个插入(+)
(此处受影响的文件列表)

$ git revert -m 1 8c29252
#在分支上开发
#你的分支在3次提交之前领先于'origin / develop'。
#(使用git push发布你的本地提交)

#未记录的文件:
#(使用git add< file> ...在什么将提交)

#exampleFile1.cs.orig
#exampleFile2.cs.orig
没有添加提交但未跟踪的文件存在(使用git add跟踪)

$ git还原a78b993
[develop 841e77c]还原支持模型和方法 - 产品类型和类别
2个文件已更改,239个删除( - )
(此处受影响的文件列表)

完成所有还原后提交日志: p>

  $ git log develop -10 --pretty = oneline --abbrev-commit 
841e77c恢复Support models& methods - 产品类型和类别
15bc02b添加文件以便能够恢复下一次提交。
3cebd68还原支持类
40965a5还原< our_repo_path> .git的合并分支'开发'
faada93< our_repo_path> .git $ b的合并分支'develop' $ b 244d174再次支持类
a97a877删除所有未使用的引用(再次)。
8c29252< our_repo_path> .git
的合并分支'develop'a78b993支持模型&方法 - 产品类型&类别
da8b496已解决JIRA问题PPF-182

回复后的图表:

  $ git log --graph --oneline -8 develop 
* 841e77c恢复Support models& methods - products types& categories
* 15bc02b添加文件以便能够恢复下一次提交。
* 3cebd68还原支持类
* 40965a5还原< our_repo_path> .git的合并分支'develop'
* faada93合并分支'our''our_repo_path> ;. git
| \
| * a97a877删除所有未使用的引用(再次)。
| * 8c29252合并分支'develop'of< our_repo_path> .git
| | \
| | * da8b496已解决JIRA问题PPF-182

似乎对我很正确。最后,我删除了一些我不想保留的备份文件:

  $ git clean -fd 

目前的状态是干净的:

  $ git status 
#在分支上开发
#你的分支在4次提交之前领先于'origin / develop'。
#(使用git push发布你的本地提交)

没有提交,工作目录干净

然后我把所有东西都推回到遥控器:

  git push origin develop 


解决方案

不是问题。使提交历史看起来应该是这样的:

  git分支-f开发dab4896 
git分支newfeature 8c29252
git push -f origin开发新功能

并让每个人都可以重新读取。你完成了。



这种错误是重写的原因之一。


Context

One of my teammates mistakenly pushed some commits to our main development branch. We're a small, collocated team. Our remote repository is hosted on an internal server.

Here is the top of our commit log (all these commits have already been pushed):

$ 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 is the last commit we wanted to keep in our develop branch, so we needed to revert the 5 last commits. We created a new branch from 8c29252 to continue work in a "feature branch."

I tried many things, guided by this answer and this post from Linus, and ended up doing what you can see in my Terminal history below. But I'm not sure if what I ended up doing is "the right way." The information I found was complex; I was unable to discern a "best solution" for this particular problem.

Question

Was the approach I chose (see details below) a good way to revert those 5 commits, without harming our history? Is there an easier or "more correct" way to accomplish the same thing?

Amongst other things, I considered creating a new branch from da8b496 (git checkout -b new-develop da8b496) and abandoning our current develop branch, but that just didn't feel right.


What I ended up doing (details)

First, I created a new branch for the commits a78b993 and 8c29252, because these commits contain work that we want to keep and eventually merge back to our main development branch.

$ git checkout -b new-feature-brach 8c29252

Then I started reverting the offending commits in our development branch.

I tried this first, but it didn't work (likely because some of the commits are merges):

$ 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

So… I manually reverted each commit instead; one by one:

$ 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)

Commit log after all the reverts were done:

$ 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

Graph after reverts:

$ 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

Seems correct to me. Lastly, I remove some backup files that I don't want to keep:

$ git clean -fd
(list of affected files here)

Current status is clean:

$ 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

And then I push everything back to the remote:

git push origin develop

解决方案

You have a small co-located team, so communication isn't a problem. Make the commit history look as it should have looked:

git branch -f develop dab4896
git branch newfeature 8c29252
git push -f origin develop newfeature

and have everyone refetch. You're done.

This kind of mistake is one of the reasons to rewrite.

这篇关于在Git中还原一系列推送合并和提交(不重写历史记录)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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