官术网_书友最值得收藏!

It's all about labels

In Git, a branch is nothing more than a label, a mobile label placed on a commit.

In fact, every leaf on a Git branch has to be labeled with a meaningful name to allow us to reach it and then move around, go back, merge, rebase, or discard some commits when needed.

Let's start exploring this topic by checking the current status of our grocery repository; we do it using the well-known git log command, this time adding some new options:

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

Let's look at those options in detail:

  • --graph: In this case it just adds an asterisk to the left, before the commit hash, but when you have more branches, this option will draw them for us giving a simple but effective graphical representation of the repository
  • --decorate: This option prints out the labels attached to any commits that are shown; in this case, it prints (HEAD ->master) on the e4a5e7b commit
  • --oneline: This is easy to understand: it reports every commit using one line, shortening things when necessary

We'll now do a new commit and see what happens:

[2] ~/grocery (master)
$ echo "orange" >> shoppingList.txt

[3] ~/grocery (master)
$ git commit -am "Add an orange"
[master 0e8b5cf] Add an orange
 1 file changed, 1 insertion(+)

Have you noticed? After adding an orange to the shopingList.txt, I made a commit without first making git add; the trick is in the -a (--add) option added to the git commit command, which means add to this commit all the modified files that I have already committed at least one time before. In our case, this option allowed us to go faster and skip the git add command.

Anyway, use it carefully, especially while learning and using Git at the beginning: you easily end up doing commit with more files than you want.

Okay, go on now and take a look at the current repository situation:

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

Interesting! Both HEAD and master have now moved on the last commit, the third one; what does it mean?

主站蜘蛛池模板: 涡阳县| 虞城县| 涞水县| 云南省| 长子县| 偏关县| 政和县| 蛟河市| 阜平县| 布拖县| 莆田市| 普安县| 北海市| 青铜峡市| 韶山市| 邹城市| 灵璧县| 德钦县| 长汀县| 冷水江市| 禄丰县| 肇源县| 柳州市| 镇江市| 邹城市| 宁都县| 苏州市| 绍兴市| 东明县| 怀化市| 祁连县| 太湖县| 南阳市| 曲阜市| 郁南县| 多伦县| 锡林郭勒盟| 黄山市| 鄂托克前旗| 都兰县| 海阳市|