Git/GitHub 无法推送到 master
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7548661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git/GitHub can't push to master
提问by kapso
I am new to Git/GitHub and ran into an issue. I created a test project and added it to the local repository. Now I am trying to add files/project to the remote repository.
我是 Git/GitHub 的新手,遇到了一个问题。我创建了一个测试项目并将其添加到本地存储库。现在我正在尝试将文件/项目添加到远程存储库。
Here's what I did (and this worked) -
这就是我所做的(并且有效)-
git remote add origin git://github.com/my_user_name/my_repo.git
Now when I try to push the repository to GitHub, using the following command, I get the following error -
现在,当我尝试使用以下命令将存储库推送到 GitHub 时,出现以下错误 -
git push origin master
Error -
错误 -
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
回答by Mark Longair
GitHub doesn't support pushing over the Git protocol, which is indicated by your use of the URL beginning git://
. As the error message says, if you want to push, you should use either the SSH URL [email protected]:my_user_name/my_repo.git
or the "smart HTTP" protocol by using the https://
URL that GitHub shows you for your repository.
GitHub 不支持推送 Git 协议,这通过您使用的 URL 开头来表示git://
。正如错误消息所说,如果你想推送,你应该使用 SSH URL[email protected]:my_user_name/my_repo.git
或“智能 HTTP”协议,使用https://
GitHub 为你的存储库显示的URL。
(Update: to my surprise, some people apparently thought that by this I was suggesting that "https" means "smart HTTP", which I wasn't. Git used to have a "dumb HTTP" protocol which didn't allow pushing before the "smart HTTP" that GitHub uses was introduced - either could be used over either http
or https
. The differences between the transfer protocols used by Git are explained in the link below.)
(更新:令我惊讶的是,有些人显然认为我在暗示“https”意味着“智能 HTTP”,我不是。Git 曾经有一个“愚蠢的 HTTP”协议,以前不允许推送引入了 GitHub 使用的“智能 HTTP” - 可以在http
或上使用。Git 使用https
的传输协议之间的差异在下面的链接中进行了解释。)
If you want to change the URL of origin, you can just do:
如果要更改原始 URL,只需执行以下操作:
git remote set-url origin [email protected]:my_user_name/my_repo.git
or
或者
git remote set-url origin https://github.com/my_user_name/my_repo.git
More information is available in 10.6 Git Internals - Transfer Protocols.
回答by Abdo
Use Mark Longair's answer, but make sure to use the HTTPS link to the repository:
使用Mark Longair 的回答,但请确保使用指向存储库的 HTTPS 链接:
git remote set-url origin https://github.com/my_user_name/my_repo.git
You can use then git push origin master
.
你可以使用然后git push origin master
。
回答by PurpleHymanet
Mark Longair's solution using git remote set-url...
is quite clear. You can also get the same behavior by directly editing this section of the .git/config file:
Mark Longair 使用的解决方案git remote set-url...
非常清楚。您也可以通过直接编辑 .git/config 文件的这一部分来获得相同的行为:
before:
前:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = git://github.com/my_user_name/my_repo.git
after:
后:
[remote "origin"]
fetch = +refs/heads/*:refs/remotes/origin/*
url = [email protected]:my_user_name/my_repo.git
(And conversely, the git remote set-url...
invocation produces the above change.)
(相反,git remote set-url...
调用会产生上述变化。)
回答by ParamQuery
There is a simple solution to this for someone new to this:
对于新手来说,有一个简单的解决方案:
Edit the configuration file in your local .git directory (config
). Change git:
to https:
below.
编辑本地 .git 目录 ( config
) 中的配置文件。更改git:
到https:
下面。
[remote "origin"]
url = https://github.com/your_username/your_repo
回答by ParamQuery
I had this issue after upgrading the Git client, and suddenly my repository could not push.
升级 Git 客户端后我遇到了这个问题,突然我的存储库无法推送。
I found that some old remote had the wrong value of url
, even through my currently active remote had the same value for url
and was working fine.
我发现一些旧遥控器的值错误url
,即使我当前活动的遥控器具有相同的值url
并且工作正常。
But there was also the pushurl
param, so adding it for the old remote worked for me:
但也有pushurl
参数,所以为旧遥控器添加它对我有用:
Before:
前:
[remote "origin"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
pushurl = [email protected]:user/repo.git
NOTE: This part of file "config" was unused for ages, but the new client complained about the wrong URL:
注意:文件“config”的这部分已经很久没有使用了,但是新客户抱怨错误的 URL:
[remote "composer"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/composer/*
So I added the pushurl
param to the old remote:
所以我将pushurl
参数添加到旧遥控器:
[remote "composer"]
url = git://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/composer/*
pushurl = [email protected]:user/repo.git
回答by Cerin
This error occurs when you clone a repo using a call like:
当您使用如下调用克隆存储库时会发生此错误:
git clone git://github.com/....git
This essentially sets you up as a pull-only user, who can't push up changes.
这本质上将您设置为只能拉取的用户,无法推送更改。
I fixed this by opening my repo's .git/config
file and changing the line:
我通过打开我的 repo.git/config
文件并更改行来解决此问题:
[remote "origin"]
url = git://github.com/myusername/myrepo.git
to:
到:
[remote "origin"]
url = ssh+git://[email protected]/myusername/myrepo.git
This ssh+git
protocol with the git
user is the authentication mechanism preferred by Github.
这种ssh+git
与git
用户的协议是 Github 首选的身份验证机制。
The other answers mentioned here technically work, but they all seem to bypass ssh, requiring you to manually enter a password, which you probably don't want.
这里提到的其他答案在技术上是有效的,但它们似乎都绕过了 ssh,需要您手动输入密码,而您可能不想要。
回答by tokhi
To set https
globally instead of git://
:
https
全局设置而不是git://
:
git config --global url.https://github.com/.insteadOf git://github.com/
回答by Hariharan AR
The below cmnds will fix the issue.
下面的命令将解决这个问题。
git pull --rebase
git push
回答by CambridgeMike
If you go to http://github.com/my_user_name/my_repoyou will see a textbox where you can select the git path to your repository. You'll want to use this!
如果您访问http://github.com/my_user_name/my_repo,您将看到一个文本框,您可以在其中选择存储库的 git 路径。你会想要使用这个!
回答by rshdev
I added my pubkey to github.com and this was successful:
我将我的公钥添加到 github.com 并且这是成功的:
ssh -T [email protected]
But I received the "You can't push" error after having wrongly done this:
但是在错误地执行此操作后,我收到了“您无法推送”错误:
git clone git://github.com/mygithubacct/dotfiles.git
git remote add origin [email protected]:mygithubacct/dotfiles.git
...edit/add/commit
git push origin master
Instead of doing what I should have done:
而不是做我应该做的:
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