razr32
razr32

Reputation: 369

Set Remote for Only One Branch?

I need to work with a remote repository and I have it set to a local branch but I need to type the URL every time I want to update the branch from the remote repo. Is there a way to change the origin only for that one branch? So to update the branch I don't have to use the URL.

What I currently do:

git fetch https://github.com/username/repo branch:my-local-branch

But I'd rather just do something like:

git fetch my-local-branch

Where the above would fetch from https://github.com/username/repo.

Upvotes: 3

Views: 3929

Answers (4)

kbro
kbro

Reputation: 4991

You can track a single branch from a remote server using e.g.

git remote add theirname git://git.theirhost.com/theirrepo.git --track theirbranch

Then you can git fetch theirname and you'll only fetch commits for theirbranch along with tags that refer to those commits.

If you look inside your .git/config file you'll see something like

[remote "theirname"]
url = git://git.theirhost.com/theirrepo.git
fetch = +refs/heads/theirbranch:refs/remotes/theirname/theirbranch

The "secret sauce" is the fetch reference. Without --track the refspec would be +refs/heads/*:refs/remotes/theirname/*, with the * causing all remote branch names to be pulled across.

Upvotes: 5

kirelagin
kirelagin

Reputation: 13616

When you do git fetch <brname>, git consults the branch.<brname>.fetch config variable to see which remote to fetch from. Therefore you need to:

  1. Add a new remote if you do not have it already:
    git remote add <remname> https://github.com/username/repo.git
  2. Set the config variable either by
    • editing .git/config manually,
    • using git config or
    • making your branch track the remote branch:
      git branch -u <remname>/<brname> <brname>
      (Note that tracking a remote branch is slightly more than pulling from it by default, which might or might not be what you want.)

Upvotes: 2

Shivkumar kondi
Shivkumar kondi

Reputation: 6762

Use two branches as : master(by-default) and local (use your name as required)

git branch local

git checkout local

Now you are on local branch.Do your changes here and lastly merge it master as:

git merge local

or else directly move it your required remote

you can create n-number of remotes

so you can add your remote and push/pull local changes on it as :

git add remote add local_up <your url>

git push -u local_up master

Upvotes: 0

Vampire
Vampire

Reputation: 38639

origin is just an arbitrary name that happens to be the conventional default. But you can add as many remote repositories as you like. Just use git remote add username https://github.com/username/repo, then you can configure the upstream of your local branch with git branch --set-upstream-to=username/branch my-local-branch.

From then on you can use git pull or git pull --rebase from your my-local-branch or git fetch username/branch to fetch the remote branch from that repository.

Upvotes: 5

Related Questions