git:分支分叉;如何进行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8716130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git: branches diverged; how to proceed?
提问by sds
My local tree has diverged from the master:
我的本地树与主树不同:
$ git status
# On branch master
# Your branch and 'origin/master' have diverged,
# and have 7 and 3 different commit(s) each, respectively.
#
nothing to commit (working directory clean)
I tried git pull --rebaseand failed:
我试过git pull --rebase并失败了:
$ git pull --rebase
First, rewinding head to replay your work on top of it...
Applying: * ...
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging ChangeLog
CONFLICT (content): Merge conflict in ChangeLog
Failed to merge in the changes.
Patch failed at 0001 * ...
So I reverted with git rebase --abortand I am now at square 1.
所以我用git rebase --abort恢复了,我现在在第 1 方格。
What I want is:
我想要的是:
- "Export" my 7 patches into human readable diff files (a la hg export).
- Make my tree a carbon copy of the origin/master (a la hg strip).
- re-apply my 7 patches one-by-one by hand (a la hg import).
- “导出”我的 7 个补丁到人类可读的差异文件中(a la hg export)。
- 使我的树成为原点/母版的副本(la hg strip)。
- 手动一个一个地重新应用我的 7 个补丁(a la hg import)。
I do understand that git rebase --continuedoes this. I did it and it did work (after a few manual merges and a git add). However, I want to be able to do that manually, so I am wondering what are the gitcommands corresponding to the hgcommands above.
我确实理解git rebase --continue这样做。我做到了,它确实有效(经过几次手动合并和git add)。但是,我希望能够手动执行此操作,因此我想知道与上述hg命令对应的git命令是什么。
Thanks.
谢谢。
PS. Please do not tell me that using a ChangeLogfile with gitis stupid. Even if it is, it is not up to me.
附注。请不要告诉我在git中使用ChangeLog文件是愚蠢的。即使是这样,也不是我的事。
回答by Pat Notz
There are, of course, several ways you could do this manually. You'll still have the same conflicts because git is basically doing this for you under the hood. But if you want to do this manually, here are a couple of ways.
当然,您可以通过多种方式手动执行此操作。你仍然会遇到同样的冲突,因为 git 基本上是在幕后为你做这件事。但是,如果您想手动执行此操作,可以使用以下几种方法。
First, export your commits as a series of patches. The easiest way to do this is using git format-patch
:
首先,将您的提交导出为一系列补丁。最简单的方法是使用git format-patch
:
git format-patch -M @{upstream}
will produce 7 patch files -- one for each of your commits. (Note that "@{upstream}" is literal -- it's a not so well known feature of git.) This is better than capturing the output of git diff
because all of the commit information (author, date, message, etc.) are preserved.
将生成 7 个补丁文件——每个提交一个。(请注意,“@{upstream}”是字面意思——它不是 git 的一个众所周知的特性。)这比捕获输出更好,git diff
因为所有提交信息(作者、日期、消息等)都被保留了.
Then you could reset your repository to match the upstream:
然后您可以重置您的存储库以匹配上游:
git reset --hard @{upstream}
Then you can re-apply your patches using git am
-- either one at a time or all at once.
然后您可以使用git am
- 一次一个或一次全部重新应用您的补丁。
git am 0001-blah-blah.patch
git am 0002-blah-blah.patch
...
A second option would be to create a spare branch with your work on it:
第二种选择是创建一个备用分支,其中包含您的工作:
git branch scrap
Then reset your branch to the upstream:
然后将您的分支重置到上游:
git reset --hard @{upstream}
Then cherry-pick the commits over:
然后挑选提交:
git cherry-pick scrap~6
git cherry-pick scrap~5
git cherry-pick scrap~4
...
Then trash the scrap branch:
然后丢弃废料分支:
git branch -D scrap
回答by Sailesh
Have you tried git merge origin/master
?
你试过git merge origin/master
吗?
Your remote changes are stored in the branch origin/master
. (Or it will, if you do git fetch
.) Just merge the two branches - master
and origin/master
- like any two branches and resolve the conflicts (if any).
您的远程更改存储在 branch 中origin/master
。(或者它会,如果你这样做git fetch
。)只需合并两个分支 -master
并且origin/master
- 就像任何两个分支一样并解决冲突(如果有的话)。
This may help you if you need to know how to resolve git conflicts.
如果您需要知道如何解决 git 冲突,这可能会对您有所帮助。
回答by wilhelmtell
Git says it tried to do exactly what you want (re-apply your patches on top of the newest changes from origin/master) but failed with a conflict. Right after git pull --rebase
conflicts open an editor with the conflicted files (git status
will list these under "both changed") and resolve the conflicts, marked in standard diff lingua. When you're done resolving the conflict throw in a git rebase --continue
(or git rebase --skip
if your resolution introduces no changes).
Git 说它试图完全按照你的意愿去做(在来自 origin/master 的最新更改之上重新应用你的补丁)但因冲突而失败。git pull --rebase
冲突后立即打开带有冲突文件的编辑器(git status
将在“两者都更改”下列出这些文件)并解决冲突,以标准差异语言标记。当您完成解决冲突时,抛出一个git rebase --continue
(或者git rebase --skip
如果您的解决方案没有引入任何更改)。
Read about his at Stackexchange documentation for 'Resolving merge conflicts after a Git rebase'.
阅读他在Stackexchange 文档中的 'Resolving merge conflict after a Git rebase'。
回答by Philipp Cla?en
Here are some good answers to the same problem (only without conflict resolving):
以下是同一问题的一些很好的答案(仅在没有解决冲突的情况下):
master branch and 'origin/master' have diverged, how to 'undiverge' branches'?
master 分支和 'origin/master' 有分歧,如何“undiverge”分支?
First, you may want to review what has been changed on the remote master in comparison to your local version:
首先,您可能需要查看与本地版本相比,远程主服务器上的更改内容:
git log HEAD..origin/master
To fix your actually problem, it boils down to the same line that wilhelmtell proposed:
为了解决您的实际问题,它归结为 wilhelmtell 提出的同一行:
git pull --rebase
As you said, you will get conflicts.
正如你所说,你会遇到冲突。
Conflicts resolving is a recurring problem. If you haven't done it yet, you may take a look at git mergetool
(see git help mergetool
for details). To get graphical support, I would recommend to overwrite the merge.tool
configuration. For example, if you want to use meld
for 3-way merges, you can use:
解决冲突是一个反复出现的问题。如果你还没有这样做,你可以看看git mergetool
(查看git help mergetool
详细信息)。要获得图形支持,我建议覆盖merge.tool
配置。例如,如果要meld
用于 3 路合并,可以使用:
git config --global merge.tool meld
So after you resolved the conflict, what has git pull --rebase
done? It merged all changes from the origin/master into your local master, and replayed your changes on top of it. Congratulations, you are back to normal.
那么在你解决了冲突之后,你git pull --rebase
做了什么?它将来自 origin/master 的所有更改合并到您的本地 master 中,并在其上重放您的更改。恭喜你,你已经恢复正常了。