我在主git分支上创建了一个名为bug10101010的新侧分支,现在我想将bug10101010合并到主分支中。到目前为止一切都很好。 现在我有同一产品的另一个不同分支,名为legacy。 我想将bug10101010合并到GIT的legacy分支中。
有什么想法吗?
我不能直接合并它,因为bug10101010分支是从主分支中衍生出来的,在legacy中我只需要该分支与其父分支之间的差异。
git rebase --onto
,并指定一个范围。(请参阅git rebase
man page: 将基于一个分支的主题分支移植到另一个分支上,以假装你从后者分支分叉了主题分支,使用rebase --onto
.)。bug10
分支移动到legacy
分支的顶部,这不是你想要/需要的。legacy
分支(克隆库中具有 bug10
修改的分支)与本地和当前的 legacy
分支(你想要修改的分支,同时保留 bug10
)合并。我只是想看看它是否可行,所以...让我们测试一下这种方法。
(Git1.6.5.1,在旧XP SP2上,使用PowerShell 1.0会话,因为Start-Transcript
命令)
PS D:\> mkdir git
PS D:\> cd git
PS D:\git> mkdir tests
PS D:\git> cd tests
PS D:\git\tests> git init mainRepo
我很喜欢现在不需要再先创建git仓库目录,然后在其中输入git init
了!自从1.6.5版本:
"
git init
"学会了在给定额外参数时(即"git init this
"),进入一个目录并创建它。
太好了!
让我们为三个不同的目的创建3个文件。
(为了举例说明,我将保持每个分支的文件修改分开:这里不会有合并或变基冲突。)
PS D:\git\tests> cd mainRepo
PS D:\git\tests\mainRepo> echo mainFile > mainFile.txt
PS D:\git\tests\mainRepo> echo contentToBeFixed > toBeFixedFile.txt
PS D:\git\tests\mainRepo> echo legacyContent > legacy.txt
PS D:\git\tests\mainRepo> git add -A
PS D:\git\tests\mainRepo> git ci -m "first commit"
PS D:\git\tests\mainRepo> echo firstMainEvol >> mainFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "first evol, for making 1.0"
PS D:\git\tests\mainRepo> git tag -m "1.0 legacy content" 1.0
此时,运行git log --graph --oneline --branches
命令会返回:
* b68c1f5 first evol, for making 1.0
* 93f9f7c first commit
让我们建立一个遗留分支legacy
PS D:\git\tests\mainRepo> git co -b legacy
PS D:\git\tests\mainRepo> echo aFirstLegacyEvol >> legacy.txt
PS D:\git\tests\mainRepo> git ci -a -m "a first legacy evolution"
我们回到主分支,进行另一次提交,我们将其标记为“2.0”(这是一个需要修复一些错误的版本!)
PS D:\git\tests\mainRepo> git co -b master
PS D:\git\tests\mainRepo> git co master
PS D:\git\tests\mainRepo> echo aMainEvol >> mainFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "a main evol"
PS D:\git\tests\mainRepo> echo aSecondMainEvolFor2.0 >> mainFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "a second evol for 2.0"
PS D:\git\tests\mainRepo> git tag -m "main 2.0 before bugfix" 2.0
* e727105 a second evol for 2.0
* 473d44e a main evol
| * dbcc7aa a first legacy evolution
|/
* b68c1f5 first evol, for making 1.0
* 93f9f7c first commit
现在我们创建一个bug10
的错误修复分支:
PS D:\git\tests\mainRepo> git co -b bug10
PS D:\git\tests\mainRepo> echo aFirstBug10Fix >> toBeFixedFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "a first bug10 fix"
PS D:\git\tests\mainRepo> echo aSecondBug10Fix >> toBeFixedFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "a second bug10 fix"
让我们在主分支上添加最终提交
PS D:\git\tests\mainRepo> git co master
PS D:\git\tests\mainRepo> echo anotherMainEvol >> mainFile.txt
PS D:\git\tests\mainRepo> git ci -a -m "another main evol"
* 55aac85 another main evol
| * 47e6ee1 a second bug10 fix
| * 8183707 a first bug10 fix
|/
* e727105 a second evol for 2.0
* 473d44e a main evol
| * dbcc7aa a first legacy evolution
|/
* b68c1f5 first evol, for making 1.0
* 93f9f7c first commit
git rebase --onto
。PS D:\git\tests\mainRepo> cd ..
PS D:\git\tests> git clone mainRepo rebaseRepo
PS D:\git\tests> cd rebaseRepo
PS D:\git\tests\rebaseRepo> git co -b bug10 origin/bug10
PS D:\git\tests\rebaseRepo> git co -b legacy origin/legacy
让我们只将 bug10 分支上 2.0 标签之后所有的提交变基到 bug10 分支的 HEAD(包括 bug10 分支的 HEAD):
PS D:\git\tests\rebaseRepo> git co bug10
PS D:\git\tests\rebaseRepo> git rebase --onto legacy 2.0
First, rewinding head to replay your work on top of it...
Applying: a first bug10 fix
Applying: a second bug10 fix
此时,bug10
已经在 legacy
的顶部重新播放,没有所有其他中间提交。
现在我们可以将 legacy
的 HEAD
快进到重新播放的 bug10
分支的顶部。
PS D:\git\tests\rebaseRepo> git co legacy
Switched to branch 'legacy'
PS D:\git\tests\rebaseRepo> git merge bug10
Updating dbcc7aa..cf02bfc
Fast forward
toBeFixedFile.txt | Bin 38 -> 104 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
以下是我们所需的内容:
PS D:\git\tests\rebaseRepo> type legacy.txt
legacyContent
aFirstLegacyEvol
main
分支的内容仅限于1.0
标签(legacy
分支的根),没有更多了。
PS D:\git\tests\rebaseRepo> type mainFile.txt
mainFile
firstMainEvol
bug10
的修复:
PS D:\git\tests\rebaseRepo> type toBeFixedFile.txt
contentToBeFixed
aFirstBug10Fix
aSecondBug10Fix
就是这样。
想法是从原始仓库中拉取那个“增强”的legacy
分支,该分支仍将其bug10
保持不变(即仍从2.0
标签开始,并且没有像我们在rebaseRepo
上重播到任何位置。
在这个克隆的仓库中,我跟踪origin/legacy
分支,以便将另一个远程源的legacy
分支合并到它上面:即rebaseRepo
。
PS D:\git\tests\rebaseRepo> cd ..
PS D:\git\tests> git clone mainRepo finalRepo
PS D:\git\tests> cd finalRepo
PS D:\git\tests\finalRepo> git co -b legacy origin/legacy
rebaseRepo
为远程仓库,并获取它的分支。PS D:\git\tests\finalRepo> git remote add rebasedRepo D:/git/tests/rebaseRepo
PS D:\git\tests\finalRepo> type D:\git\tests\finalRepo\.git\config
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = D:/git/tests/mainRepo
[branch "master"]
remote = origin
merge = refs/heads/master
[branch "legacy"]
remote = origin
merge = refs/heads/legacy
[remote "rebasedRepo"]
url = D:/git/tests/rebaseRepo
fetch = +refs/heads/*:refs/remotes/rebasedRepo/*
PS D:\git\tests\finalRepo> git fetch rebasedRepo
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 3), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From D:/git/tests/rebaseRepo
* [new branch] bug10 -> rebasedRepo/bug10
* [new branch] legacy -> rebasedRepo/legacy
* [new branch] master -> rebasedRepo/master
legacy
而不用修改 bug10
。PS D:\git\tests\finalRepo> git merge rebasedRepo/legacy
Updating dbcc7aa..4919b68
Fast forward
toBeFixedFile.txt | Bin 38 -> 104 bytes
1 files changed, 0 insertions(+), 0 deletions(-)
您可以重复此过程任意次数,每当需要将新的bug10
提交重放到旧的legacy
分支之上时,而不必包含所有中间提交。