So I haven't found any answers that explains this in a way that I understand. So how do I add a remote branch to my local repo using git?
-
2Did you clone your repository from another, or otherwise add a remote repository to the local one? – chepner Feb 18 '21 at 23:21
-
What do you mean? I have a repo in github and I have added some branches to github with my other computer, and now I want to get the branches to my laptop. @chepner – Feb 18 '21 at 23:23
-
I would suggest (based on the long comment thread on an otherwise simple answer) spending some time reading https://git-scm.com/book/en/v2 to understand how Git, as a distributed source-control system, works. – chepner Feb 19 '21 at 13:37
2 Answers
A simple git fetch
should be enough.
Type
cd /path/to/local/repo/on/first/computer
git fetch
git branch -avv
You should see the new branches listed as origin/xxx
(namespace 'origin
')
A git switch xxx
will create a local branch based on that remote tracking branch origin/xxx
.
That will also update your Git repository working tree (ie, your files), which will make them visible in your IDE/editor.

- 1,262,500
- 529
- 4,410
- 5,250
-
-
1@Erik After a `git fetch`, your PC (meaning the repo which was already cloned on that PC) will have the branches. All the branches. – VonC Feb 18 '21 at 23:50
-
Sorry for this stupid question, but what do you mean with cd /path/to/local/repo/on/first/computer? – Feb 18 '21 at 23:52
-
@Erik I mean, on your first PC, go to the folder where you had cloned your repository. – VonC Feb 18 '21 at 23:54
-
-
@Erik it is a simple cd, in a shell session, in order to change your current working folder (https://www.cyberciti.biz/faq/how-to-change-directory-in-linux-terminal/). Once you are in the right folder, then you can type `git fetch` and `git branch -avv` – VonC Feb 18 '21 at 23:56
-
-
@Erik As I said, the first PC/laptop, the one which does not contain the branch, the one for which you have asked this very question. You said "I have added some branches to github with my other computer, and now I want to get the branches to my laptop": so to see those new branch on your laptop, go to the folder in your laptop where you had already clone the repository, and do the `git fetch` + `git branch -avv`, tpo confirm you do see the enw branches added from your "other computer". – VonC Feb 19 '21 at 00:00
-
Yes, I see those branches like this remotes/origin/branch1, but how do I get them to my computer? Sorry if my questions are stupid, but I'm new to git. – Feb 19 '21 at 00:05
-
-
-
@Erik That is what the `git switch` command I mention in my answer is for. – VonC Feb 19 '21 at 00:17
-
-
-
@Erik The name of the remote branch you want to see. For example: `git switch branch1` would make visible files from the `remotes/origin/branch1` remote tracking branch that you have fetched. – VonC Feb 19 '21 at 00:25
-
It says Switched to a new branch 'branch1' M style.css Branch 'branch1' set up to track remote branch 'branch1' from 'origin'. And nothing else happened. – Feb 19 '21 at 00:29
-
@Erik That should be enough for your working tree to reflect the files in branch1. Let me know if your editor does show those files. You might have to relaunch it (depending on your actual editor and OS) – VonC Feb 19 '21 at 00:34
I think others did not understand your question.
You are trying to bring down to your local machine a remote branch that you currently do not have. This could happen if, for instance, your colleague created a new branch.
Now that git switch
has been added, this is simple:
git switch <name of remote branch>
Since you do not have the branch locally, this will automatically make switch
look on the remote repo. It will then also automatically set up remote branch tracking.
Lastly, this has already been discussed a bunch here on SO. You can look here for more answers, but some are outdated.

- 4,915
- 14
- 67
- 104