Github: Import upstream branch into fork

后端 未结 6 450
攒了一身酷
攒了一身酷 2020-12-07 07:24

I have a fork (origin) from a project (upstream) on github. Now the upstream project has added a new branch, I want to import into my fork. How do

相关标签:
6条回答
  • 2020-12-07 07:29

    I would use

    git checkout -b <new_branch> upstream/<new_branch>
    
    0 讨论(0)
  • 2020-12-07 07:32

    I had a slightly more complicated scenario where I already had an upstream defined in my fork (from the canonical repo) but needed to checkout a branch from a different fork. To get that done, the process is slightly different. Here's the config I ended up with:

    [remote "origin"]
    url = git@github.com:<your_user/org>/<repo>.git
    fetch = +refs/heads/*:refs/remotes/origin/*
    [branch "master"]
    remote = origin
    merge = refs/heads/master
    rebase = true
    [remote "upstream"]
    url = git@github.com:<upstream_user>/<repo>.git
    fetch = +refs/heads/*:refs/remotes/upstream/*
    [remote "other_user"]
    url = git@github.com:<other_user>/<repo>.git
    fetch = +refs/heads/*:refs/remotes/<other_user>/*
    

    Now you can checkout a branch from <other_user> fork as well.

    git fetch <other_user> <branch>
    git checkout -b <branch> <other_user>/<branch>
    

    That will give you a local branch which is derived from the fork.

    To push that local branch I had to be specific with my push command.

    git push origin <branch>
    
    0 讨论(0)
  • 2020-12-07 07:37

    I had trouble with this too, and google took me here. The solutions didn't work however. My problem was that when i added my upstream, it set up my git config to only fetch master, rather than all branches. e.g. It looked like this

    [remote "somebody"]
            url = git@github.com:somebodys/repo.git
            fetch = +refs/heads/master:refs/remotes/upstream/master
    

    Editing .git/config as follows fixed my problem

    [remote "somebody"]
            url = git@github.com:somebodys/repo.git
            fetch = +refs/heads/*:refs/remotes/upstream/*
    
    0 讨论(0)
  • 2020-12-07 07:44
    1. Make sure you've pulled the new upstream branch into your local repo:

      • First, ensure your working tree is clean (commit/stash/revert any changes)
      • Then, git fetch upstream to retrieve the new upstream branch
    2. Create and switch to a local version of the new upstream branch (newbranch):

      • git checkout -b newbranch upstream/newbranch
    3. When you're ready to push the new branch to origin:

      • git push -u origin newbranch

    The -u switch sets up tracking to the specified remote (in this example, origin)

    0 讨论(0)
  • 2020-12-07 07:47

    The following steps worked well for me (assuming the upstream branch name is branch):

    $ git fetch upstream
    $ git checkout branch
    $ git push origin
    
    0 讨论(0)
  • 2020-12-07 07:49

    --track?

    git branch --track branch upstream/branch
    
    0 讨论(0)
提交回复
热议问题