'How do I track branches from another repo?

How do I track branches from another repo? I duplicated somebody else's repo and plan to use it as a base for my own changes, but I would like to receive pull request updates when the original repo changes.

Right now I have master and develop in my local repo (these are the ones I would like to track). Do I delete them, or can I leave them and everything will work out?

I also have a branch that I will use for my changes, derived from their latest source.

I hope this is enough info to help. My .git/config is below.

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/me/my-repo/
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master
[branch "develop"]
    remote = origin
    merge = refs/heads/develop
[remote "other-repo"]
    url = https://github.com/somebody-else/other-repo.git
    fetch = +refs/heads/master:refs/remotes/other-repo/master
    fetch = +refs/heads/develop:refs/remotes/other-repo/develop


Solution 1:[1]

You already have two remotes:

[remote "origin"]
    url = https://github.com/me/my-repo/
    fetch = +refs/heads/*:refs/remotes/origin/* [remote "other-repo"]
url = https://github.com/somebody-else/other-repo.git
    fetch = +refs/heads/master:refs/remotes/other-repo/master
    fetch = +refs/heads/develop:refs/remotes/other-repo/develop

Your second remote, other-repo, is almost a single-branch clone, but in fact is a two-branch clone (master and develop => other-repo/master and other-repo/develop). If you replace the last two fetch lines with:

    fetch = +refs/heads/*:refs/remotes/other-repo/*

you'll have two full remotes. This will give you other-repo/* remote-tracking names, just as you now have origin/* remote-tracking names.

Now, there's no reason your branch names have to imitate either of the other remotes' remote-tracking names in your repository. (There is a reason those remote-tracking names have to imitate the branch names in the two remote Git repositories: namely, the fetch = lines that have * in them.1)

That is, suppose Alice has branches named master and develop and so on, that show up in your repository as alice/*, and Bob has branches master and develop and so on that all show up as bob/*. You can call your (local) branch malice when tracking alice/master, and jr-bob-dobbs when tracking bob/develop, if you like.

The same thing goes here: the upstream setting of your branch framboise can be other-repo/master, and the upstream of your branch grand-marnier can be origin/master. There's no reason you have to use the same branch names they use. And in fact, if they're using master for two different purposes, you can't use a single master in your repository for purposes related to both different purposes. So, just don't.


1This provides a second alternative method. Currently there is only one * line, and you could maintain that by replacing all the fetch = lines under the second remote to use different remote-tracking names for each branch name in the remote. That is, instead of:

fetch = +refs/heads/master:refs/remotes/other-repo/master
fetch = +refs/heads/develop:refs/remotes/other-repo/develop

you could have:

fetch = +refs/heads/master:refs/remotes/other-repo/iron-maiden
fetch = +refs/heads/develop:refs/remotes/other-repo/jethro-tull

for instance. But this way lies madness.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 torek