我是 Git/GitHub 新手,遇到了一个问题。我创建了一个测试项目并将其添加到本地存储库。现在我正在尝试将文件/项目添加到远程存储库。
这就是我所做的(并且有效)-
git remote add origin git://github.com/my_user_name/my_repo.git
现在,当我尝试使用以下命令将存储库推送到 GitHub 时,出现以下错误 -
git push origin master
错误-
fatal: remote error:
You can't push to git://github.com/my_user_name/my_repo.git
Use [email protected]:my_user_name/my_repo.git
GitHub 不支持通过 Git 协议推送,这通过您使用以
git://
开头的 URL 来表明。正如错误消息所示,如果您想推送,您应该使用 SSH URL [email protected]:my_user_name/my_repo.git
或使用 GitHub 向您显示的存储库的 https://
URL 来使用“智能 HTTP”协议。
(更新:令我惊讶的是,有些人显然认为我这样建议“https”意味着“智能 HTTP”,但我不是。Git 曾经有一个“哑 HTTP”协议,不允许这样做在引入 GitHub 使用的“智能 HTTP”之前推送 - 可以通过
http
或 https
使用。Git 使用的传输协议之间的差异在下面的链接中进行了解释。)
如果你想更改原始URL,你可以这样做:
git remote set-url origin [email protected]:my_user_name/my_repo.git
或
git remote set-url origin https://github.com/my_user_name/my_repo.git
更多信息请参阅 10.6 Git 内部结构 - 传输协议。
使用 Mark Longair 的答案,但请确保使用存储库的 HTTPS 链接:
git remote set-url origin https://github.com/my_user_name/my_repo.git
然后就可以使用
git push origin master
。
Mark Longair 使用
git remote set-url...
的解决方案非常清晰。您还可以通过直接编辑 .git/config 文件的这一部分来获得相同的行为:
之前:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git://github.com/my_user_name/my_repo.git
之后:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:my_user_name/my_repo.git
(相反,
git remote set-url...
调用会产生上述变化。)
对于新手来说,有一个简单的解决方案:
编辑本地 .git 目录中的配置文件 (
config
)。将下面的 git:
更改为 https:
。
[remote "origin"]
url = https://github.com/your_username/your_repo
以下命令将解决该问题。
git pull --rebase
git push
我遇到了这个问题升级 Git 客户端后,突然我的存储库无法推送。
我发现一些旧遥控器的
url
值错误,即使我当前活动的遥控器具有相同的 url
值并且工作正常。
但是还有
pushurl
参数,因此为旧遥控器添加它对我有用:
之前:
[remote "origin"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = [email protected]:user/repo.git
注意:文件“config”的这部分已经使用很长时间了,但是新客户抱怨 URL 错误:
[remote "composer"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/composer/*
所以我将
pushurl
参数添加到旧遥控器中:
[remote "composer"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/composer/*
pushurl = [email protected]:user/repo.git
当您使用如下调用克隆存储库时,会发生此错误:
git clone git://github.com/....git
这本质上将您设置为仅拉动用户,无法推送更改。
我通过打开我的存储库的
.git/config
文件并更改行来修复此问题:
[remote "origin"]
url = git://github.com/myusername/myrepo.git
至:
[remote "origin"]
url = ssh+git://[email protected]/myusername/myrepo.git
与
ssh+git
用户的此 git
协议是 Github 首选的身份验证机制。
这里提到的其他答案在技术上是可行的,但它们似乎都绕过了 ssh,要求您手动输入密码,而您可能不希望这样做。
如果您访问 http://github.com/my_user_name/my_repo,您将看到一个文本框,您可以在其中选择存储库的 git 路径。你会想用这个!
我将我的公钥添加到 github.com 并且成功了:
ssh -T [email protected]
但是在错误地执行此操作后,我收到了“你无法推送”错误:
git clone git://github.com/mygithubacct/dotfiles.git
git remote add origin [email protected]:mygithubacct/dotfiles.git
...edit/add/commit
git push origin master
而不是做我应该做的事:
mkdir dotfiles
cd dotfiles
git init
git remote add origin [email protected]:mygithubacct/dotfiles.git
git pull origin master
...edit/add/commit
git push origin master
要全局设置
https
而不是 git://
:
git config --global url.https://github.com/.insteadOf git://github.com/
100% 成功解决 github 的任何问题。 https://www.youtube.com/watch?v=V8WbIERFOJU
克服它的最快方法是将
origin
替换为它给出的建议。
代替
git push origin master
,使用:
git push [email protected]:my_user_name/my_repo.git master