Dev Ops Git

Basics of Git Merging and Deleting Branches

Branching can help you keep your work organized. However, you need to be able to merge your work in order to make the work coherent. If you never merge and delete the branches, your history might become too chaotic to understand.

Working with Merging and Branch Delete

Let’s first create a master branch, put in a few commits, create a new branch called features, add a few commits, then come back to master and commit again. Here are the commands:

$ mkdir mygame
$ cd mygame
$ git init
$ echo "Design Decision 1: Brainstarm" >> design.txt
$ git add -A
$ git commit -m "C0: Started Project"
$ echo "Design Decision 2: Write Code" >> design.txt
$ git add -A
$ git commit -m "C1: Submitted Code"
$ git branch features
$ git checkout features
$ echo "Add Feature 1" >> feature.txt
$ git add -A
$ git commit -m "C2: Feature 1"
$ echo "Add Feature 2" >> feature.txt
$ git add -A
$ git commit -m "C3: Feature 2"
$ git checkout master
$ echo "Modifying Master Again" >> design.txt
$ git add -A
$ git commit -m "C4: Master Modified"

The above commands created the following situation:

You can check the history of the two branches to see what commits they have:

$ git status
On branch master
nothing to commit, working directory clean
$ git log --oneline
2031b83 C4: Master Modified
1c0b64c C1: Submitted Code
 
$ git checkout features
Switched to branch 'features'
 
$ git log --oneline
93d220b C3: Feature 2
ad6ddb9 C2: Feature 1
1c0b64c C1: Submitted Code
ec0fb48 C0: Started Project

Now let’s suppose, you want to bring all the changes from the features branch to our master branch. You will have to start the process from the destination of the merge. Because we want to merge into the master branch, you need to initiate the process from there. So let’s check out the master branch:

$ git checkout master
Switched to branch 'master'
 
$ git status
On branch master
nothing to commit, working directory clean

Now let’s create the merge:

$ git merge features

If there are no conflicts in the merge, you will get a text editor open up with the comments:

Merge branch 'features'
 
# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.

You can modify the comments or accept the default ones. The merge output should show results like this:

Merge made by the 'recursive' strategy.
feature.txt | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 feature.txt

After the merge, you have the following condition:

If you check the logs, you will find:

$ git status
On branch master
nothing to commit, working directory clean
 
$ git log --oneline
46539a3 C5: Merge branch 'features'
2031b83 C4: Master Modified
93d220b C3: Feature 2
ad6ddb9 C2: Feature 1
1c0b64c C1: Submitted Code
ec0fb48 C0: Started Project

You have successfully merged the changes. However, the feature branch is still present.

$ git branch -a
features
* master

You can delete it with the following command:

$ git branch -d features

If you check now, you should only see the master branch:

$ git branch -a
* master

Conclusion

Make sure you regularly check for unused branches and delete them. You want to keep your repository clean to make it easy to navigate and understand.

Further Reading:

About the author

Zak H

Zak H

Zak H. lives in Los Angeles. He enjoys the California sunshine and loves working in emerging technologies and writing about Linux and DevOps topics.