Basics of Git Merging and Deleting Branches

28/12/2020
Git
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:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

GitLab Container Registry Setup

In our last post we showed you how you can set up a GitLab instance to manage your organization’s or your personal projects....
28/12/2020

Install Git on Ubuntu

Git is a distributed version control system. It is used to track changes in source codes during software development. It...
29/12/2020

Installing and Getting Started with Git on Debian 10

Git is a very popular Version Control System (VCS). It is used to track changes in source codes during software development....
29/12/2020