0

tried creating a branch from the gitab gui and used the following commands

>>git pull origin remote_branch_name
>>git checkout   remote_branch_name

did not work -> did not get the remote branch name when I used the command git branch. Would also like to know how to create a remote branch and the entire process of creating a local branch from a remote branch using the command line?

  • 2
    Can you elaborate of "did not work"? What error are you getting? – Mureinik Dec 20 '21 at 11:38
  • A *repository* is never created from a *branch*. A repository is a collection of commits and other databases, with an optional working tree. The `git init` program creates a new, empty repository, and `git clone` runs `git init` and then fills in the empty repository. So these are the two ways to create a repository: create an empty one from scratch, or create an empty one and then fill it from another repository. – torek Dec 20 '21 at 11:45
  • This might be a simple wording error: maybe you meant to ask how a branch name in your repository is created from some other branch name in someone else's repository. But the exact words here matter, a lot. – torek Dec 20 '21 at 11:46
  • @Mureinik- I did not get the remote branch name when i used the command git branch – aniketh deokar Dec 20 '21 at 11:50
  • @torek - sorry for that I meant creating a local branch and not a repo I will edit the question posted thank you for the advice – aniketh deokar Dec 20 '21 at 11:52

2 Answers2

0

Branch creation (remote vs local)

OP: How or rather when does a local branch get created from a remote branch in Git?

A local branch gets created whenever:

  • a remote branch is checked out (switched to) in your local repository
  • a new branch (that does not exist on remote) is created locally

Git mental model In above example a remote repository has been cloned, and the entire repo including the only branch available (master) has been mirrored locally through o/master; it has also gotten a local master branch automatically created to track it's remote counter part. This happens automatically for the default branch whenever a new repository is cloned.

Notice also the branch super-cool-feature which is currently checked out locally, but does not yet exist remotely. To publish the branch remotely it must first be pushed.

Switching to an existing remote branch

$ git switch branch_name

Creating a new local branch (and switch to it automatically)

$ git switch --create=branch_name

Publishing a local branch remotely

$ git push --set-upstream origin branch_name

Source: The example above is borrowed from this blog.

0

Here are the steps to take:

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

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

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

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

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

torek
  • 448,244
  • 59
  • 642
  • 775
  • Referring to the 5th point when I create a local branch using the command git switch . Is it mandatory for me to name the local branch with the available remote branch names for instance to create a local branch from the remote branch origin/todo I would have to run git switch todo if yes when I make some changes to this newly created local branch todo and try to push it to the same remote repository will the remote repository recognize it as a different branch and create one or do perform some other action slightly? confused with this. – aniketh deokar Dec 21 '21 at 13:42
  • Inspect the remote-tracking names that `git branch -r` lists. Remove `origin/` or the other prefix (whatever it is: there's one per remote) from each name. Does the result match the name you typed in? If so, that's a *candidate* for automatic creation. How long is your final list of candidates? If it's one entry long, that's the matching name and your (local) branch will be created. – torek Dec 21 '21 at 19:07
  • If **you** use the same name (e.g., `seen` or `todo`) as "he" (the other Git repository) is using, you can then use the shorthand for `git push`, i.e., `git push origin todo`. If you use a *different* name—for instance, if instead of `todo` you call yours `toodle-oo`—you must write `git push origin toodle-oo:todo`. That's really all there is to it: your names are *yours* and his names are *his* and they only match if *you choose to make them match*. You probably *want* to do this for your own convenience, but there's no relationship between your branch names and his. – torek Dec 21 '21 at 19:09
  • In general it's unwise to make the names different because we (humans) are bad at that. Git isn't a human and Git makes computer-style mistakes, not human-style mistakes, but you're going to type in the commands and *you* will make human-style mistakes, like accidentally using the same name on both "sides". – torek Dec 21 '21 at 19:11
  • Thankyou for explaining it so elaborately – aniketh deokar Jan 28 '22 at 10:33