Git lesson 9: Deleting branches

In the previous lesson we merged the changes we made on our bug_fix_#001 branch into our master branch. Because the bug is now fixed, we have no need to keep the associated branch active. In this lesson we will learn how to close an active branch, as well as start a new, slightly more complicated example using branches.

Closing an active branch

We are done with our bug_fix_#001 branch, so to tidy up our repository we will close that branch. This action is quite simple:

09:34 ~/.../project_repo$ git branch
  bug_fix_#001
* master

09:35 ~/.../project_repo$ git branch -d bug_fix_#001
Deleted branch bug_fix_#001 (was a81aa62).

09:35 ~/.../project_repo$ git branch
* master

We first verified what branches existed in our repository and what branch we were on using git branch. Next, we deleted the bug_fix_#001 branch using the git branch -d <branch_name> command. Afterwards, we verified that the branch was indeed closed by running git branch again.

Although the branch is now closed, the work that was done on bug_fix_#001 is still part of our repository. Run git log to confirm that your last commit relates to the work that was done on bug_fix_#001 and merged into your master branch. This will become a little clearer as we work through our next example using branches.

A new example using branches

We currently only have one branch in our repository, the master branch. First, let’s add a new file that contains a draft of the introduction to our new paper. As always, I will use the echo command to generate the files, but you can write the text in a text editor and save the file in the main folder of your repository.

echo 'This is an important area of research.' > intro.txt
echo 'Others have studied this problem.' >> intro.txt
echo 'But they did not completely succeed.' >> intro.txt
echo 'Thus, in this study we succeed.' >> intro.txt

Now we can go ahead and add this file to our repository using git add <filename> and commit this change using git commit -m '<commit message'>.

We are writing this paper with our colleague named Beatrice, but unfortunately Beatrice does not know how to use Git. So we will create a branch in our repository named after her, which is where we will include any comments or changes she makes to our intro.txt.

Let’s go ahead and create a new branch using git branch beatrice. We now e-mail Beatrice a copy of our introduction for her comments; at this point it does not matter if we send her the version on the master or beatrice branch of our repository because the two files are identical!

Summary

In this post we learned how to delete branches using git branch -d <branch_name>. We also started work on the introduction to our new paper and created a new branch for our colleague using git branch <branch_name>. In the next lesson we will learn how to merge changes from a branch and deal with the situation where there is a conflict between the version of the file on the master branch and another branch.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s