I am using a repo from a private website and want to pull changes from a public repo. The commands look like
git clone -b b1 https://private_git.com
# fetch branch dev from https://github.com/repo_name
Specifically I want to fetch branch "dev". What is the right command for that?
UPDATE:
I tried the following commands:
$ git clone -b b1 https://private_git.com/my_repo
$ cd my_repo
$ git remote add origin https://github.com/repo
fatal: remote origin already exists.
$ git fetch origin dev
Username for 'https://private_git.com':
I think that fatal error is fine because at the first time, I clone the public repo and then upload it to private domain. As you see, when I want to fetch the "dev" branch, it asks for user/pass from private domain. I expected to fetch from github.
CodePudding user response:
I think that fatal error is fine ...
It's not.
A remote, in Git terminology, is a short name that:
- stores a URL, and
- makes things easier for you when you want to talk to the Git software that responds at that URL.
When you run git clone
, this creates a new, empty Git repository and then runs git remote add
for you to add one remote to your Git repository. By default—and generally you should not change this—this first, and for most people only, remote is named origin
. So origin
tells you where you cloned your repository.
You want to:
- clone a private repository, but then
- obtain additional commits from a different public repository
and hence you should use two remotes, one for the private repository and one for the public repository. The names you choose for these two remotes are up to you: you can call the private one fred
and the public one barney
or wilma
if you like, or you can keep calling one origin
and call the other public
or private
if you like.
Whether to call the public repository origin
, or call the private repository origin
, or to use origin
at all, is up to you. But most people will mostly expect the name origin
to refer back to wherever you got the repository from and/or wherever you expect to send new commits. Take those two factors into consideration.
Then, if you decide to call them private
and public
respectively for instance, you might do this:
git clone -o private -b b1 https://private_git.com/my_repo
cd my_repo
# now fetch branch dev from https://github.com/repo_name
git remote add public https://github.com/repo_name
git fetch public dev
You will now have two sets of remote-tracking names: private/*
refer to the private repository commits and branch names, and public/dev
refers to branch dev
from repository public
. (This assumes your Git version is 1.8.2 or later.)
Note that your -b b1
in your git clone
tells your Git software to create, locally, branch b1
after the clone process. For this to succeed, this requires that the private repository have a branch named b1
, so you'll have a private/b1
. Since you did not use --single-branch
or --depth
options you will have private/*
names for all other branches. You will not have a remote named origin
, which may confuse some humans.