我需要取消洗掉顯示為子模塊的檔案夾的未提交的 git rm。
這比您想象的要復雜。
在我在 GitHub 上的私人倉庫中,我找到了一個子模塊。這是出乎意料的。GitHub 專案頁面將子模塊顯示為帶有箭頭的檔案夾,但不可點擊。
本地有一個子模塊名稱的檔案夾,其中有一個 .git 檔案夾。表明它已經在git init本地。
在本地我重命名了子模塊檔案夾中的 .git 檔案夾并從父模塊運行
git rm -f folder
認為它會擺脫子模塊,只給我留下真正的檔案夾,然后我可以git add回到回購
但它在本地洗掉了該檔案夾,現在以下命令似乎都無法將其取回。請記住,作為子模塊列出的檔案夾從未提交給 git 服務器。但是它所在的檔案夾有。
嘗試了以下所有方法
git reset HEAD folder
git add folder
git checkout -- folder
git reset HEAD folder/*
并將 git 更新到 2.33.1 也可以嘗試
git restore folder
git restore folder/*
和
git status
目前顯示
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: folder
.git/config 沒有對子模塊的參考
我在此執行緒中讀到的任何內容都沒有幫助 在 Git 存盤庫中恢復已洗掉的檔案夾
uj5u.com熱心網友回復:
在我在 GitHub 上的私人倉庫中,我找到了一個子模塊。這是出乎意料的。GitHub 專案頁面將子模塊顯示為帶有箭頭的檔案夾,但不可點擊。
這表明子模塊是......充其量是不完整的,最壞的情況是非常破碎。
本地有一個子模塊名稱的
.git檔案夾,其中有一個檔案夾。表明它已經在git init本地。
可能——或者你或某人跑去git clone創造它。無論哪種方式,該.git目錄(檔案夾)都包含實際的存盤庫。
如果該特定存盤庫還有其他克隆,則這些其他克隆存在,并且具有它們所具有的任何最新狀態。如果沒有,它們就不存在。
在本地我重命名了
.git子模塊檔案夾中的檔案夾并從父模塊運行git rm -f folder
一切都很好,除了一條關鍵資訊。您重命名(或移動)了 this .git。 你把它放在哪里?
讓我們建立一個類似的情況。請注意hintGit 在此處列印的長警告和序列:
$ cd ~/tmp
$ mkdir tt
$ cd tt
$ git init
Initialized empty Git repository in .../tt/.git
$ mkdir sub
$ cd sub
$ git init
Initialized empty Git repository in .../tt/sub/.git
$ echo for testing > README
$ git add README
$ git commit -m initial
[master (root-commit) 1fd3599] initial
1 file changed, 1 insertion( )
create mode 100644 README
$ cd ..
$ git add sub
warning: adding embedded git repository: sub
hint: You've added another git repository inside your current repository.
hint: Clones of the outer repository will not contain the contents of
hint: the embedded repository and will not know how to obtain it.
hint: If you meant to add a submodule, use:
hint:
hint: git submodule add <url> sub
hint:
hint: If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached sub
hint:
hint: See "git help submodule" for more information.
這樣做是用gitlink準備我的下一次提交。
gitlink 是子模塊中最重要的部分。另一半也是最重要的,?? 是進入.gitmodules檔案的東西:這是告訴超級專案Git (in tt/)如何克隆子模塊的東西 tt/sub。
由于我以半途而廢的方式制作了這個子模塊,因此我只有一半的子模塊:gitlink 的一半。這就是 GitHub 上不可點擊的檔案夾圖示所代表的內容:一個 gitlink,其中子模塊克隆指令要么完全丟失,要么不在 GitHub 上。(如果子模塊存盤庫在其他一些公共訪問站點上,也許他們會顯示更多或允許單擊,但如果它完全私有或丟失,則無法顯示更多;您可以通過查看.gitmodules檔案中的檔案來檢查確切情況超級專案。)
現在讓我在我的超級專案 (in tt/) 中進行第一次提交,以便我實際參考子模塊。然后我會做你所做的:將.git檔案夾移動到某個地方,然后運行git rm -f sub:
$ git commit -m "initial in superproject"
[master (root-commit) 20ab8f6] initial in superproject
1 file changed, 1 insertion( )
create mode 160000 sub
$ mv sub/.git save-the-repo
$ git rm -f sub
rm 'sub'
$ ls
save-the-repo
I have not yet committed, but once I do, I have a new commit where there is no gitlink. There is no half-assed submodule, nor a fully-assed submodule: there is nothing at all, because I never put anything else into the repository. Of course, the old commit still exists, and it still refers to the submodule:
git commit -m 'remove half-assed submodule'
[master b01e217] remove half-assed submodule
1 file changed, 1 deletion(-)
delete mode 160000 sub
Note the mode 160000, by the way: that's what Git uses to designate something as a gitlink. We can see it in the previous commit, and that it's gone in the current commit:
$ git ls-tree -r HEAD^
160000 commit 1fd3599ca076ba9f03c88661013810a9536921ea sub
$ git diff HEAD^ HEAD
diff --git a/sub b/sub
deleted file mode 160000
index 1fd3599..0000000
--- a/sub
/dev/null
@@ -1 0,0 @@
-Subproject commit 1fd3599ca076ba9f03c88661013810a9536921ea
Back to your problem
... thinking it would get rid of the submodule and just leave me with the real folder which I can then git add back into the repo
I must now repeat the question (already asked in a comment and here in this answer): Where did you put the original .git folder?
I put mine in save-the-repo. We see it above in the ls output. Your job is now to make some place to hold it and call it .git in that place:
$ mkdir recover
$ mv save-the-repo recover/.git
$ cd recover
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: README
no changes added to commit (use "git add" and/or "git commit -a")
To restore the files to their latest version, I can follow the advice above now, or more simply, git restore .:
$ git restore .
$ ls
README
$ git status
On branch master
nothing to commit, working tree clean
Note that this extracted the most recent commit on master. The .git repository here still exists, and still has all the commits in it that I made. I made only one commit of course, so that's all the commits. But if I now take the one README file and move it into sub, that's the only version I'll save back in the original repository:
$ cd ..
$ ls
recover
$ mkdir sub
$ mv recover/README sub/README
$ git add sub/README
$ git commit -m "move the latest sub/* files into the main repo, losing earlier ones"
[master 2af80e1] move the latest sub/* files into the main repo, losing earlier ones
1 file changed, 1 insertion( )
create mode 100644 sub/README
$ git log --oneline
2af80e1 (HEAD -> master) move the latest sub/* files into the main repo, losing earlier ones
b01e217 remove half-assed submodule
20ab8f6 initial in superproject
I now have three commits: my first one, where I have the half-assed submodule, my second one where I removed it, and my third one where I created sub/ and populated it with the files (well, file, singular) from the latest commit in what used to be a submodule. The current version of the superproject does not depend on any submodules, but older commits in the superproject do—and since there's no .gitmodules giving instructions for where to clone the submodule, those half-assed submodules are broken.
Truly fixing this, leaving no trace of a mistake, would involve creating new history from scratch, where either the submodule is added correctly at the start, or never created as a gitlink in the first place. Then, for every superproject and/or submodule commit, I'd make another new commit to update all the files in the new combined project. But this is just a demo.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/317339.html
標籤:混帐 git-submodules
上一篇:防止特定分支被合并到任何東西中?
下一篇:Git日志-顯示合并請求ID
