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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:23:17  来源:igfitidea点击:

How can I push to my fork from a clone of the original repo?

gitgithub

提问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 masterbranch 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 pushattempts to push your local masterbranch to the masterbranch 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

你需要

  1. either redefine the originremote to be associated with your fork, by running

    git remote set-url origin https://github.com/RemiB/myrepo.git
    
  2. or, if you want to preserve the original definition of the originremote, define a new remote (called myrepo, 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 masterbranch to your fork by running

    git push myrepo master
    

    And if you want to tell Git that git pushshould push to myrepoinstead of originfrom now on, you should run

    git push -u myrepo master
    
  1. 通过运行重新定义origin要与您的叉关联的遥控器

    git remote set-url origin https://github.com/RemiB/myrepo.git
    
  2. 或者,如果您想保留origin遥控器的原始定义,请定义一个myrepo与您的 fork 关联的新遥控器(此处称为):

    git remote add myrepo https://github.com/RemiB/myrepo.git
    

    然后你应该能够master通过运行将你的本地分支推送到你的分支

    git push myrepo master
    

    如果你想告诉 Gitgit 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.gitis 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.

您绝对有权推送到此存储库。如果要将代码推送到原始存储库,可以发出拉取请求。