Here are the steps to take:
Find out which Git version you have. Run git --version
. If it's older than Git 2.0, update it if at all possible. Ideally it should be above 2.17 or so (to avoid various bugs; in general newer is better, although there were, for instance, some new git stash
bugs introduced pretty recently, that are still being squashed, so it's not always better to have the latest Git version).
Find out whether your current repository is a single-branch repository or not by running:
git config --get remote.origin.fetch
(Note: this assumes your remote is named origin
. That's the standard name; it's the one you show in your git pull origin remote_branch_name
command.) This should print:
+refs/heads/*:refs/remotes/origin/*
If it prints instead something like +refs/heads/master:refs/remotes/origin/master
, you have a single-branch clone (with the single branch being, in this case, master
). See How do I "undo" a --single-branch clone? or run:
git remote set-branches origin "*"
to fix it. (If it prints the expected normal stuff, you need not do anything here.)
Run:
git fetch --prune origin
Do not run git pull
. Do not put in branch names. Just run git fetch
or git fetch origin
, preferably with --prune
(though the prune part is optional).
The explicit origin
here means fetch from origin
. Without the name, Git figures out which remote to use, and if you have more than one, can fetch from some other remote instead of from origin
. You almost certainly have only one remote, named origin
, so that there's no question about which remote to use. Use git remote -v
to list the set of remotes that you have; it will print one pair of lines per remote.
Run git branch -r
to list the remote-tracking names that git fetch origin --prune
left behind. These are derived from the branch names that your Git can create automatically, if you do not already have them. For instance, in my Git clone of the Git repository for Git, I get:
$ git branch -r
origin/HEAD -> origin/master
origin/main
origin/maint
origin/master
origin/next
origin/seen
origin/todo
which means I can create a branch named todo
easily.
To create a new local branch from one of these remote-tracking names, run:
git switch <name>
(fill in the name
part, removing the angle brackets <>
, with the remote-tracking name, stripped of the origin/
part). If your Git is older than 2.23, use:
git checkout <name>
to get the same effect. This tells your Git to use the guess mode, which Git previously called DWIM mode: if you don't already have a branch with that name, Git will guess that you meant for it to create a new local branch based on the remote-tracking name.
If for some reason the --guess
mode does not work—for instance, if you have disabled it in your own Git configuration, which you can do in some recent versions of Git—you can always be much more explicit. For instance, if there's an origin/feature/short
and you wish to create feature/short
based on origin/feature/short
, run:
git switch --track origin/feature/short
Your Git understands that origin/feature/short
is a remote-tracking name (because it gets found as one, left behind by git fetch origin
earlier) and that --track
means: Convert this remote-tracking name to a branch name by removing the origin/
part, and then create that branch name, pointing to the correct commit. This is the same trick that the --guess
option—which defaults to "on"—does, except that instead of doing it at the last minute just before saying "there is no such branch", Git will do it early and won't have to guess.
Would also like to know how to create a remote branch ...
In a sense, you can't do this. Imagine you have a brother or friend named Fred. You tell Fred: Change your shirt! The one you have on has a big hole in it! If Fred changes his shirt, did you make him do it? If your answer is "yes, I made him do that", then you can create a remote branch. If it's "no, he chose to do that, I just asked him first", then you can't create a remote branch.
A remote is some other Git repository. Each remote you connect to from your Git repository has a name. The standard name for the first remote is origin
(just like the standard name for your brother is Fred ). You don't really create a remote branch. You just ask or tell some remote, such as origin
, to create a branch. He—we'll assume here that origin
is a guy, or at least goes by masculine pronouns—either does what you ask, or doesn't.
So the more-precise question is: How do I ask a remote repository to create a branch name? The answer is: You run git push
.
When you run:
git push origin my-branch-name:his-branch-name
or:
git push origin one-branch-name
you are instructing your Git software to call up some other Git repository via your name origin
. That other Git repository, which will be updated (or not) by some other Git software, exists independently of your Git repository. He has his own branch names, each one of which stores a commit hash ID. He stores commits. Your Git stores commits and finds them with your branch names. You now have your Git send to his Git any commits that you have, that he does not, that he will need, and then your git push
ends with a polite request to him: Please, if it's OK, create or update your branch name ________ (fill in the blank) to point to commit hash ID ________ (fill in the blank).
Your Git fills in the first blank with the branch name on his side, from the my-branch-name:his-branch-name
pair. If you used the git push origin one-branch-name
syntax, your Git fills in the first blank with one-branch-name
. That is, you and he will use the same branch name.
Your Git fills in the second blank—the commit hash ID—with the hash ID from your branch name, i.e., from the my-branch-name
part of the pair. If you used the one-branch-name
syntax, your Git fills in the second blank with the hash ID from your name one-branch-name
.
Either way, you've now asked him—origin
—to create or update a branch name in his repository. He will either obey, or not. You have no direct control at this point in time as to whether he will obey. (If you "own" the other Git repository—for instance, if it's on GitHub but you're the owner of the GitHub repository—you can, at some earlier time, log in to GitHub on the Web and set up your own permissions to determine whether your later git push
will be obeyed. But that's separate from your git push
operation.)
If this branch name is new to origin
, your request is one to create a branch name. If he obeys, you have created, or at least caused the creation of, the corresponding branch name in the other Git repository. Whether you want to call that "creating a remote branch" is up to you, but in a technical sense, what you really did was send a request. He—the Git and repository at origin
—decided whether to obey the request. So it always takes at least a little bit of coöperation.