我有一个尚未合并的功能分支。我需要从Master
创建一个新分支,但我希望它也包括我尚未合并的功能分支的更改。
我不想分支我的功能分支,该怎么办?
master
~ branch A (not yet merged)
~ branch B (new branch which includes changes from branch A)
我当时想也许应该从master分支,然后根据分支A重新建立新分支?
当我最终合并分支B之后的分支B时,所做的更改应仅显示分支B的更改,因为分支A已被合并-这是正确的吗?
不需要任何特殊准备。您可以
# master is checked out
git checkout -b B
git merge A
或
# A is checked out
git checkout -b B
git merge master
[在两种情况下,您的新分支B最终都是“分支脱离母版”,并且对A进行了新的更改。在A合并回母版之后,master
和B
之间的差异将不再显示A的内容为不同,因为这些提交现在在两个分支的历史中。 B的后续合并将仅包括/添加针对该新功能发生的那些提交。
完整示例,从空目录开始:
# 1st commit
git init
echo "foo" >> foo
git add -A
git commit -m "foo"
# Branch A
git checkout -b A
echo A >> A
git add -A
git commit -m "A"
# Branch B on top of A
git checkout -b B
echo B >> B
git add -A
git commit -m "B"
# More development on master
git checkout master
echo bar >> bar
git add -A
git commit -m "bar"
# First, B "contains" both A and B changes
git diff B
# But after merging A...
git merge A
# ...Diff B now only shows "remaining" changes
git diff B