如何修复错误的合并,并将好的提交重播到固定的合并中? [英] How do you fix a bad merge, and replay your good commits onto a fixed merge?

查看:87
本文介绍了如何修复错误的合并,并将好的提交重播到固定的合并中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不小心将一个不需要的文件(解析合并时为filename.orig)提交到了我的存储库中,但之前并没有注意到它.我想从存储库历史记录中完全删除该文件.

I accidentally committed an unwanted file (filename.orig while resolving a merge) to my repository several commits ago, without me noticing it until now. I want to completely delete the file from the repository history.

是否可以重写更改历史记录,以使filename.orig从未被添加到存储库中?

Is it possible to rewrite the change history such that filename.orig was never added to the repository in the first place?

推荐答案

如果您的情况不是问题中描述的那样,请不要使用此食谱.此食谱用于修复错误的合并,并将好的提交重播到固定的合并中.

尽管filter-branch将执行您想要的操作,但这是一个非常复杂的命令,我可能会选择使用git rebase进行此操作.这可能是个人喜好. filter-branch可以用一个稍微复杂一点的命令来完成它,而rebase解决方案则一次执行等效的逻辑操作.

Although filter-branch will do what you want, it is quite a complex command and I would probably choose to do this with git rebase. It's probably a personal preference. filter-branch can do it in a single, slightly more complex command, whereas the rebase solution is performing the equivalent logical operations one step at a time.

尝试以下食谱:

# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>

# remove the incorrectly added file
git rm somefile.orig

# commit the amended merge
git commit --amend

# go back to the master branch
git checkout master

# replant the master branch onto the corrected merge
git rebase tmpfix

# delete the temporary branch
git branch -d tmpfix

(请注意,您实际上并不需要临时分支,可以使用分离的HEAD"执行此操作,但是需要记录由git commit --amend步骤生成的提交ID,以提供给git rebase命令,而不使用临时分支名称.)

(Note that you don't actually need a temporary branch, you can do this with a 'detached HEAD', but you need to take a note of the commit id generated by the git commit --amend step to supply to the git rebase command rather than using the temporary branch name.)

这篇关于如何修复错误的合并,并将好的提交重播到固定的合并中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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