Git Essentials(Second Edition)
上QQ阅读APP看书,第一时间看更新

Creating a new branch

Now that we have warmed up, the fun begins. Let's see what happens when you ask Git to create a new branch. Since we are going to serve a delicious fruit salad, it's time to set a branch apart for a berries-flavored variant recipe:

[9] ~/grocery (master)
$ git branch berries

That's all! To create a new branch, all you need to do is call the git branch followed by the name of the branch you'd like to use. And this is super-fast; always working locally, Git does this kind of work in a blink of an eye.

To be true, there are some (complicated) rules to be respected and things to know about the possible name of a branch (all you need to know is here: https://git-scm.com/docs/git-check-ref-format), but for now it is not relevant.

So, git log again:

[10] ~/grocery (master)
$ git log --oneline --graph --decorate
* 0e8b5cf (HEAD -> master, berries) Add an orange
* e4a5e7b Add an apple
* a57d783 Add a banana to the shopping list

Wonderful! Now Git tells us there's a new branch, berries, and it refers to the same commit as a master branch.

Anyway, at the moment we continue to be located in the master branch; in fact, as you can see in the shell output prompt, it continues to appear (master) between the round parenthesis:

[10] ~/grocery (master)

How can I switch branch? By using the git checkout command:

[11] ~/grocery (master)
$ git checkout berries
Switched to branch 'berries'

Do a git log to see:

[12] ~/grocery (berries)
$ git log --oneline --graph --decorate
* 0e8b5cf (HEAD -> berries, master) Add an orange
* e4a5e7b Add an apple
* a57d783 Add a banana to the shopping list

Mmm, interesting! Now there's a (berries) sign into the shell prompt, and more, something happened to that HEAD thing: now the arrows points to berries, not more to master. What does it mean?