Git pull from another repository

83,988

Solution 1

Issue the following command in your Acme repo. It adds a new remote repository named upstream that points to the Generic repo.

git remote add upstream https://location/of/generic.git

You can then merge any changes made to Generic into the current branch in Acme with the following command:

git pull upstream

If you just want it to download the changes without automatically merging, use git fetch instead of git pull.

If you want to disable pushing to that repository, set the push URL to an invalid URL using something like

git config remote.upstream.pushurl "NEVER GONNA GIVE YOU UP"

Git will now yell at you about not being able to find a repo if you try to push to upstream (and sorry about the Rickroll, but it was the first random string that popped into my head).

Solution 2

In order to pull a particular branch from different repo you can use the below git command.

git pull <git_url> <branch> --allow-unrelated-histories

Solution 3

As @vin mentioned you can pull directly from another repo without adding a new origin (assuming a pull from another repo's master branch to the local master branch):

git pull https://some-other-repo.git master:master 

If the commit histories of the two repos are not linked then --allow-unrelated-histories will be needed but use it with caution, as it implies you know what you are doing.

You can also push a local repo to another repo without adding it as a remote (the remote branch doesn't have to exist so you can use anything you want, e.g. master:new_branch):

git push https://some-other-repo.git master:master
Share:
83,988

Related videos on Youtube

Libbux
Author by

Libbux

Updated on August 05, 2021

Comments

  • Libbux
    Libbux almost 3 years

    I have a repository called Generic, which is a generic application. I have forked it into a repository called Acme, which just builds upon the application stored Generic repository and adds Acme Co branding to it.

    If I make changes to the core functionality in Generic, I want to update the Acme repository with the latest changes I have made to the core functionality in Generic. How would I do that?

    As far as I can tell, I am essentially trying to merge the changes made in an upstream repository into the current fork.

    If it means anything, I'm trying to do this because I have a generic application that I then build upon and brand for individual clients (like Acme in this example). If there is a cleaner way of doing this, let me know.

  • Libbux
    Libbux almost 10 years
    Excellent, thanks. Just one other thing: is there a way I can make this remote read-only, so I don't end up accidentally pushing to it?
  • Frankie Simon
    Frankie Simon about 9 years
    I started to answer but I see McLovin already edited his answer to include that.
  • sfinks_29
    sfinks_29 over 6 years
    Since git 2.9, you will need to specify the --allow-unrelated-histories flag. See stackoverflow.com/a/37938036/3799847
  • Alec Jacobson
    Alec Jacobson over 6 years
    I needed to issue git pull --allow-unrelated-histories upstream master
  • Paulo Carvalho
    Paulo Carvalho over 5 years
    I tried that but git pull upstream just hangs... any ideas?
  • Alejandro
    Alejandro about 5 years
    What if I want to push only to Acme and never to Generic?
  • ILoveGit
    ILoveGit about 5 years
    For anybody looking to do this for dependencies, and not just build off of a project, see submodules: (blog.joncairns.com/2011/10/how-to-use-git-submodules)
  • Antimatterr
    Antimatterr about 2 years
    I would like to add one thing if there are several branches in upstream you have to mention the branch name from where you want to pull from. "git pull upstream <branchName>"
  • timhc22
    timhc22 about 2 years
    Shame it isn't possible to pull a specific commit (so like doing a cherry pick from a different repo, which could be useful if you have used another repo as a base and you want to continue updating from the base repo). Any ideas on that?