Home > OS >  How or rather when does a local branch get created from a remote branch in Git ? commands required t
How or rather when does a local branch get created from a remote branch in Git ? commands required t

Time:12-22

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?

CodePudding user response:

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.

CodePudding user response:

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

  • Related