Viewing and Creating Branches
Goals
By the end of this lesson you should be able to
- Use the
git branch
command to view, create, and delete branches in a repository - Use the
git log
command to see which commits branches are pointing to
The Branch Command
Listing and Creating Branches
The git branch
command can be used to list or create branches.
$ git branch # list all branches in the repository
$ git branch <name> [<commit_id>] # create a branch with the given name
When you create a new branch it will point to the commit specified by <commit_id>
. If no <commit_id>
is specified it will point to the same commit as HEAD
.
List the branches in your repository:
$ git branch * master
You have only one branch named
master
. The*
indicates the current active branch.Now create two new branches, one named
mybranch
pointing atHEAD
and one namedtemp
pointing at a previous commit of your choice, then list them:$ git branch mybranch $ git branch temp b2764 # You will need to use a different commit ID $ git branch * master mybranch temp
You now have three branches. The
master
branch is still the active branch.
Examining Branches using the Log
Recall that the git log
command indicates any objects that are pointing to each commit.
Confirm that your new branches are pointing at the expected commits:
$ git log --oneline 08f4897 (HEAD -> master, tag: anothertag, mybranch) Renamed git.txt 714c41e Removed unnecessary files 39fa5fc Revert "Revert "Added note about the 'git add' command"" b2763fe (temp) Revert "Added note about the 'git add' command" 87af010 Added note about the 'git add' command ...
Note the following:
HEAD
is pointing tomaster
(this is what makesmaster
the active branch)mybranch
andmaster
point to the same committemp
points to a previous commit
As you can see, the log output indicates which commit each branch is
Deleting Branches
The --delete
or -d
option can be used to remove a branch, as in
# Format for deleting a branch
$ git branch -d <name>
Delete the
temp
branch you just created$ git branch -d temp Deleted branch temp (was b2763fe).