Git: Merge a Remote branch locally

后端 未结 5 874
小蘑菇
小蘑菇 2020-12-02 03:58

I\'ve pulled all remote branches via git fetch --all. I can see the branch I\'d like to merge via git branch -a as remotes/origin/branchname. Probl

相关标签:
5条回答
  • 2020-12-02 04:10

    Maybe you want to track the remote branch with a local branch:

    1. Create a new local branch: git branch new-local-branch
    2. Set this newly created branch to track the remote branch: git branch --set-upstream-to=origin/remote-branch new-local-branch
    3. Enter into this branch: git checkout new-local-branch
    4. Pull all the contents of the remote branch into the local branch: git pull
    0 讨论(0)
  • 2020-12-02 04:14

    You can reference those remote tracking branches ~(listed with git branch -r) with the name of their remote.

    You need to fetch the remote branch:

    git fetch origin aRemoteBranch
    

    If you want to merge one of those remote branches on your local branch:

    git checkout master
    git merge origin/aRemoteBranch
    

    Note 1: For a large repo with a long history, you will want to add the --depth=1 option when you use git fetch.

    Note 2: These commands also work with other remote repos so you can setup an origin and an upstream if you are working on a fork.


    Opposite scenario: If you want to merge one of your local branch on a remote branch (as opposed to a remote branch to a local one, as shown above), you need to create a new local branch on top of said remote branch first:

    git checkout -b myBranch origin/aBranch
    git merge anotherLocalBranch
    

    The idea here, is to merge "one of your local branch" (here anotherLocalBranch) to a remote branch (origin/aBranch).
    For that, you create first "myBranch" as representing that remote branch: that is the git checkout -b myBranch origin/aBranch part.
    And then you can merge anotherLocalBranch to it (to myBranch).

    0 讨论(0)
  • 2020-12-02 04:26

    Whenever I do a merge, I get into the branch I want to merge into (e.g. "git checkout branch-i-am-working-in") and then do the following:

    git merge origin/branch-i-want-to-merge-from

    0 讨论(0)
  • 2020-12-02 04:32

    Fetch the remote branch from the origin first.

    git fetch origin remote_branch_name
    

    Merge the remote branch to the local branch

    git merge origin/remote_branch_name
    
    0 讨论(0)
  • 2020-12-02 04:32

    If you already fetched your remote branch and do git branch -a,
    you obtain something like :

    * 8.0
      xxx
      remotes/origin/xxx
      remotes/origin/8.0
      remotes/origin/HEAD -> origin/8.0
      remotes/rep_mirror/8.0
    

    After that, you can use rep_mirror/8.0 to designate locally your remote branch.

    The trick is that remotes/rep_mirror/8.0 doesn't work but rep_mirror/8.0 does.

    So, a command like git merge -m "my msg" rep_mirror/8.0 do the merge.

    (note : this is a comment to @VonC answer. I put it as another answer because code blocks don't fit into the comment format)

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