Create local branch from a remote branch

Asked

Viewed 11,154 times

6

I have a cloud branch on Github called x1 and I need to create a local branch called xlocal from that cloud branch called x1. How do I do that?

  • 1

    Hello, Sena! If one of the answers below helped you, choose one of them and mark as "accept" :)

  • 1

    I must here ratify what Dherik said, so that you mark as "accept" the answer that best solved your problem

3 answers

6


You can create a branch from another one as follows.

First, do the git pull to ensure that you receive the branch x1 in your repository. Then log into the branch x1 using the command checkout:

git checkout x1

Then, being in the branch x1, create another branch using the option -b. In this case, the xlocal:

git checkout -b xlocal

The option -b specifies that a new branch will be created from the branch on which it is.

  • Thank you very much!

  • 1

    In the same lie

  • 1

    @hugocsl upvoto to strengthen the brothers :D

  • 1

    Tmj in the brotherhood then rss

6

Complementing the other answers, follows an alternative.

If the remote repository has a branch x1 (you do not own locally), first you need to pull this information from the remote repository. You can use git pull, as you suggested one of the answers, but this command will also update your current branch if there are new commits remote that you don’t already own locally (for example, if I’m on master and do git pull, to master will be updated if there are new commits in the remote repository master).

If you just want to pull branches and commits remote, but not yet want to update your local repository, you can use git fetch, which is enough to pull the information from the branch x1 remote.

Then the other answers suggest starting with git checkout x1. This is not wrong, but will create a local branch also called x1, which in turn will be "tracking" the remote branch x1.

But think about it, if you want to create another branch xlocal based on the remote branch x1, will also need to create a local branch x1 that will only serve to create the xlocal? Because that’s what the other answers are doing (not that it’s wrong, only you’ll have a local branch for nothing, which won’t be used for anything else - assuming you want to do all the work on xlocal, because I believe this is why I created it).

So you could just do:

git checkout -b xlocal origin/x1

So I say I want to create the branch xlocal, and that it should be created from the branch origin/x1 (that is, the branch x1 remote repository). Here I am assuming that the remote repository is the origin, clear-cut (it is possible to have multiple remote repositories configured, each with its own name).

This way the local branch is not created x1. And just as you indicated one of the answers, the first time the push you must inform that the local branch is "tracking" this remote:

git push -u origin xlocal

Next time this will no longer be necessary, just do git push.

5

First gives a git checkout x1.

Then already inside the branch x1 you’re gonna give a git checkout -b xlocal.

This will create a new branch from x1 by the name of xlocal.

Only in the first push of that branch new you’ll have to give a git push -u origin xlocal , but after that it’s just give git push that will already be done the direct push.

-u is an abbreviation of --set-upstream which serves for the new branch to be "tracked" by master, because before the first push this new branch does not exist in master for anyone yet.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.