0

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.

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 1
    See if this solutions works for you. https://stackoverflow.com/a/64769880/6374123 – vsbehere Jun 28 '22 at 08:36
  • 1
    Change the name of public remote, give some other name. As origin already exist which is pointing to your private repo. e.g. 'git remote add public-origin ' then 'git fetch public-origin dev' – vsbehere Jun 28 '22 at 08:47
  • If you want to fetch from github instead you need to use the github url instead of the private_git url – mousetail Jun 28 '22 at 08:47
  • You might need to check what's in the config file from the .git folder. Whenever you clone, the .git folder will be created with the details of the repo from which you cloned. In the config file, you will see the url and the fetch parameters, which contain useful information of the repo you are actually interacting with. You may need to change these to the public repo in order to interact with it. – laurentiurad Jun 28 '22 at 09:06
  • Have you seen the fatal error message? `fatal: remote origin already exists.` Because of it, you are not overwriting or defining anything – LinFelix Jun 28 '22 at 09:19

1 Answers1

1

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:

  1. clone a private repository, but then
  2. 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.

torek
  • 448,244
  • 59
  • 642
  • 775