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

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.


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


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://[email protected]/user/repo.git

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 [email protected]:username/repository

Then simply:

git push