我有一個git似乎對其分支跟蹤配置感到困惑的存盤庫,我需要一些幫助來理順它。我在跑步git version 2.21.0.windows.1。
問題是git branch -a仍然列出了我洗掉的遠程,并且它顯示的分支跟蹤與git remote -vand不一致git remote show origin。以下是顯示回購當前狀態的命令:
C:\dev> git branch -a
* dev-mdbootstrap
master
remotes/localrepo/HEAD -> localrepo/master
remotes/localrepo/dev-mdbootstrap
remotes/localrepo/master
remotes/origin/master
C:\dev> git remote -v
origin https://github.com/MyOrganization/MyProject.git (fetch)
origin https://github.com/MyOrganization/MyProject.git (push)
C:\dev> git remote show origin
* remote origin
Fetch URL: https://github.com/MyOrganization/MyProject.git
Push URL: https://github.com/MyOrganization/MyProject.git
HEAD branch: master
Remote branches:
dev-mdbootstrap tracked
master tracked
Local branches configured for 'git pull':
dev-mdbootstrap merges with remote dev-mdbootstrap
master merges with remote master
Local refs configured for 'git push':
dev-mdbootstrap pushes to dev-mdbootstrap (up to date)
master pushes to master (up to date)
C:\dev> git ls-remote origin
2ac96d0912e126f5ccaafef66133e3fbc3f23f1d HEAD
bd3366870b7c6848c7d182e971307b9a74e0ae48 refs/heads/dev-mdbootstrap
2ac96d0912e126f5ccaafef66133e3fbc3f23f1d refs/heads/master
我很困惑為什么git branch -a仍然列出localrepo并且不顯示本地分支dev-mdbootstrap跟蹤remotes/origin/dev-mdbootstrap。
需要對該存盤庫的歷史進行解釋。
我最初克隆了一個本地git 存盤庫,創建了一個名為 的新分支dev-mdbootstrap,然后開始作業。所以那時origin是本地存盤庫。
后來,我想將現有的 github.com 存盤庫配置為origin,所以我做了以下操作:
C:\dev> git remote rename origin localrepo
C:\dev> git remote add -t master origin https://github.com/MyOrganization/MyProject.git
所有這一切似乎作業得很好。(https://github.com/MyOrganization/MyProject.git遠程以前只包含master分支。)
我將本地dev-mdbootstrap分支推送到新配置的 GitHuborigin存盤庫:
C:\dev> git push -u origin dev-mdbootstrap:dev-mdbootstrap
Counting objects: 100% (59/59), done.
[...snipped...]
Branch 'dev-mdbootstrap' set up to track remote branch 'dev-mdbootstrap' from 'origin'.
最終,我決定洗掉localrepo存盤庫:
git remote rm localrepo
我很不解為什么git branch -a還要顯示localrepo。我怎樣才能理順它?
謝謝!
uj5u.com熱心網友回復:
在 Git 內部,遠程跟蹤名稱的存在(和存盤的值)與遠程本身是否存在無關。也就是說,它們只是符合模式的文本字串,其中是遠程名稱,并且是在該遠程上找到的分支名稱。refs/remotes/r/namername
git remote更新遙控器的命令—— add、、rename和rm——應該在適當的子命名空間中管理遠程跟蹤名稱。所以當你跑的時候git remote rm localrepo,這應該已經洗掉了所有的refs/remotes/localrepo/*名字。顯然沒有;追查為什么沒有——錯誤在哪里——需要一個復制器。
您可以使用低級“管道”命令手動洗掉每個“壞”名稱git update-ref,該命令繞過所有正常機制并直接進入 Git 內部:
git update-ref -d refs/remotes/localrepo/HEAD
git update-ref -d refs/remotes/localrepo/dev-mdbootstrap
git update-ref -d refs/remotes/localrepo/master
我們可能永遠不會知道為什么git remote rm不在這里自行清理。
(我懷疑您可能已設定origin為單分支遙控器。請檢查:
git config --get-all remotes.origin.fetch
輸出通常應該是一行:
refs/heads/*:refs/remotes/origin/*
其他任何內容都表明設定例外。不一定是錯的,只是不尋常。)
uj5u.com熱心網友回復:
你試過運行 git prune 嗎?
git remote prune origin
您可以使用 --dry-run 查看將修剪哪些分支
git remote prune origin --dry-run
uj5u.com熱心網友回復:
@torek讓我走上了正確的道路。非常感謝!不過,完整的清理作業涉及更多,所以我想記錄它以防它對某人有所幫助。(請注意,因為我不確定我是否走在正確的道路上,所以.git在繼續之前我對我的 repo 檔案夾進行了完整的備份。)
從我在原始問題中注意到的這個加擾的分支配置開始:
C:\dev> git branch -a
* dev-mdbootstrap
master
remotes/localrepo/HEAD -> localrepo/master
remotes/localrepo/dev-mdbootstrap
remotes/localrepo/master
remotes/origin/master
根據@torek 的指導,我跑了:
C:\dev> git update-ref -d refs/remotes/localrepo/HEAD
C:\dev> git update-ref -d refs/remotes/localrepo/dev-mdbootstrap
C:\dev> git update-ref -d refs/remotes/localrepo/master
這導致了以下情況。注意ignoring broken ref警告資訊:
C:\dev> git branch -a
warning: ignoring broken ref refs/remotes/localrepo/HEAD
* dev-mdbootstrap
master
remotes/origin/master
我摸索著試圖解決這個問題。首先git config,我按照@torek 的建議重新檢查了。看著git config --get-all ...:
C:\dev> git config --get-all remote.origin.fetch
refs/heads/*:refs/remotes/localrepo/*
refs/heads/dev-mdbootstrap:refs/remotes/origin/dev-mdbootstrap
由于localrepo仍然出現在配置中并且我不確定哪些單獨的git config ...命令會清理它,我只是跑到我的文本編輯器git config -e中編輯.git\config檔案。最初,該檔案包含:
[core]
...various settings... snipped out...
[remote "origin"]
url = https://github.com/MyOrganization/MyProject.git
fetch = refs/heads/*:refs/remotes/localrepo/*
fetch = refs/heads/dev-mdbootstrap:refs/remotes/origin/dev-mdbootstrap
[branch "dev-mdbootstrap"]
remote = origin
merge = refs/heads/dev-mdbootstrap
[branch "master"]
remote = origin
merge = refs/heads/master
我將該部分簡化為[remote "origin"]以下內容,并保持其他所有內容不變:
[remote "origin"]
url = https://github.com/MyOrganization/MyProject.git
fetch = refs/heads/*:refs/remotes/origin/*
但是,這對git branch -a輸出沒有影響。
我直接檢查了C:\dev\.git\refs\remotes檔案夾。它包含一個origin和一個localrepo檔案夾。后者包含一個以HEAD內容命名的檔案:
ref: refs/remotes/localrepo/master
I poked around for a way to clear this out using a git update-ref -d ... command, but nothing I tried worked, so I finally just deleted the ``C:\dev.git\refs\remotes\localrepo` folder manually.
This helped! After this git branch -a and git branch -vv returned the following:
git branch -a
* dev-mdbootstrap
master
remotes/origin/master
PS C:\dev> git branch -vv
* dev-mdbootstrap bd336687 [origin/dev-mdbootstrap: gone] ...snipped commit message #1...
master 2ac96d09 [origin/master] ...snipped commit message #1...
But why did this show "origin/dev-mdbootstrap: gone"? I tried git remote show origin, which returned the following. Note the line dev-mdbootstrap new (next fetch will store in remotes/origin).
C:\dev> git remote show origin
* remote origin
Fetch URL: https://github.com/MyOrganization/MyProject.git
Push URL: https://github.com/MyOrganization/MyProject.git
HEAD branch: master
Remote branches:
dev-mdbootstrap new (next fetch will store in remotes/origin)
master tracked
Local branches configured for 'git pull':
dev-mdbootstrap merges with remote dev-mdbootstrap
master merges with remote master
Local refs configured for 'git push':
dev-mdbootstrap pushes to dev-mdbootstrap (up to date)
master pushes to master (up to date)
It seems that during the time my git configuration was scrambled, my earlier commands such as git fetch origin dev-mdbootstrap and others to try to force the local dev-mdbootstrap branch to track origin/dev-mdbootstrap were simply having no effect.
But since git indicated a next fetch was needed, I ran:
C:\dev> git fetch origin
From https://github.com/MyOrganization/MyProject
* [new branch] dev-mdbootstrap -> origin/dev-mdbootstrap
and then:
C:\dev> git branch --set-upstream-to=origin/dev-mdbootstrap dev-mdbootstrap
Branch 'dev-mdbootstrap' set up to track remote branch 'dev-mdbootstrap' from 'origin'.
Finally, 'git branch -a -vv' returns:
C:\dev> git branch -a -vv
* dev-mdbootstrap bd336687 [origin/dev-mdbootstrap] ...snipped commit message #1...
master 2ac96d09 [origin/master] ...snipped commit message #2...
remotes/origin/dev-mdbootstrap bd336687 ...snipped commit message #1...
remotes/origin/master 2ac96d09 ...snipped commit message #2...
Now everything looks clean, and git is behaving as I normally expect it to. Again, props to @torek for setting me on the right course. This may not be the optimal or most correct way to fix this, but it got the job done.
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/435162.html
標籤:混帐 github 分支 追踪 git-remote
