取消子模塊的git子模塊

如何取消git子模塊的子模塊(將全部代碼帶回內核)? git

正如我「應該」如何,如「最佳程序」同樣... fetch


#1樓

對於何時 this

git rm [-r] --cached submodule_path

退貨 url

fatal: pathspec 'emr/normalizers/' did not match any files

上下文:我在子模塊文件夾中作了rm -r .git* ,而後才意識到須要在剛剛添加了它們的主項目中對它們進行反調製。 當對部分而非所有解調器進行非子調製時,出現上述錯誤。 不管如何,我經過運行來修復它們(固然,在rm -r .git*spa

mv submodule_path submodule_path.temp
git add -A .
git commit -m "De-submodulization phase 1/2"
mv submodule_path.temp submodule_path
git add -A .
git commit -m "De-submodulization phase 2/2"

請注意,這不會保留歷史記錄。 code


#2樓

我發現從子模塊中獲取本地提交數據也更加方便,由於不然我將丟失它們。 (因爲我沒法訪問該遙控器,所以沒法推送它們)。 所以,我將submodule / .git添加爲remote_origin2,獲取了該提交併從該分支合併。 不知道我是否仍須要將遠程子模塊做爲源,由於我對git還不熟悉。 orm


#3樓

git 1.8.5(2013年11月 )開始( 不保留子模塊的歷史記錄 ): 索引

mv yoursubmodule yoursubmodule_tmp
git submodule deinit yourSubmodule
git rm yourSubmodule
mv yoursubmodule_tmp yoursubmodule
git add yoursubmodule

那將: rem

  • 取消註冊並卸載 (即刪除 )子模塊( deinit ,所以首先mv )的內容,
  • 爲您清理.gitmodulesrm ),
  • 並在父存儲庫( rm )的索引中刪除表示該子模塊SHA1的特殊條目

一旦刪除了子模塊( deinitgit rm ),就能夠將文件夾重命名爲其原始名稱,並將其做爲常規文件夾添加到git repo中。 get

注意:若是子模塊是由一箇舊的Git(<1.8)建立的,則可能須要刪除嵌套的.git子模塊自己的文件夾,如評論西蒙東


若是須要保留子模塊的歷史記錄,請參閱jsears答案 ,該答案使用git filter-branch


#4樓

  1. git rm --cached the_submodule_path
  2. .gitmodules文件中刪除子模塊部分,或者若是它是惟一的子模塊,則刪除文件。
  3. 提交「已刪除子模塊xyz」
  4. git add the_submodule_path
  5. 另外一個提交「添加了xyz的代碼庫」

我尚未找到更簡單的方法。 您能夠經過git commit -a 3-5壓縮爲一步。


#5樓

若是隻須要將子模塊代碼放入主存儲庫,則只需刪除子模塊,而後將文件從新添加到主存儲庫中便可:

git rm --cached submodule_path # delete reference to submodule HEAD (no trailing slash)
git rm .gitmodules             # if you have more than one submodules,
                               # you need to edit this file instead of deleting!
rm -rf submodule_path/.git     # make sure you have backup!!
git add submodule_path         # will add files instead of commit reference
git commit -m "remove submodule"

若是您還想保留子模塊的歷史記錄,能夠作一個小技巧:將子模塊「合併」到主存儲庫中,以便結果與之前相同,只是子模塊文件如今位於主存儲庫。

在主模塊中,您須要執行如下操做:

# Fetch the submodule commits into the main repository
git remote add submodule_origin git://url/to/submodule/origin
git fetch submodule_origin

# Start a fake merge (won't change any files, won't commit anything)
git merge -s ours --no-commit submodule_origin/master

# Do the same as in the first solution
git rm --cached submodule_path # delete reference to submodule HEAD
git rm .gitmodules             # if you have more than one submodules,
                               # you need to edit this file instead of deleting!
rm -rf submodule_path/.git     # make sure you have backup!!
git add submodule_path         # will add files instead of commit reference

# Commit and cleanup
git commit -m "removed submodule"
git remote rm submodule_origin

生成的存儲庫看起來有些奇怪:將有多個初始提交。 但這不會對git形成任何問題。

在第二種解決方案中,您將具備很大的優點,即您仍然能夠在最初位於子模塊中的文件上運行git blame或git log。 實際上,您在這裏所作的是在一個存儲庫中重命名許多文件,而git應該自動檢測到這一點。 若是git log仍然有問題,請嘗試一些選項(--follow,-M,-C),這些選項能夠更好地重命名/複製檢測。

相關文章
相關標籤/搜索