Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

github

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?

like image 842
Rémi Becheras Avatar asked Oct 01 '22 23:10

Rémi Becheras


People also ask

How do I sync my fork repo with original repo?

Go to your fork, click on Fetch upstream , and then click on Fetch and merge to directly sync your fork with its parent repo. You may also click on the Compare button to compare the changes before merging.

Can you push to a cloned repo?

After creating the repository, copy the repo URL. Now, add the URL to your repo. Now you can push/publish it to your own repository!

Can a forked repo pull from original?

Sync Your Forked GitHub Repo Using A Reverse Pull RequestCreate a pull request on GitHub.com to update your fork of the repository from the original repository, and. Run the git pull command in the terminal to update your local clone.


1 Answers

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.

like image 259
jub0bs Avatar answered Oct 08 '22 11:10

jub0bs