191

I created a fork (let's call it myrepo) of another repository (let's call it orirepo) on GitHub. Later, I cloned orirepo.

git clone https://github.com/original/orirepo.git

I modified about 20 files, then I staged my change and made a commit

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?

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81

6 Answers6

277

By default, when you clone a repository

  • that resides at https://github.com/original/orirepo.git,
  • whose current branch is called 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 track origin/master.

Therefore, if you don't modify the config of your clone, Git interprets

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.

You need to

  1. either redefine the origin remote 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 origin remote, 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 master branch to your fork by running

    git push myrepo master
    

    And if you want to tell Git that git push should push to myrepo instead of origin from now on, you should run

    git push -u myrepo master
    

instead.

jub0bs
  • 60,866
  • 25
  • 183
  • 186
  • will ´git push -u´ also change the default behavior of ´git pull´? – benroth Mar 15 '17 at 14:38
  • 2
    Yes it will, so instead of doing "push -u", you should use pushDefault option: git config --add remote.origin.pushdefault myrepo and it will only affect pushing, and applies to all existing an new branches. – Marius K Aug 04 '17 at 10:34
  • 3
    Did `git remote set-url origin http://github.com/myname/reponame` and then `git push origin mybranch`. Worked! Thank you! – Developer Marius Žilėnas Dec 12 '17 at 12:58
  • 1
    If add remote is used, how does it affect pull? Where does it pull from? How does it handle future merging between the 2 repos? – Kok How Teh Nov 30 '18 at 13:38
  • 1
    @KokHowTeh Adding another remote doesn't affect which remote branch (if any) a local branch tracks. However, `git push -u myrepo master` makes your local master start tracking `myrepo/master`. If, after that, you run `git pull` while on master, you will pull from `myrepo`, and no longer from `origin`. – jub0bs Nov 30 '18 at 18:55
32

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.

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.

After that, just

git push

and you'll be able to push your changes to your fork :)

Ahmad Awais
  • 33,440
  • 5
  • 74
  • 56
11

Okay I finally edited my git config file :

$ 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 :

git remote set-url origin https://yourusername@github.com/user/repo.git
Community
  • 1
  • 1
Rémi Becheras
  • 14,902
  • 14
  • 51
  • 81
3

If you are using SSH for authentication (instead of over HTTPS) then you can swap the origin URL in your local clone with the URL of your forked repo as follows:

git remote set-url origin git@github.com:username/repository

Then simply:

git push
Dusk
  • 71
  • 3
0

Thought I'd add for those using a jetbrains IDE (similar probably applies to others) Git menu (via menu bar) -> Manage Remotes -> Edit the URL under the repo you want to change the origin location for to e.g. https://github.com/your_git_handle/your_repo_name.git

-6

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.

LeonF
  • 423
  • 6
  • 14