我該如何在不出現任何衝突(沒有太多手動工作,例如rebase -i)的情況下儘早提交分支來提交分支?git - 儘可能早的在分支上重新排序提交而不會發生衝突
如
A-B-C-d-X
應該成爲
A-B-X-C-d
如果交換X與C和d沒有矛盾,而是換爲B X會導致衝突。
謝謝。
我該如何在不出現任何衝突(沒有太多手動工作,例如rebase -i)的情況下儘早提交分支來提交分支?git - 儘可能早的在分支上重新排序提交而不會發生衝突
如
A-B-C-d-X
應該成爲
A-B-X-C-d
如果交換X與C和d沒有矛盾,而是換爲B X會導致衝突。
謝謝。
好了,這個漂亮的作品不多,但需要一些清理。
經過一段時間後,我遇到了另一個問題,我發佈了here。
#!/bin/sh -e # todo: integrate with git GIT_DIR=./.git/ commitid=$1 if [ "$1" = "" ]; then echo usage: $0 commitid exit 1 fi tmpdir="$GIT_DIR/bubble-work" /bin/rm -rf "$tmpdir" mkdir "$tmpdir" # checkout commit with detached head git checkout -q $commitid^0 || die "could not detach HEAD" while [ 1 = 1 ]; do # todo pipe output to avoid temp files # see git-rebase.sh patchfile=`git format-patch -k --full-index --src-prefix=a/ --dst-prefix=b/ --no-renames -o "$tmpdir" HEAD~1` echo patch = $patchfile git checkout -q HEAD~2 git am --rebasing "$patchfile" || die "git am failed" /bin/rm -f "$patchfile" echo looping done /bin/rm -rf "$tmpdir"
我需要以自動方式(而不是rebase -i)執行此操作,因爲分支上可能會有數百個提交。另外,我預期的訂單還未提前知道。 – CoderBrien 2011-12-20 18:30:37
除非我誤解,否則這隻會顯示編輯文件中的X提交。 – toddsundsted 2011-12-20 18:35:23
@toddsundsted我認爲'X'是最早的提交,所以它應該顯示'X..HEAD'。 – m0tive 2011-12-20 18:40:30
下面是我在黑客入侵15分鐘後想出的內容。它不是一個完整的解決方案,但它應該減少所涉及的工作。
目標是使用git bisect
爲將來的提交找到最早的無衝突合併點。該解決方案利用git bisect
固有的二進制搜索功能,以減少步驟。
不幸的是,這不會排除後來提交衝突,所以需要交互式基地來審查結果(但這就是要點)。
一個缺點/警告是,當您在測試補丁時指示git關於步驟失敗還是成功時,您必須將good
和bad
顛倒過來。
如果以下任何步驟都不清楚,請告訴我,我會盡力詳細說明。
首先在一系列提交中創建以下文件。每個提交應該添加一系列四條相同的行(a,b和c,然後d's)。
a
a
a
a
b
b
b
b
c
c
c
c
d
d
d
d
在這一點上,git log
應輸出類似:
commit 6f2b809863632a86cc0523df3a4bcca22cf5ab17
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:45:44 2011 -0500
Added d.
commit 91ba7e6f19db74adb6ce79e7b85ea965788f6b88
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:44:26 2011 -0500
Added c.
commit f83beee55d6e060536584852ebb55c5ac3b850b2
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:44:00 2011 -0500
Added b.
commit d6d924b0a30a9720f6e01dcc79dc49097832a587
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:43:38 2011 -0500
Added a.
commit 74d41121470108642b1a5df087bc837fdf77d31c
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:43:11 2011 -0500
Initial commit.
現在編輯的文件,以便它包含以下內容,並提交此:
a
a
a
a
b
x
x
b
c
x
x
c
d
d
d
d
的應記錄現在包含一個以上的提交:
commit 09f247902a9939cb228b580d39ed2622c3211ca6
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:46:36 2011 -0500
Replaced a few lines with x.
現在爲X
提交生成補丁。
git diff -p master~ > x.patch
殺青bisect
- 記得用git bisect good
當補丁失敗,當補丁成功git bisect bad
:
$ git bisect start
$ git bisect good 74d41121470108642b1a5df087bc837fdf77d31c
$ git bisect bad master
Bisecting: 2 revisions left to test after this (roughly 1 step)
[f83beee55d6e060536584852ebb55c5ac3b850b2] Added b.
$ patch --dry-run -p1 < x.patch
patching file file.txt
Hunk #1 FAILED at 3.
1 out of 1 hunk FAILED -- saving rejects to file file.txt.rej
$ git bisect good
Bisecting: 0 revisions left to test after this (roughly 1 step)
[6f2b809863632a86cc0523df3a4bcca22cf5ab17] Added d.
$ patch --dry-run -p1 < x.patch
patching file file.txt
$ git bisect bad
Bisecting: 0 revisions left to test after this (roughly 0 steps)
[91ba7e6f19db74adb6ce79e7b85ea965788f6b88] Added c.
$ patch --dry-run -p1 < x.patch
patching file file.txt
Hunk #1 succeeded at 3 with fuzz 2.
$ git bisect bad
91ba7e6f19db74adb6ce79e7b85ea965788f6b88 is the first bad commit
commit 91ba7e6f19db74adb6ce79e7b85ea965788f6b88
Author: Todd Sundsted <...>
Date: Tue Dec 20 22:44:26 2011 -0500
Added c.
$ git bisect reset
正如預期的那樣,在編輯提交X
可以移動立即提交後C
。一個互動變基證實了這一點:
91e92489 * Added d.
6c082b1f * Replaced a few lines with x.
a60ae2a9 * Added c.
4d5e78f2 * Added b.
7d2ff759 * Added a.
74d41121 * Initial commit.
感謝您的嘗試。我現在正在研究一種解決方案,我認爲它會起作用。我要編寫一個腳本,它在循環中使用'git format-patch'和'git am'來向後走分支並查看'git am'失敗的位置。希望 :-)。 – CoderBrien 2011-12-20 20:53:23
有趣的問題。即使發現歷史記錄中的點是自動的,導致沒有衝突,也不能保證生成的文件能夠正確編譯/正確運行等。 – toddsundsted 2011-12-20 18:33:16
不應該在重新排序之後所得到的頭部樹相同,或者我誤解了乾淨的提交重新排序意味着什麼? – CoderBrien 2011-12-20 18:46:36
沒問題,對...你在考慮頭部,我在考慮歷史早期變化的影響 - 特別是在X(不是D) – toddsundsted 2011-12-20 19:09:17