Git的.合并提交之间的结帐功能分支

这有点奇怪,但我不能用git完成一个非常常见的操作.基本上我想要的是签出一个功能分支,而不是使用它的头但使用SHA id.此SHA指向主分支的合并之间.

问题是,我得到的只是主分支而没有来自功能分支的提交.
目前我正在尝试修复之前在master分支中引入的回归.

为了更具描述性,我制作了一个小的bash脚本来重新创建一个问题存储库:

#!/bin/bash

rm -rf ./.git
git init

echo "test1" > test1.txt
git add test1.txt
git commit -m "test1" -a

git checkout -b patches master

echo "test2" > test2.txt
git add test2.txt
git commit -m "test2" -a

git checkout master

echo "test3" > test3.txt
git add test3.txt
git commit -m "test3" -a

echo "test4" > test4.txt
git add test4.txt
git commit -m "test4" -a

echo "test5" > test5.txt
git add test5.txt
git commit -m "test5" -a

git checkout patches
git merge master    

#Now how to get a branch having all commits from patches + test3.txt + test4.txt - test5.txt ???

基本上我想要的只是用文件1-4签出分支“补丁”,但不包括test5.txt.

这样做:
git checkout [sha_where_test4.txt_entered]

…只是给出了test1,test3,test4的分支,但不包括test2.txt

更复杂的例子:

#!/bin/bash

rm -rf ./.git
git init

echo "test1" > test1.txt
git add test1.txt
git commit -m "test1" -a

git checkout -b patches master

echo "test2" > test2.txt
git add test2.txt
git commit -m "test2" -a

git checkout master

echo "test3" > test3.txt
git add test3.txt
git commit -m "test3" -a

echo "test4" > test4.txt
git add test4.txt
git commit -m "test4" -a

echo "test5" > test5.txt
git add test5.txt
git commit -m "test5" -a

git checkout patches
git merge master

echo "test6" > test6.txt
git add test6.txt
git commit -m "test6" -a

#Now how to get a branch having all commits from patches + test3.txt + test4.txt - test5.txt ???
git log --topo-order | cat

# Now I need something to help me going back to history 
# without manually calculating that patches~2 sha's
git checkout -b patches.tmp master~1
git merge patches~2

谢谢.

最佳答案 关于你的第一个例子,你需要在test2上重播test3和4:这是一个经典的
case of rebase --onto

从…开始:

alt text http://img169.imageshack.us/img169/2255/gitr1.png

标记当前的补丁安置,并将补丁分支移动到您想要结束的位置(test4):

C:\Prog\Git\tests\rep\main>git checkout patches
Switched to branch 'patches'

C:\Prog\Git\tests\rep\main>git checkout -b tmp
Switched to a new branch 'tmp'

C:\Prog\Git\tests\rep\main>git checkout patches
Switched to branch 'patches'

C:\Prog\Git\tests\rep\main>git reset --hard master~1
HEAD is now at 8448d0f test4

这给你:

alt text http://img169.imageshack.us/img169/4826/gitr2.png

只需重新定义你想要的正确提交顺序:

C:\Prog\Git\tests\rep\main>git checkout tmp
Switched to branch 'tmp'

C:\Prog\Git\tests\rep\main>git rebase --onto tmp tmp~1 patches
First, rewinding head to replay your work on top of it...
Applying: test3
Applying: test4

这使:

alt text http://img52.imageshack.us/img52/372/gitr3.png

这仅适用于要移动的线性提交集.

点赞