最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

git merge --ff/--no-ff/--ff-only 三種選項參數的區(qū)別解析

 更新時間:2021年04月29日 10:28:09   作者:肖斌  
這篇文章主要介紹了git merge --ff/--no-ff/--ff-only 三種選項參數的區(qū)別解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

前言

git merge 應該是開發(fā)者最常用的 git 指令之一,默認情況下你直接使用 git merge 命令,沒有附加任何選項命令的話,那么應該是交給 git 來判斷使用哪種 merge 模式,實際上 git 默認執(zhí)行的指令是 git merge -ff 指令(默認值)

對于專業(yè)的開發(fā)者來說,你可能無須每次合并都指定合并模式(如果需要的話還是要指定的),但是你可能需要知道 git 在背后為你默認做了什么事情,這樣才能保證你的代碼萬無一失。

先說說什么是 Fast-forward

我們從一個正常開發(fā)流程來看看:

開發(fā)者小王接到需求任務,從 master 分支中創(chuàng)建功能分支,git 指令如下:

git checkout -b feature556
Switched to a new branch 'feature556'

小王在 feature556 分支上完成的功能開發(fā)工作,然后產生1次 commit,

git commit -m 'Create pop up effects'
[feature556 6104106] create pop up effects
 3 files changed, 75 insertions(+)

我們再更新一下 README 自述文件,讓版本差異更明顯一些

git commit -m `updated md`

這時候我們看看當前分支的 git 歷史記錄,輸入 git log --online -all 可以看到全部分支的歷史線:

f2c9c7f (HEAD -> feature556) updated md
6104106 create pop up effects
a1ec682 (origin/main, origin/HEAD, main) import dio
c5848ff update this readme
8abff90 update this readme

直接看下圖可能會更好理解一些

功能完成后自然要上線,我們把代碼合并,完成上線動作,代碼如下

git checkout master
git merge feautre556
Updating a1ec682..38348cc
Fast-forward
  .......  | 2+++
 1 file changed, 2 insertions(+)

如果你注意上面的文字的話,你會發(fā)現 git 幫你自動執(zhí)行了 Fast-forward 操作,那么什么是 Fast-forward ?
Fast-forward 是指 Master 合并 Feature 時候發(fā)現 Master 當前節(jié)點一直和 Feature 的根節(jié)點相同,沒有發(fā)生改變,那么 Master 快速移動頭指針到 Feature 的位置,所以 Fast-forward 并不會發(fā)生真正的合并,只是通過移動指針造成合并的假象,這也體現 git 設計的巧妙之處。合并后的分支指針如下:

通常功能分支(feature556) 合并 master 后會被刪除,通過下圖可以看到,通過 Fast-forward 模式產生的合并可以產生干凈并且線性的歷史記錄:

再說說什么是 non-Fast-forward

剛說了會產生 Fast-forward 的情況,現在再說說什么情況會產生 non-Fast-forward,通常,當合并的分支跟 master 不存在共同祖先節(jié)點的時候,這時候在 merge 的時候 git 默認無法使用 Fast-forward 模式,
我們先看看下圖的模型:

可以看到 master 分支已經比 feature001 快了2個版本,master 已經沒辦法通過移動頭指針來完成 Fast-forward,所以在 master 合并 feature001 的時候就不得不做出真正的合并,真正的合并會讓 git 多做很多工作,具體合并的動作如下:

  • 找出 master 和 feature001 的公共祖先,節(jié)點 c1,c6, c3 三個節(jié)點的版本 (如果有沖突需要處理)
  • 創(chuàng)建新的節(jié)點 c7,并且將三個版本的差異合并到 c7,并且創(chuàng)建 commit
  • 將 master 和 HEAD 指針移動到 c7

補充:大家在 git log 看到很多類似:Merge branch 'feature001' into master 的 commit 就是 non-Fast-forward 產生的。
執(zhí)行完以上動作,最終分支流程圖如下:

如何手動設置合并模式 ?

先簡單介紹一下 git merge 的三個合并參數模式:

  • -ff 自動合并模式:當合并的分支為當前分支的后代的,那么會自動執(zhí)行 --ff (Fast-forward) 模式,如果不匹配則執(zhí)行 --no-ff(non-Fast-forward) 合并模式
  • --no-ff 非 Fast-forward 模式:在任何情況下都會創(chuàng)建新的 commit 進行多方合并(及時被合并的分支為自己的直接后代)
  • --ff-onlu Fast-forward 模式:只會按照 Fast-forward 模式進行合并,如果不符合條件(并非當前分支的直接后代),則會拒絕合并請求并且推出

以下是關于 --ff, --no-ff, --ff-only 三種模式的官方說明(使用 git merge --helo 即可查看):

Specifies how a merge is handled when the merged-in history is already a descendant of the current history. --ff is the default unless merging an annotated (and possibly signed) tag that is not stored in its natural place in the refs/tags/ hierarchy, in which case --no-ff is assumed.

With --ff, when possible resolve the merge as a fast-forward (only update the branch pointer to match the merged branch; do not create a merge commit). When not possible (when the merged-in history is not a descendant of the current history), create a merge commit.

With --no-ff, create a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.

With --ff-only, resolve the merge as a fast-forward when possible. When not possible, refuse to merge and exit with a non-zero status.

總結:

三種 merge 模式沒有好壞和優(yōu)劣之分,只有根據你團隊的需求和實際情況選擇合適的合并模式才是最優(yōu)解,那么應該怎么選擇呢? 我給出以下推薦:

  • 如果你是小型團隊,并且追求干凈線性 git 歷史記錄,那么我推薦使用 git merge --ff-only 方式保持主線模式開發(fā)是一種不錯的選擇
  • 如果你團隊不大不小,并且也不追求線性的 git 歷史記錄,要體現相對真實的 merge 記錄,那么默認的 git --ff 比較合適
  • 如果你是大型團隊,并且要嚴格監(jiān)控每個功能分支的合并情況,那么使用 --no-ff 禁用 Fast-forward 是一個不錯的選擇

到此這篇關于git merge --ff/--no-ff/--ff-only 三種選項參數的區(qū)別解析的文章就介紹到這了,更多相關git merge --ff/--no-ff/--ff-only內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

梅河口市| 南京市| 海宁市| 宝兴县| 龙江县| 修文县| 墨江| 商水县| 大同县| 孝义市| 莫力| 双鸭山市| 大宁县| 凤山县| 古交市| 城固县| 黄浦区| 永善县| 潜山县| 揭西县| 和政县| 阜南县| 东莞市| 边坝县| 托克逊县| 论坛| 游戏| 榆中县| 磐安县| 全南县| 武宁县| 双牌县| 厦门市| 合作市| 梁平县| 定边县| 信阳市| 阿城市| 桐城市| 墨竹工卡县| 武乡县|