Why check out the master before creating a new branch?

I am reading the Rails Tutorial book and over and over again the author asks the reader to run the following two commands:

$ git checkout master
$ git checkout -b a

      

I understand what these commands are doing: they checkout a branch master

and then create and checkout a new branch named a

.

Why do we need the first line? Does it matter, or can I just leave it?

+3


source to share


3 answers


When you check in a branch, you will detach it from a specific commit. Master checking ensures that your new branch starts at the head (last commit) of the master branch.



+3


source


TL; DR

Why do we need the first line? [...]

This is a slightly roundabout way of making sure the new branch a

points to the same message as master

.

More detailed answer

What git checkout -b newbranch

does

To fix ideas, you can come up with a pointer HEAD

as the You-are-here label on the subway map, which is your commit graph. Now, the next command,

git checkout -b newbranch

      

creates and checks out a new named branch newbranch

that points to the same commit that it points to HEAD

(directly or indirectly). For example, if your repo looked like this,

enter image description here

running git checkout -b newbranch

you get

enter image description here

Where Michael Hartl Leads You

However, Michael Hartl (author of the Rails Tutorial ) wants you to create and checkout a new branch that points to a specific commit: the tip of the branch master

(i.e. the commit that the branch points to master

):

enter image description here

So why check first master

?

Asking you to run



git checkout master

      

Michael Hartl just makes sure that your HEAD

points (indirectly) to the right are fixed, i.e. tip master

:

enter image description here

Then by running

git checkout -b newbranch

      

will definitely create a new branch where needed:

enter image description here

A more direct approach: two birds, one stone ...

If you check it first master

, it might seem a little cumbersome. You can condense these two commands into one:

git checkout -b newbranch master

      

Regardless of where you are in your repo (i.e. where it HEAD

points to), this command will create and checkout a new branch with a name newbranch

that points to the end master

.

However, since the Rails tutorial is for people who are new to Git , and because

git checkout -b newbranch master

      

slightly more advanced and arguably less intuitive than

git checkout master
git checkout -b newbranch

      

you cannot blame Michael Hartl for recommending the latter.

+7


source


The branch will be created based on what you start when you create a branch. Building it from Master HEAD is a common pattern, but it depends on the branch workflow you want to use. It is important to have a git workflow and all people in the project follow the same patterns in order to maintain and maintain all members. Check out this link to see some of the more common git workflows.

+1


source







All Articles