目錄
Setting up the remote
Merging the subtree
Updating the subtree
首頁 開發工具 Git 如何將子樹添加到我的git存儲庫中?

如何將子樹添加到我的git存儲庫中?

Jul 16, 2025 am 01:48 AM
git 子樹

要將子樹添加到Git倉庫,首先添加遠程倉庫並獲取其歷史記錄,接著使用git merge和git read-tree命令將其合併為子目錄。步驟如下:1. 使用git remote add -f命令添加遠程倉庫;2. 運行git merge --s recursive --no-commit獲取分支內容;3. 使用git read-tree --prefix=指定目錄將項目作為子樹合併;4. 提交更改以完成添加;5. 更新時先git fetch再重複合併步驟提交更新。此方法保持外部項目歷史完整且便於維護。

How do I add a subtree to my Git repository?

Adding a subtree to your Git repository is a useful way to include another project as a subdirectory inside your main repo, while still keeping its history intact. It's especially handy when you want to pull in third-party libraries or share code between projects without using submodules. Here's how to do it effectively.


Setting up the remote

Before adding a subtree, you need to have the remote repository set up. If you haven't already added it as a remote, now's the time to do so. This makes it easier to update later.

You can add a new remote like this:

 git remote add -f <remote-name> <repository-url>

For example:

 git remote add -f vendor https://github.com/example/external-project.git

The -f flag fetches the remote right away, which saves you a step. Once that's done, you're ready to actually merge it into your project.


Merging the subtree

Now comes the actual process of merging the external repo as a subtree. You'll use git merge with the --s recursive and --no-commit flags to avoid conflicts and keep things flexible.

Here's the basic command:

 git merge --s recursive --no-commit <remote-name>/<branch-name>

Then tell Git that this is a subtree:

 git read-tree --prefix=<subdir-name>/ -u <remote-name>/<branch-name>

So if you were adding the vendor remote's main branch into a folder called lib/external , it would look like:

 git merge --s recursive --no-commit vendor/main
git read-tree --prefix=lib/external -u vendor/main

Finally, commit the changes:

 git commit -m "Added external project as subtree"

This will bring the entire project into your repo under the specified directory.


Updating the subtree

One of the nice things about subtrees is that you can update them easily. When the external project gets new commits, just pull those changes into your subtree:

  1. Fetch the latest changes from the remote:

     git fetch vendor
  2. Merge the updates using the same subtree logic:

     git merge --s recursive --no-commit vendor/main
    git read-tree --prefix=lib/external -u vendor/main
    git commit -m "Updated external subtree"

    This keeps your embedded project current without having to manually copy files.


    That's basically it. Once you've got the hang of it, working with subtrees becomes second nature — it's not complicated, but it's easy to overlook small steps like fetching first or using the correct prefix. Keep a quick reference handy until it clicks.

    以上是如何將子樹添加到我的git存儲庫中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

灰度數字大盤是什麼意思 灰度數字大盤是什麼意思 Jul 01, 2025 pm 05:57 PM

灰度數字大盤是指灰度投資旗下數字資產信託基金的整體表現,其核心包含單一資產信託和復合型基金。 1.“灰度”指Grayscale Investments,旗下有GBTC、ETHE等信託產品;2.“數字大盤”狹義指追踪主流加密資產的GDLC基金,廣義則涵蓋灰度所有信託產品整體表現。關注灰度數字大盤的原因包括:1.其持倉變化反映機構投資者對加密貨幣的配置傾向;2.信託產品的溢價或折價波動影響市場情緒;3.作為受SEC監管的合規通道,其動向具有政策參考價值。

Lightchain AI:額外獎勵輪次熱議及主網啟動即將到來 Lightchain AI:額外獎勵輪次熱議及主網啟動即將到來 Jul 02, 2025 pm 06:33 PM

LightchainAI當前正處在獎勵輪次階段,為投資者提供在2025年7月主網上線前最後獲取LCAI代幣的機會。平台至今已募集2110萬美元資金,其自主研發的AI虛擬機正在行業內引發高度關注。去中心化人工智能的發展勢頭愈發強勁,而LightchainAI憑藉其獨特的創新模式正在成為焦點。隨著主網啟動日期鎖定在2025年7月,目前平台開放的獎勵輪次成為投資者入場的重要窗口。我們一起來看看LightchainAI的核心亮點及其為何備受矚目。 LightchainAI:推動去中心化AI發展Lightc

