Git:真實 merge

前言

Git:真實 merge 是一種 merge 的方式乘综,除去真實 merge裆装,肯定還有不真實的 merge崇棠,就是那種 FF (FAST-FORWARD MERGE)的 merge涛贯,這個 FF厕氨,曾經(jīng)出現(xiàn)在在收音機上坝辫,出現(xiàn)在錄像機上篷就,出現(xiàn)在視頻播放器上,就是快進的意思近忙。

博客

IT老兵驛站竭业。

前言

這里準備碎片化地去解讀和理解 Git 的一些功能。關于 git-merge 的總結一直沒有做及舍,但是幾乎每天都會遇到 git-merge未辆,而且會遇到很多隨著 merge 而產(chǎn)生的問題,所以只好碎片化地去做一做整理锯玛。

正文

git-merge 有兩種 merge 方式咐柜,ff 方式和 true merge 方式,關于 ff 的方式攘残,另外一篇文章有講過拙友,這里不再贅述,這里整理一下 true merge歼郭,真實 merge遗契。

介紹

先摘錄一段:

TRUE MERGE
Except in a fast-forward merge (see above), the branches to be merged must be tied together by a merge commit that has both of them as its parents.

真實 merge 是真的去 merge 一下,而不是像 ff 那樣病曾,只是把一個 commit 放過來牍蜂,從這個角度來說漾根,ff 不是有點像 cherry-pick 嗎?

A merged version reconciling the changes from all branches to be merged is committed, and your HEAD, index, and working tree are updated to it. It is possible to have modifications in the working tree as long as they do not overlap; the update will preserve them.

一個 merge 的版本融合了(reconcile 的意思是調(diào)節(jié)捷兰,放在這里有點不好理解立叛,所以解釋為融合)所有要被 merge 的分支的改變,并且會被提交贡茅,這樣你的 HEAD秘蛇、index、working tree 都會被更新顶考。

When it is not obvious how to reconcile the changes, the following happens:
</br>
The HEAD pointer stays the same.
</br>
The MERGE_HEAD ref is set to point to the other branch head.
</br>
Paths that merged cleanly are updated both in the index file and in your working tree.
</br>
For conflicting paths, the index file records up to three versions: stage 1 stores the version from the common ancestor, stage 2 from HEAD, and stage 3 from MERGE_HEAD (you can inspect the stages with git ls-files -u). The working tree files contain the result of the "merge" program; i.e. 3-way merge results with familiar conflict markers <<< === >>>.

3路 merge赁还,公共祖先、HEAD驹沿、MERGE_HEAD(是指另外那個分支的 HEAD) 這三個方面來 merge艘策。

這里是三個版本的關系,公共祖先的版本渊季、HEAD(本地倉庫的版本)朋蔫、MERGE_HEAD(另外一個分支想要merge過來的版本),所以叫3-way merge却汉,三路合并驯妄。

If you tried a merge which resulted in complex conflicts and want to start over, you can recover with git merge --abort.

當你搞不定了,使用 git merge --abort 回滾吧合砂。

關于HEAD青扔、index、worktree翩伪、local repository微猖、remote repository的關系,請參考這里缘屹,這個挺重要凛剥,隨后要整理一下。

合并策略:

MERGE STRATEGIES
</br>
The merge mechanism (git merge and git pull commands) allows the backend merge strategies to be chosen with -s option. Some strategies can also take their own options, which can be passed by giving -X<option> arguments to git merge and/or git pull.

git merge 和 git pull 命令使用的 merge 機制允許通過 -s 選項后面的 merge 策略被選擇轻姿。

resolve
This can only resolve two heads (i.e. the current branch and another branch you pulled from) using a 3-way merge algorithm. It tries to carefully detect criss-cross merge ambiguities and is considered generally safe and fast.

recursive
This can only resolve two heads using a 3-way merge algorithm. When there is more than one common ancestor that can be used for 3-way merge, it creates a merged tree of the common ancestors and uses that as the reference tree for the 3-way merge. This has been reported to result in fewer merge conflicts without causing mismerges by tests done on actual merge commits taken from Linux 2.6 kernel development history. Additionally this can detect and handle merges involving renames, but currently cannot make use of detected copies. This is the default merge strategy when pulling or merging one branch.
</br>
The recursive strategy can take the following options:
</br>
ours
This option forces conflicting hunks to be auto-resolved cleanly by favoring our version. Changes from the other tree that do not conflict with our side are reflected to the merge result. For a binary file, the entire contents are taken from our side.
</br>
This should not be confused with the ours merge strategy, which does not even look at what the other tree contains at all. It discards everything the other tree did, declaring our history contains all that happened in it.
</br>
theirs
This is the opposite of ours; note that, unlike ours, there is no theirs merge strategy to confuse this merge option with.
......

使用-X<option>參數(shù)当悔,可以指定合并策略,上面摘錄了兩種踢代,一種是 resolve盲憎,一種是recursive,第一種策略看上去似乎是可以自動解決沖突胳挎,第二種是 Git 默認的 merge 策略饼疙,會產(chǎn)生一些少量的沖突,而不會進行錯誤的合并,它還有幾個選項窑眯,就是合并時屏积,只選擇本地的(ours),或者只選擇別人的(theirs)磅甩。

參考

https://git-scm.com/docs/git-merge
https://stackoverflow.com/questions/3689838/whats-the-difference-between-head-working-tree-and-index-in-git

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末炊林,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子卷要,更是在濱河造成了極大的恐慌渣聚,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件僧叉,死亡現(xiàn)場離奇詭異奕枝,居然都是意外死亡,警方通過查閱死者的電腦和手機瓶堕,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進店門隘道,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人郎笆,你說我怎么就攤上這事谭梗。” “怎么了宛蚓?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵默辨,是天一觀的道長。 經(jīng)常有香客問我苍息,道長,這世上最難降的妖魔是什么壹置? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任竞思,我火速辦了婚禮,結果婚禮上钞护,老公的妹妹穿的比我還像新娘盖喷。我一直安慰自己,他們只是感情好难咕,可當我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布课梳。 她就那樣靜靜地躺著,像睡著了一般余佃。 火紅的嫁衣襯著肌膚如雪暮刃。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天爆土,我揣著相機與錄音椭懊,去河邊找鬼。 笑死步势,一個胖子當著我的面吹牛氧猬,可吹牛的內(nèi)容都是我干的背犯。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼盅抚,長吁一口氣:“原來是場噩夢啊……” “哼漠魏!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起妄均,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤柱锹,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后丛晦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奕纫,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年烫沙,在試婚紗的時候發(fā)現(xiàn)自己被綠了匹层。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡锌蓄,死狀恐怖升筏,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情瘸爽,我是刑警寧澤您访,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站剪决,受9級特大地震影響灵汪,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜柑潦,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一享言、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧渗鬼,春花似錦览露、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至堰乔,卻和暖如春偏化,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背镐侯。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工夹孔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓搭伤,卻偏偏與公主長得像只怎,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子怜俐,可洞房花燭夜當晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容