Every commit is linked to its predecessor through its parent commit ID. The named head points to a series of linked commits in a path is called a
branch. The default branch in Git is called the
master. When a branch is labeled, the commits can be referenced using their branch name instead of the commit ID. The command git branch branch-name branch-start
assigns a new branch label to an existing starting point (commit ID or branch name). In the following example, we will create the branch oldmaster
that points to our first commit:
$ git branch oldmaster 4febfb3
The branch label is automatically updated with the latest commit added to it.
The command git branch
without arguments displays the list of named branches, the current one prefixed by *
:
$ git branch
* master
oldmaster
The command git checkout branch
(commit ID or branch name) extracts from the Git repository all of the files associated to the commit pointed to branch
:
$ git checkout oldmaster Switched to branch 'oldmaster' $ cat hello.txt hello world $ git checkout master Switched to branch 'master' $ cat hello.txt hello git world
It can be very useful to display the current branch point on the working directory. When using bash, it is possible to set up a custom prompt which includes the current branch as a prefix under brackets:
$ source \ /usr/local/git/contrib/completion/git-completion.bash $ export PS1='$(__git_ps1 " (%s)") \$ ' (master) $ git checkout oldmaster (oldmaster) $