How can I push a local Git branch to a remote with a different name easily?

前端 未结 5 1453
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-30 16:38

I\'ve been wondering if there\'s an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.

For exa

相关标签:
5条回答
  • 2020-11-30 17:10

    I have been running into the same issue for quite sometime now. I finally have a set of statements so I don't have to do git push origin local:remote every time. I followed these:

    git branch --set-upstream-to origin/remote_branch_name
    git config push.default upstream
    git push
    

    After setting upstream to a remote branch with different name (1st line) and then making that upstream as default (2nd line), 3rd line will now obey these rules and push to the set upstream.

    0 讨论(0)
  • 2020-11-30 17:17

    Here's the process that has worked for me.

    git clone original-repo-url
    git remote rename origin upstream
    git remote add origin new-repo-url
    

    Now your new repo will be ‘origin’ and the original repo is ‘upstream’. Confirm it by running git remote -v. (Side note: Upstream is used to fetch from the original repo - in order to keep your local copy in sync with the project you want to contribute to - and origin is used to pull and push since you can contribute to your own repo).

    git push origin master

    Now your new remote repo's master (on Github) will be in-sync with the original master, but it won't have any of the feature branches.

    git rebase upstream/branch-name
    git push origin master
    

    Rebase is a smart merge. Then push to master again and you’ll see the selected feature branch as master on the new repo.

    Optional:

    git remote rm upstream
    git remote add upstream new-repo-url
    
    0 讨论(0)
  • 2020-11-30 17:20

    Sure. Just set your push.default to upstream to push branches to their upstreams (which is the same that pull will pull from, defined by branch.newb.merge), rather than pushing branches to ones matching in name (which is the default setting for push.default, matching).

    git config push.default upstream
    

    Note that this used to be called tracking not upstream before Git 1.7.4.2, so if you're using an older version of Git, use tracking instead. The push.default option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.

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

    The command by Adam is now deprecated. You can use:

    git branch --set-upstream-to origin/my_remote_branch my_local_branch
    

    to set the upstream branch of my_local_branch to origin/my_remote_branch.

    0 讨论(0)
  • 2020-11-30 17:31

    When you do the initial push add the -u parameter:

    git push -u origin my_branch:remote_branch
    

    Subsequent pushes will go where you want.

    EDIT:

    As per the comment, that only sets up pull.

    git branch --set-upstream
    

    should do it.

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