Ripple、美國銀行與XRP:金融創新的新時代? Ripple、美國銀行與XRP:金融創新的新時代? Jul 04, 2025 pm 08:36 PM

Ripple正在通過申請國家銀行牌照,以及推動XRP在加密經濟中的新角色,重新定義金融行業的未來格局。掌握最新動態與深度觀察,把握趨勢先機。加密貨幣生態正處於高速演進之中,而Ripple及其數字資產XRP無疑站在風暴中心。尤其在美國銀行體系中所展開的一系列動作,正在引發廣泛關注。這一切的發展彷彿是一場現實中的金融劇集,正逐步揭開序幕! Ripple的銀行業宏圖大略Ripple首席執行官BradGarlinghouse已不再滿足於傳統金融科技的邊界。作為戰略升級的關鍵步驟,Ripple

代幣聚焦:XRP、Solana 與不斷變化的加密貨幣格局 代幣聚焦:XRP、Solana 與不斷變化的加密貨幣格局 Jul 02, 2025 pm 06:12 PM

深入解析XRP與Solana:探討其最新動態與市場地位,把握altcoin的發展趨勢。聚焦altcoin:XRP、Solana與加密生態的演進altcoin市場正迎來新一輪活躍期! XRP和Solana等主流代幣正在引發廣泛關注。本文將剖析它們的最新進展,為加密投資者提供有價值的參考信息。 XRP:法律前景明朗化提振市場信心XRP與其和美國證券交易委員會(SEC)之間的訴訟案接近尾聲,這一進展可能為機構資金入場打開大門。目前已有超過50家國際銀行及支付網絡將其納入結算系統,

什麼是git中的包裝文件? 什麼是git中的包裝文件? Jul 08, 2025 am 12:14 AM

Packfile是Git用來打包、壓縮和傳輸版本庫對象的高效機制。當你執行gitpush、gitfetch或gitclone時,Git實際傳輸的就是packfile;1.它最初由鬆散對象通過gitgc或gitrepack命令生成,存於.git/objects/pack/目錄;2.Packfile不僅包含對像數據,還記錄對象間的差異(delta)關係,並配合索引文件(.idx)實現快速查找;3.這種設計減少了傳輸體積,提高了同步效率;4.大量小packfile可能影響性能,可通過gitgc或git

我如何查看我的git存儲庫的提交歷史? 我如何查看我的git存儲庫的提交歷史? Jul 13, 2025 am 12:07 AM

要查看Git提交歷史,使用gitlog命令。 1.基本用法為gitlog,可顯示提交哈希、作者、日期和提交信息;2.使用gitlog--oneline獲取簡潔視圖;3.通過--author和--grep按作者或提交信息過濾;4.添加-p查看代碼變更,--stat查看變更統計;5.使用--graph和--all查看分支歷史,或借助GitKraken、VSCode等可視化工具。

2025年最值得投資的5大穩定幣(附最新數據) 2025年最值得投資的5大穩定幣(附最新數據) Jul 09, 2025 am 06:06 AM

2025年最具投資價值的五大穩定幣為Tether(USDT)、USD Coin(USDC)、Dai(DAI)、First Digital USD(FDUSD)和TrueUSD(TUSD)。

如何刪除git分支? 如何刪除git分支? Jul 13, 2025 am 12:02 AM

要刪除Git分支,首先確保已合併或無需保留,使用gitbranch-d刪除本地已合併分支,若需強制刪除未合併分支則用-D參數。遠程分支刪除使用gitpushorigin--deletebranch-name命令,並可通過gitfetch--prune同步他人本地倉庫。 1.刪除本地分支需確認是否已合併;2.遠程分支刪除需使用--delete參數;3.刪除後應驗證分支是否成功移除;4.與團隊溝通避免誤刪共享分支;5.定期清理無用分支以保持倉庫整潔。

See all articles