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

How to Git Rebase

git rebase is a merge utility just like git merge. But the way they work is different. In this article, I will talk about...
29/12/2020

Configure Git Server with SSH on Ubuntu

If you have a small number of team members working on some projects, then you can setup a Git server via SSH on your office...
29/12/2020

Git Merge –no-ff Option

Understanding Git Merge no-ff Option The easy merge capability of git is one of its strengths. During a merge, git uses...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Reliable IPv4 and IPv6 Subnet Rental Services: The Perfect Solution for Global Businesses
23/12/2024

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024