Explore Courses Blog Tutorials Interview Questions
+11 votes
1 view
in DevOps and Agile by (19.7k points)

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/branch-name. Problem is its not accessible. I can't merge or checkout?

1 Answer

+9 votes
by (62.9k points)
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 wish to merge one of those remote branches on your native 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 repository so you can set up 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 native 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 1st "myBranch" as representing that remote branch: that's the git checkout -b myBranch origin/aBranch part.
And then you can merge anotherLocalBranch to it (to myBranch).

Browse Categories