316

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. Problem is it is not accessible. I can't merge or checkout.

Ajay
  • 18,086
  • 12
  • 59
  • 105
micahblu
  • 4,924
  • 5
  • 27
  • 33

5 Answers5

491

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 aLocalBranch
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.

Note 3: user3265569 suggests the following alias in the comments:

From aLocalBranch, run git combine remoteBranch
Alias:

combine = !git fetch origin ${1} && git merge origin/${1}

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).

Hari
  • 1,561
  • 4
  • 17
  • 26
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • aLocalBranch? is that a typo? I guess you meant to write "myBranch" again? – knocte Jan 19 '16 at 04:40
  • 1
    @knocte No: "If you want to merge one of your local branch on one of those remote branch": I am merging "`aLocalBranch`" to "`myBranch`", with "`myBranch`" representing a remote branch `origin/aBranch`. – VonC Jan 19 '16 at 05:37
  • Sorry to bother you @VonC, in your previous comment you say `aLocalBranch` is not a typo, but you approved the latest edit (2 years later!) which corrects this "probable typo". I wanted to check with you before undoing the edit. – rath May 01 '19 at 13:36
  • 1
    @rath You are correct: it appears I might have reviewed that edit a bit hastily, bordering on carelessness. I did edit the answer to clarify the second merge case: can you tell me if this is clearer now? – VonC May 01 '19 at 18:54
  • I've created an alias for this. From `aLocalBranch ` run `git combine remoteBranch`. Alias: `combine = !git fetch origin ${1} && git merge origin/${1}` – user3265569 Feb 09 '21 at 15:42
  • @user3265569 Thank you for this contribution. I have included your comment in the answer for more visibility. – VonC Feb 09 '21 at 15:46
  • As previous comments highlight, this answer overcomplicates the issue with hard to understand branch names and a follow-on answer that isn't in the question. I'm just looking for `git merge remoteMain into featureLocal` or whichever way around it is. – Rin and Len Aug 20 '21 at 14:01
  • @RinandLen OK. Did you find a clear answer below? – VonC Aug 20 '21 at 14:04
113

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

Bono
  • 4,757
  • 6
  • 48
  • 77
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
57

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
Lanil Marasinghe
  • 2,785
  • 24
  • 24
  • 1
    This worked for me when the accepted answer reported Not something we can merge. In my case I was merging from another user’s fork of my repo on GitHub. – SJT Jul 05 '19 at 12:59
  • 3
    Does the first command fetch required? Does ‘git merge origin/remote_branch_name’ reads from remote branch, not local. So I shouldn’t care, is local branch updated or not? – Michael Freidgeim Apr 30 '20 at 06:45
  • 1
    @MichaelFreidgeim You do need to fetch first so your local repository knows the state of remote. Git pull is basically a fetch and merge from the current remote version of your current branch, but now you fetch and merge from a different one. https://stackoverflow.com/questions/292357/what-is-the-difference-between-git-pull-and-git-fetch – DZet Jul 06 '20 at 17:18
24

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

  1. Create a new local branch:
  git branch -b new-local-branch
  1. Set this newly created branch to track the remote branch:
  git branch --set-upstream-to=origin/remote-branch new-local-branch
  1. Enter into this branch:
  git checkout new-local-branch
  1. Pull all the contents of the remote branch into the local branch:
  git pull
e18r
  • 7,578
  • 4
  • 45
  • 40
3

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)

herve-guerin
  • 2,827
  • 20
  • 25