Make an existing Git branch track a remote branch?

前端 未结 22 2990
执念已碎
执念已碎 2020-11-21 07:16

I know how to make a new branch that tracks remote branches, but how do I make an existing branch track a remote branch?

I know I can just edit the

相关标签:
22条回答
  • 2020-11-21 07:50

    In a somewhat related way I was trying to add a remote tracking branch to an existing branch, but did not have access to that remote repository on the system where I wanted to add that remote tracking branch on (because I frequently export a copy of this repo via sneakernet to another system that has the access to push to that remote). I found that there was no way to force adding a remote branch on the local that hadn't been fetched yet (so local did not know that the branch existed on the remote and I would get the error: the requested upstream branch 'origin/remotebranchname' does not exist).

    In the end I managed to add the new, previously unknown remote branch (without fetching) by adding a new head file at .git/refs/remotes/origin/remotebranchname and then copying the ref (eyeballing was quickest, lame as it was ;-) from the system with access to the origin repo to the workstation (with the local repo where I was adding the remote branch on).

    Once that was done, I could then use git branch --set-upstream-to=origin/remotebranchname

    0 讨论(0)
  • 2020-11-21 07:50

    To avoid remembering what you need to do each time you get the message:

    Please specify which branch you want to merge with. See git-pull(1)
    for details.
    .....

    You can use the following script which sets origin as upstream for the current branch you are in.

    In my case I almost never set something else than origin as the default upstream. Also I almost always keep the same branch name for local and remote branch. So the following fits me:

    #!/bin/bash
    # scriptname: git-branch-set-originupstream
    current_branch="$(git branch | grep -oP '(?<=^\* )(.*)$')"
    upstream="origin/$current_branch"
    git branch -u "$upstream"
    
    0 讨论(0)
  • 2020-11-21 07:51

    Use '--track' Option

    • After a git pull :

      git checkout --track <remote-branch-name>

    • Or:

      git fetch && git checkout <branch-name>

    0 讨论(0)
  • 2020-11-21 07:52

    For Git versions 1.8.0 and higher:

    Actually for the accepted answer to work:

    git remote add upstream <remote-url>
    git fetch upstream
    git branch -f --track qa upstream/qa
    # OR Git version 1.8.0 and higher:
    git branch --set-upstream-to=upstream/qa
    # Gitversions lower than 1.8.0
    git branch --set-upstream qa upstream/qa
    
    0 讨论(0)
  • 2020-11-21 07:56

    I do this as a side-effect of pushing with the -u option as in

    $ git push -u origin branch-name
    

    The equivalent long option is --set-upstream.

    The git-branch command also understands --set-upstream, but its use can be confusing. Version 1.8.0 modifies the interface.

    git branch --set-upstream is deprecated and may be removed in a relatively distant future. git branch [-u|--set-upstream-to] has been introduced with a saner order of arguments.

    It was tempting to say git branch --set-upstream origin/master, but that tells Git to arrange the local branch "origin/master" to integrate with the currently checked out branch, which is highly unlikely what the user meant. The option is deprecated; use the new --set-upstream-to (with a short-and-sweet -u) option instead.

    Say you have a local foo branch and want it to treat the branch by the same name as its upstream. Make this happen with

    $ git branch foo
    $ git branch --set-upstream-to=origin/foo
    

    or just

    $ git branch --set-upstream-to=origin/foo foo
    
    0 讨论(0)
  • 2020-11-21 07:56

    This isn't a direct answer to this question, but I wanted to leave a note here for anyone who may be having the same issue as me when trying to configure an upstream branch.

    Be wary of push.default.

    With older git versions, the default was matching, which would cause very undesirable behaviour if you have, for example:

    Local branch "master" tracking to origin/master

    Remote branch "upstream" tracking to upstream/master

    If you tried to "git push" when on the "upstream" branch, with push.default matching git would automatically try to merge the local branch "master" into "upstream/master", causing a whole lot of chaos.

    This gives more sane behaviour:

    git config --global push.default upstream

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