Git/GitHub无法推送到主分支

155

我是一名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 git@github.com:my_user_name/my_repo.git

所有的Github页面底部都有一个帮助链接(http://help.github.com/)。该帮助描述了许多主题,包括此主题。我建议先阅读这些内容,然后针对您不理解的特定问题提出问题。 - jamessan
21
如果Jamessan指出了帮助页面中的特定位置,那会更有用。 - Deonomo
1
可能是重复的问题:git github 无法推送到 origin - gion_13
如果您正在阅读此内容,且是2022年3月17日,请检查Down Detector。Github出现故障。 - Peter Cotton
11个回答

249

GitHub不支持通过Git协议进行推送,这是由您使用以git://开头的URL所指示的。正如错误消息所说,如果您想要推送,您应该使用SSH URL git@github.com:my_user_name/my_repo.git 或者使用GitHub为您的存储库显示的https:// URL的“智能HTTP”协议。

(更新:让我惊讶的是,一些人显然认为我是在暗示“https”是“智能HTTP”,但我并没有这样做。Git曾经有一个“愚蠢的HTTP”协议,在引入GitHub使用的“智能HTTP”之前不允许推送-可以在httphttps上使用任何一种协议。Git使用的传输协议之间的差异在下面的链接中解释。)

如果您想更改源的URL,只需执行以下操作:

git remote set-url origin git@github.com:my_user_name/my_repo.git
或者
git remote set-url origin https://github.com/my_user_name/my_repo.git

更多信息可在10.6 Git Internals - Transfer Protocols中获取。


1
好的,你必须先在GitHub上创建存储库,然后才能将代码推送到它上面。当你这样做时,它会给你提供有关如何克隆或推送到存储库的说明。 - Mark Longair
很酷。我也是Github的新手。我的第一次探索Github非常成功。当我尝试为另一个项目第二次使用它时,我遇到了相同的错误,显示“github错误:.git不存在。您输入正确吗?”在阅读Mark Longair的建议后,我在Github上创建了一个新的repo。我通过这个链接http://help.github.com/create-a-repo/完成的。在您的Github帐户中创建新存储库的直接链接是https://github.com/repositories/new。这很顺利 :) - itsraghz
1
我之前也遇到了同样的问题。实际上,问题在于你在github.com和你的用户名之间使用了斜杠“/”,而应该使用冒号“:”。这就是问题所在:D - Wilmer E. Henao
2
@WilmerEHenaoH:那可能是你的问题,但它不是问题或我的答案中的问题 ;)(仅供参考,有时会混淆git中两种SSH URL样式,其中一种使用冒号分隔主机名和路径,另一种则不使用。) - Mark Longair
@Rubens,因为这个答案是错误的。请停止给它点赞。 - Cerin
显示剩余7条评论

38

使用Mark Longair的答案,但请确保使用存储库的HTTPS链接:

git remote set-url origin https://github.com/my_user_name/my_repo.git

您可以使用git push origin master命令。


1
它对我有效。然后只需键入 git push 也可以工作。 - youngzy

13

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 = git@github.com:my_user_name/my_repo.git

(反过来,git remote set-url... 命令会产生上述变化。)


2

对于刚接触这个的人,有一个简单的解决方案:

编辑本地 .git 目录下的配置文件(config)。将下面的 git: 改为 https:

[remote "origin"]
    url = https://github.com/your_username/your_repo

1
不正确,https协议将不允许推送。请参见:“Pro Git”书,第4.1.4节“HTTP / S协议”。链接:http://git-scm.com/book/en/v2 - Kevin J. Rice
@KevinJ.Rice:嗯,哪里说了那个?Github使用智能HTTP协议,这允许推送得非常好。 - Martijn Pieters

1
当您使用类似以下调用克隆存储库时,会出现此错误:
git clone git://github.com/....git

这基本上将您设置为仅拉取用户,无法推送更改。
我通过打开我的存储库的.git/config文件并更改以下行来解决此问题:
[remote "origin"]
    url = git://github.com/myusername/myrepo.git

到:

[remote "origin"]
    url = ssh+git://git@github.com/myusername/myrepo.git

这个使用ssh+git协议和git用户的身份验证机制是Github首选的身份验证方式。其他答案在技术上都可以工作,但它们似乎都绕过了ssh,需要您手动输入密码,这可能不是您想要的。

1

在升级Git客户端后,我遇到了这个问题,我的代码库突然无法推送。

我发现一些旧的远程仓库url的值是错误的,尽管我的当前活动的远程仓库url的值相同且正常工作。

但是也有pushurl参数,所以为旧的远程仓库添加它对我有用:

之前:

[remote "origin"]
    url = git://github.com/user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    pushurl = git@github.com:user/repo.git
< p > 注意:这个"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 = git@github.com:user/repo.git

1
以下命令将解决问题。
 git pull --rebase
 git push

0

我将我的公钥添加到了github.com,操作成功:

ssh -T git@github.com

但是在错误地执行了以下操作后,我收到了“无法推送”的错误:

git clone git://github.com/mygithubacct/dotfiles.git
git remote add origin git@github.com:mygithubacct/dotfiles.git
...edit/add/commit
git push origin master

与其做我应该做的事情:

mkdir dotfiles
cd dotfiles
git init
git remote add origin git@github.com:mygithubacct/dotfiles.git
git pull origin master
...edit/add/commit
git push origin master

0

0

将全局的 https 设置为 git://

git config --global url.https://github.com/.insteadOf git://github.com/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接