git 如何从原始存储库的克隆推送到我的分叉?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25545613/
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
How can I push to my fork from a clone of the original repo?
提问by Rémi Becheras
I created a fork (let's call it myrepo
) of another repository (let's call it orirepo
) on GitHub. Later, I cloned orirepo
.
我在 GitHub 上创建了myrepo
另一个存储库(我们称之为orirepo
)的分支(我们称之为)。后来,我克隆了orirepo
.
git clone https://github.com/original/orirepo.git
I modified about 20 files, then I staged my change and made a commit
我修改了大约 20 个文件,然后我进行了更改并进行了提交
git add
git commit
However, when I tried to push
然而,当我试图推动
git push
I got this error:
我收到此错误:
remote: Permission to original/orirepo.git denied to mylogin.
fatal: unable to access 'https://github.com/original/orirepo.git/': The requested URL returned error: 403
I know I made a mistake: I should have cloned my fork rather than orirepo
, but it's too late for that now.
How could I push to my fork rather than to origin/orirepo
, which I don't have write access to?
我知道我犯了一个错误:我应该克隆我的 fork 而不是orirepo
,但现在为时已晚。我怎么能推送到我的 fork 而不是 to origin/orirepo
,我没有写权限?
回答by jub0bs
By default, when you clone a repository
默认情况下,当您克隆存储库时
- that resides at
https://github.com/original/orirepo.git
, - whose current branch is called
master
,
- 驻留在
https://github.com/original/orirepo.git
, - 其当前分支称为
master
,
then
然后
- the local config of the resulting clone lists only one remote called
origin
, which is associated with the URL of the repository you cloned; - the local
master
branch in your clone is set to trackorigin/master
.
- 结果克隆的本地配置仅列出了一个名为 的远程
origin
,它与您克隆的存储库的 URL 相关联; master
克隆中的本地分支设置为trackorigin/master
。
Therefore, if you don't modify the config of your clone, Git interprets
因此,如果您不修改克隆的配置,Git 会解释
git push
as
作为
git push origin master:origin/master
In other words, git push
attempts to push your local master
branch to the master
branch that resides on the remote repository (known by your clone as origin
). However, you're not allowed to do that, because you don't have write access to that remote repository.
换句话说,git push
尝试将您的本地master
分支推送到master
驻留在远程存储库上的分支(您的克隆称为origin
)。但是,您不能这样做,因为您没有对该远程存储库的写访问权限。
You need to
你需要
either redefine the
origin
remote to be associated with your fork, by runninggit remote set-url origin https://github.com/RemiB/myrepo.git
or, if you want to preserve the original definition of the
origin
remote, define a new remote (calledmyrepo
, here) that is associated to your fork:git remote add myrepo https://github.com/RemiB/myrepo.git
Then you should be able to push your local
master
branch to your fork by runninggit push myrepo master
And if you want to tell Git that
git push
should push tomyrepo
instead oforigin
from now on, you should rungit push -u myrepo master
通过运行重新定义
origin
要与您的叉关联的遥控器git remote set-url origin https://github.com/RemiB/myrepo.git
或者,如果您想保留
origin
遥控器的原始定义,请定义一个myrepo
与您的 fork 关联的新遥控器(此处称为):git remote add myrepo https://github.com/RemiB/myrepo.git
然后你应该能够
master
通过运行将你的本地分支推送到你的分支git push myrepo master
如果你想告诉 Git
git push
应该推送到myrepo
而不是origin
从现在开始,你应该运行git push -u myrepo master
instead.
反而。
回答by Ahmad Awais
So, you cloned someone's repo made the changes and then realized you can't push to that repo, but you can push to your own fork. So, you went ahead and forked the original repo.
因此,您克隆了某人的存储库进行了更改,然后意识到您无法推送到该存储库,但您可以推送到您自己的分支。所以,你继续并分叉了原始回购。
All you have to do is swap the origin URL in your local clone with the URL of your forked repo.
您所要做的就是将本地克隆中的原始 URL 与分叉存储库的 URL 交换。
Do it like this
像这样做
git remote set-url origin https://github.com/fork/name.git
Where https://github.com/fork/name.git
is the URL of your fork from the original repo.
https://github.com/fork/name.git
原始 repo 中的 fork 的 URL在哪里。
After that, just
之后,只要
git push
and you'll be able to push your changes to your fork :)
并且您将能够将您的更改推送到您的 fork :)
回答by Rémi Becheras
Okay I finally edited my git config file :
好的,我终于编辑了我的 git 配置文件:
$ nano .git/config
changing :
改变:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/<origin-project>/<origin-repo>.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
to
到
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = https://github.com/<mylogin>/<myrepo>.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Then,
然后,
$ git push
Worked like a charm.
像魅力一样工作。
Or, thanks to Thiago F Macedo answer:
或者,感谢Thiago F Macedo 的回答:
git remote set-url origin https://[email protected]/user/repo.git
回答by LeonF
You should clone the forked repo in your account first.
您应该首先在您的帐户中克隆分叉的存储库。
git clone https://github.com/your_account/repo.git
You absolutely have permissions to push to this repo. If you want to push your code to original repo, You can issue a pull request.
您绝对有权推送到此存储库。如果要将代码推送到原始存储库,可以发出拉取请求。