[英]go get results in 'terminal prompts disabled' error for github private repo
我從我的瀏覽器使用 Github UI 創建了私有 repo examplesite/myprivaterepo。
然后我轉到我的 go 目錄(在桌面上)並克隆它:
$ cd $GOPATH
$ go get github.com/examplesite/myprivaterepo
到目前為止,一切都很好。 創建文件 scheduler.go,添加到 repo,並推送。
$ vim scheduler.go
$ git add scheduler.go
$ git commit
$ git push
一切都好。 但是,當我使用一台干凈的筆記本電腦並嘗試克隆存儲庫時,出現了一個錯誤:
# Now on laptop, which doesn't yet know about the repo
$ cd $GOPATH
$ go get github.com/examplesite/myprivaterepo
# At this point it should ask for my user ID and password ,right? But it doesn't.
# Instead, this error occurs:
cd .; git clone https://github.com/examplesite/myprivaterepo /Users/tom/go/src/github.com/examplesite/myprivaterepo
Cloning into '/Users/tom/go/src/github.com/examplesite/myprivaterepo'...
fatal: could not read Username for 'https://github.com': terminal prompts disabled
package github.com/examplesite/myprivaterepo: exit status 128
為什么我的筆記本電腦討厭我自己的回購,我怎樣才能讓它接受它的命運? 謝謝。
我發現這非常有幫助,它解決了我的問題。 此命令將允許您的 2FA 執行其操作(並省去輸入用戶名和密碼的麻煩):
對於 Github:
git config --global --add url."git@github.com:".insteadOf "https://github.com/"
對於 Gitlab:
git config --global --add url."git@gitlab.com:".insteadOf "https://gitlab.com/"
來源: http ://albertech.blogspot.com/2016/11/fix-git-error-could-not-read-username.html
如果您不使用 2FA,您仍然可以使用 SSH,這將起作用。
結果.gitconfig
將如下所示:
[url "git@github.com:"]
insteadOf = https://github.com/
go get 默認禁用“終端提示”。 這可以通過設置 git 的環境變量來改變:
env GIT_TERMINAL_PROMPT=1 go get github.com/examplesite/myprivaterepo
它抱怨是因為它需要使用ssh
而不是https
,但你的 git 仍然配置了 https。 所以基本上就像前面提到的其他人一樣,您需要啟用提示或將 git 配置為使用ssh
而不是https
。 通過運行以下命令來執行此操作的簡單方法:
git config --global --add url."git@github.com:".insteadOf "https://github.com/"
或者如果您已經在機器中使用ssh
和git
,您可以安全地編輯~/.gitconfig
並在最底部添加以下行
注意:這涵蓋了所有 SVC,源代碼版本控制,這取決於你到底使用什么,github,gitlab,bitbucket)
# Enforce SSH
[url "ssh://git@github.com/"]
insteadOf = https://github.com/
[url "ssh://git@gitlab.com/"]
insteadOf = https://gitlab.com/
[url "ssh://git@bitbucket.org/"]
insteadOf = https://bitbucket.org/
如果要禁用密碼提示,則需要緩存密碼。 有關如何在 mac、windows 或 linux 上緩存 github 密碼的更多信息,請訪問此頁面。
有關如何將 ssh 添加到您的 github 帳戶的更多信息,請訪問此頁面。
此外,更重要的是,如果這是公司或您自己的私有存儲庫,您可能需要跳過對此類存儲庫使用代理或校驗和數據庫以避免公開暴露它們。
為此,您需要設置GOPRIVATE
環境變量來控制 go 命令認為哪些模塊是私有的(不公開),因此不應使用代理或校驗和數據庫。
該變量是以逗號分隔的模式列表(與 Go 的path.Match
語法相同)的模塊路徑前綴。 例如,
export GOPRIVATE=*.corp.example.com,github.com/mycompany/*
或者
go env -w GOPRIVATE=github.com/mycompany/*
最后一件事不要忘記提及,您仍然可以配置go get
以通過https
進行身份驗證和獲取,您需要做的就是將以下行添加到$HOME/.netrc
machine github.com login USERNAME password APIKEY
我希望這有助於社區並節省其他人的時間來快速解決所描述的問題。 如果您需要更多支持或幫助,請隨時發表評論。
1st -- go get
將拒絕在命令行上進行身份驗證。 因此,您需要在 git 中緩存憑據。 因為我使用 osx,所以我可以使用 osxkeychain 憑證助手。
第二對我來說,我啟用了 2FA,因此無法使用我的密碼進行身份驗證。 相反,我必須生成一個個人訪問令牌來代替密碼。
git clone https://github.com/user/private_repo
並使用你的 github.com 用戶名作為用戶名,並使用生成的個人訪問令牌作為密碼。 刪除了剛剛克隆的 repo 並重新測試以確保 creds 被緩存 - git clone https://github.com/user/private_repo
並且這次沒有要求 creds。
如果您只想go get
開始工作,並與您的工作一起前進...
只需導出GIT_TERMINAL_PROMPT=1
$ export GIT_TERMINAL_PROMPT=1
$ go get [whatever]
它現在將提示您為 shell 會話的其余部分輸入用戶/通行證。 將其放入您的.profile
或設置git
以獲得更永久的解決方案。
從 go 1.13 開始,如果您已經使用 git 憑據配置了終端但仍面臨此問題,那么您可以嘗試設置GOPRIVATE
環境變量。 設置這個環境變量為我解決了這個問題。
export GOPRIVATE=github.com/{organizationName/userName of the package}/*
添加這是您的 bash_profile:
export GOPRIVATE=github.com/your-organization/*
然后運行:
git config --global --add url."git@github.com:".insteadOf "https://github.com/"
如果你用這個選項配置你的 gitconfig,你以后在克隆 GitHub 的其他 repos 時會遇到問題
git config --global --add url. "Git@github.com". Instead, "https://github.com/"
相反,我建議您使用此選項
echo "export GIT_TERMINAL_PROMPT=1" >> ~/.bashrc || ~/.zshrc
並且不要忘記從您的私人存儲庫生成訪問令牌。 當提示輸入密碼時,只需粘貼訪問令牌。 快樂克隆 :)
雖然有很多關於 GitHub 使用 SSH 身份驗證的答案,但問題與 go 模塊的正確用法有關。 為此,Luna Lovegood 在這里提供了正確的答案。
重申一下,默認情況下,Go 嘗試使用公共存儲庫來下載包。 我們需要指定它使用 SSH 對私有存儲庫進行身份驗證。 為此,可以使用以下內容:
go env -w GOPRIVATE=github.com/{your-username}/{your-lib}
.
.
go mod vendor # this will now work for your private lib
Glob 模式也可以使用,因此如果您有多個私有存儲庫,則可以添加
go env -w GOPRIVATE=github.com/{your-username}/*
編輯:正確的措辭。
我在windows上遇到了同樣的問題“錯誤:無法執行提示腳本(退出代碼1)致命:無法讀取' https://github.com '的用戶名:沒有錯誤”試圖通過登錄對話框登錄到github。 當我取消對話框時,git 在命令行中詢問我的登錄名和密碼,它工作正常。
我在更改憑據后遇到此錯誤terminal prompts disabled
,Carthage 無法更新依賴項並在終端上顯示此錯誤。
我只是做了簡單的以下步驟,它開始工作了......
keychain access
中刪除舊憑據我嘗試添加此命令
導出 GOPRIVATE=github.com/{private_reponame}/*
它對我有用
另一種選擇是將憑據添加為 URL 的一部分。
例如,要訪問 GitLab 上的存儲庫,我對 URL 使用以下格式:
https://<user>:<access-token>@<gitlab-server>/<path-to-repo>.git
我了解到,為了節省 GitHub 的時間,它有助於下載 CLI gh
有了它,我使用了 auth 命令,它讓我的生活更輕松
gh auth login
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.