简体   繁体   中英

How to push into a repository different from the repository I'm pulling from

I have cloned a public github repo and started modifying some of the files locally.

Now I'd like to commit and push these changes into a private repository, but still be able to pull and merge changes from the public repo.

How can I setup git for doing that ?

I know I could fork the public project and work off the forked version, but I am not going to merge my changes into the public repo, so I don't think it's the correct solution.

You can define multiple remote repositories.

$ git remote add repo1 <repo1_url>
$ git remote add repo2 <repo2_url>

Now you have 2 remote repos, lets say you are on master branch of repo1 you can git pull directly if your branch is tracking the remote master branch then you can push to the other repo using

$ git push repo2 <branch_name>

now you have the branch pushed to the other repo.

I know I could fork the public project and work off the forked version, but I am not going to merge my changes into the public repo, so I don't think it's the correct solution.

It is a valid solution, even if you don't intent to make any pull-request.

It would not be a valid solution in case you don't want your changes to be visible (in a public repo, like the fork repo would be)

In that case, create an empty private repo (on Bitbucket for instance) and:

cd /path/to/local/repo
git remote rename origin upstream
git remote add origin /url/to/private/repo
git push --mirror

It is important to keep a link to the original public repo (here referenced as 'upstream') in order to be able to fetch or rebase on upstream/master (and getting the latest updates from that repo).

I would change the remote origin to the private repo:

git remote delete origin
git remote add origin git@server:user/repo.git

Now you can push to the private repo using

git push origin BRANCH_NAME

To pull (and merge) from the public repo I would create another remote, lets call it upstream :

git remote add upstream git@server:public/repo.git

You are now able to fetch from the upstream:

git fetch upstream master

and probably merge it into your local branch:

git merge upstream/BRANCH_NAME

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM