pull/push from multiple remote locations

后端 未结 15 1268
傲寒
傲寒 2020-11-21 10:17

The short: is there a way to have a git repo push to and pull from a list of remote repos (rather than a single \"origin\")?

The long:

相关标签:
15条回答
  • 2020-11-21 10:24

    add an alias to global gitconfig(/home/user/.gitconfig) with below command.

    git config --global alias.pushall '!f(){ for var in $(git remote show); do echo "pushing to $var"; git push $var; done; }; f'
    

    Once you commit code, we say

    git push

    to push to origin by default. After above alias, we can say

    git pushall

    and code will be updated to all remotes including origin remote.

    0 讨论(0)
  • 2020-11-21 10:25

    I added two separate pushurl to the remote "origin" in the .git congfig file. When I run git push origin "branchName" Then it will run through and push to each url. Not sure if there is an easier way to accomplish this but this works for myself to push to Github source code and to push to My.visualStudio source code at the same time.

    [remote "origin"]
      url = "Main Repo URL"
      fetch = +refs/heads/*:refs/remotes/origin/*
      pushurl = "repo1 URL"
      pushurl = "reop2 URl"
    
    0 讨论(0)
  • 2020-11-21 10:26

    I wanted to work in VSO/TFS, then push publicly to GitHub when ready. Initial repo created in private VSO. When it came time to add to GitHub I did:

    git remote add mygithubrepo https://github.com/jhealy/kinect2.git
    git push -f mygithubrepo master
    

    Worked like a champ...

    For a sanity check, issue "git remote -v" to list the repositories associated with a project.

    C:\dev\kinect\vso-repo-k2work\FaceNSkinWPF>git remote -v
    githubrepo      https://github.com/jhealy/kinect2.git (fetch)
    githubrepo      https://github.com/jhealy/kinect2.git (push)
    origin  https://devfish.visualstudio.com/DefaultCollection/_git/Kinect2Work (fetch)
    origin  https://devfish.visualstudio.com/DefaultCollection/_git/Kinect2Work (push)
    

    Simple way, worked for me... Hope this helps someone.

    0 讨论(0)
  • 2020-11-21 10:30

    You'll need a script to loop through them. Git doesn't a provide a "push all." You could theoretically do a push in multiple threads, but a native method is not available.

    Fetch is even more complicated, and I'd recommend doing that linearly.

    I think your best answer is to have once machine that everybody does a push / pull to, if that's at all possible.

    0 讨论(0)
  • 2020-11-21 10:30

    Adding the all remote gets a bit tedious as you have to setup on each machine that you use.

    Also, the bash and git aliases provided all assume that you have will push to all remotes. (Ex: I have a fork of sshag that I maintain on GitHub and GitLab. I have the upstream remote added, but I don't have permission to push to it.)

    Here is a git alias that only pushes to remotes with a push URL that includes @.

    psall    = "!f() { \
        for R in $(git remote -v | awk '/@.*push/ { print $1 }'); do \
        git push $R $1; \
        done \
        }; f"
    
    0 讨论(0)
  • 2020-11-21 10:31

    Doing this manually is no longer necessary, with modern versions of git! See Malvineous's solution, below.

    Reproduced here:

    git remote set-url origin --push --add <a remote>
    git remote set-url origin --push --add <another remote>
    

    Original answer:

    This something I’ve been using for quite a while without bad consequences and suggested by Linus Torvalds on the git mailing list.

    araqnid’s solution is the proper one for bringing code into your repository… but when you, like me, have multiple equivalent authoritative upstreams (I keep some of my more critical projects cloned to both a private upstream, GitHub, and Codaset), it can be a pain to push changes to each one, every day.

    Long story short, git remote add all of your remotes individually… and then git config -e and add a merged‐remote. Assuming you have this repository config:

    [remote "GitHub"]
        url = git@github.com:elliottcable/Paws.o.git
        fetch = +refs/heads/*:refs/remotes/GitHub/*
    [branch "Master"]
        remote = GitHub
        merge = refs/heads/Master
    [remote "Codaset"]
        url = git@codaset.com:elliottcable/paws-o.git
        fetch = +refs/heads/*:refs/remotes/Codaset/*
    [remote "Paws"]
        url = git@github.com:Paws/Paws.o.git
        fetch = +refs/heads/*:refs/remotes/Paws/*
    

    … to create a merged‐remote for "Paws" and "Codaset", I can add the following after all of those:

    [remote "Origin"]
        url = git@github.com:Paws/Paws.o.git
        url = git@codaset.com:elliottcable/paws-o.git
    

    Once I’ve done this, when I git push Origin Master, it will push to both Paws/Master and Codaset/Master sequentially, making life a little easier.

    0 讨论(0)
提交回复
热议问题