Peter Rundqvist
Peter Rundqvist

Reputation: 315

How to Fetch from one Remote and Push into another

I'm developing a Virtual Assistant solution to be used within my organization. I have cloned the Microsoft AI repository from GitHub.

The AI repo is updated regularly with new features which I want to take advantage of.

This is what I want to accomplish:

  1. Have a local repo (AI) to which I fetch remote commits from origin/master (AI)
  2. I want to be able to customize the code to suit our needs (e.g. add custom skills and/or localizing content)
  3. I want to be able to push the code and Changes to another remote repo ("MyOwnRemoteRepo")
  4. Then, when new commits are fetched from the AI repo, I want to be able to merge these with my customized code and commit this to MyOwnRemoteRepo repo.

So:

  1. Clone the AI repo (AI) to local master
  2. Fetch changes from AI (origin/master-> local master)
  3. Make changes to local master
  4. Push changes in local master to MyOwnRemoteRepo (origin/master)
  5. Back to step 2

I have tried to look for a Git strategy for this situation, but I haven't found any good suggestion of how to do.

Any suggestions?

Upvotes: 2

Views: 2332

Answers (2)

gregory
gregory

Reputation: 12905

Perhaps the confusing aspect of forking is the term "origin." When a repo is cloned, it has a default remote called origin that points to your fork on GitHub, not the original repo it was forked from. To keep track of the original repo, you need to add another remote, typically named "upstream:"

git remote add upstream git://github.com/user/repo.git

Your process then becomes clear: you fetch from upstream, and push/pull into origin. When all set up, 'git remote -v` will show something like this:

`origin https://github.com/your_repo/AI.git (fetch)
 origin https://github.com/your_repo/AI.git (push)
 upstream   https://github.com/Microsoft/AI.git (fetch)
 upstream   https://github.com/Microsoft/AI.git (push)`

See other more in-depth answers on this and related topics: What is the difference between origin and upstream on GitHub? and Definition of "downstream" and "upstream"

Upvotes: 3

jacobherrington
jacobherrington

Reputation: 463

The easiest way to do this is a fork, sometimes that isn't what you want. What I do in this situation:

Create my repo (in GitHub, GitLab, etc.) with a README

Clone my repo locally

Add a remote to the repo I want code from (I usually call this upstream)

git remote add upstream [email protected]/whatever

Pull from the remote

git pull upstream master

Push to my repo

git push origin master

Upvotes: 2

Related Questions