[英]Git. Checkout feature branch between merge commits
有點奇怪,但是我無法用git完成一個非常普通的操作。 基本上我想要的是簽出要素分支,而不是使用它的頭部,而是使用SHA id。 此SHA指向主分支合並之間。
問題是我得到的只是master分支,而沒有來自feature分支的提交。 目前,我正在嘗試修復先前在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
謝謝。
沒有這一點。 在這里,您有兩個並行的開發路徑,一個包含文件test1和test2,另一個包含文件test1,test3,test4和test5。 不過,您可以通過合並創建這樣的點。
檢出SHA1中添加了test4的提交,並與添加了test2的提交合並。 例如,運行腳本后,我的存儲庫如下所示:
* b054987 (HEAD, patches) Merge branch 'master' into patches
|\
* | 5ae790f test2
| * f2a3dac (master) test5
| * 70e8cd2 test4
| * c4102ed test3
|/
* d448eaa test1
在此運行:
% git checkout 70e8c
% git merge 5ae79
結果是包含文件1-4的HEAD:
* bcc8f7a (HEAD) Merge commit '5ae79' into HEAD
|\
| | * b054987 (patches) Merge branch 'master' into patches
| | |\
| |/ /
| * | 5ae790f test2
| | * f2a3dac (master) test5
| |/
|/|
* | 70e8cd2 test4
* | c4102ed test3
|/
* d448eaa test1
% ls
test1.txt test2.txt test3.txt test4.txt
現在,您可以根據自己的喜好創建分支。
關於第一個示例,您需要在test2之上重播test3和4:這是rebase --onto
的經典案例 :
從...開始:
替代文字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
那給你:
替代文字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
這使:
替代文字http://img52.imageshack.us/img52/372/gitr3.png
這僅適用於線性移動的提交集。
git merge
操作不會返回並更改歷史記錄,而是會創建一個新的提交,指向它來自的兩個歷史記錄。 因此,在圖片中,您擁有的是這樣的:
2-----------\ / \ 1---3---4---5---M (HEAD)
當您倒回歷史記錄以返回到修訂版4時,您將獲得文件1、3和4,但沒有得到 2。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.