Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `git checkout -b` with and without a branch starting point

Tags:

git

I am new to Git, I want to know the difference between two commands.

`git checkout -b <branch-name>`
`git checkout -b <branch-name> origin/master`

If I execute the first command, how does Git create the branch? Will the branch be created from local master or remote master?

like image 559
Lolly Avatar asked May 03 '13 08:05

Lolly


People also ask

What is start point in git checkout?

Checkout a New Branch or Reset a Branch to a Start PointIf the BRANCH-NAME branch doesn't exist, Git will create it and start it at START-POINT . If the BRANCH-NAME branch already exists, then Git resets the branch to START-POINT . This is equivalent to running git branch with -f .

What is the right way to perform checkout in git?

In order to checkout a remote branch you have to first fetch the contents of the branch. In modern versions of Git, you can then checkout the remote branch like a local branch. Older versions of Git require the creation of a new branch based on the remote .

How do I checkout from an old branch?

Use the git switch - (Or git checkout - ) to switch to the previous branch you were working with. This is pretty similar to the cd - command, which is used to switch to the previous directory.


1 Answers

If you don't specify a starting point, the new branch is created from what you currently have checked out (the current HEAD).

git-checkout:

git checkout -b|-B <new_branch> [<start point>]

Specifying -b causes a new branch to be created as if git-branch(1) were called and then checked out.

And git-branch:

[...] The command’s second form creates a new branch head named <branchname> which points to the current HEAD, or <start-point> if given.

like image 106
Mat Avatar answered Nov 15 '22 20:11

Mat