Git Compare Two Branches

28/12/2020
Git

Almost all version control systems have branching options. But Git is known for its fast branching capabilities. Git branches are lightweight. So the performance penalties for branching are minimal and development teams are encouraged to branch and merge as much as possible. But when you are working with multiple branches, it’s important to be able to compare and contrast the differences. In this tutorial, we will go through a workflow to see how we can compare various branches and commits.Let’s first set up the following situation:

C00  =>  C01 => C03  => C06 (master)

     

       C02 => C04 => C05 (development)

The following steps were taken:

  • C00: Added hello_world.py (master branch)
  • — Created the development branch
  • C01: Modified hello_world.py to add second hello (master branch)
  • C02: Modified hello_world.py to add Development branch says Hello (development branch)
  • C03: Added readme.txt (master branch)
  • C04: Modified hello_world.py to add Development branch says Hello again (development branch)
  • C05: Added info.txt (development branch)
  • C06: Modified readme.txt to add second line (master branch)

After all the commits, the ‘master’ branch has the following files:

hello_world.py
readme.txt

And the ‘development’ branch has the following files:

hello_world.py
info.txt


Comparing the heads of two branches

You can use the name of the branches to compare the heads of two branches:

$ git diff master..development

diff –git a/hello_world.py b/hello_world.py
index e27f806..3899ed3 100644
a/hello_world.py
+++ b/hello_world.py
@@2,7 +2,7 @@

def main():
print("First Hello!")
– print("Second Hello!")

+ print("Development branch says Hello")
+ print("Development branch says Hello again")
if __name__ == "__main__":
main()
diff –git a/info.txt b/info.txt
new file mode 100644
index 0000000..0ab52fd
/dev/null
+++ b/info.txt
@@0,0 +1 @@
+New information
diff –git a/readme.txt b/readme.txt
deleted file mode 100644
index e29c296..0000000
a/readme.txt
+++ /dev/null
@@1,2 +0,0 @@
-1 First line of readme.txt
-2 Second line of readme.txt

The diff command is recursively looking at the changes. It has run the following diffs:

diff –git a/hello_world.py b/hello_world.py
diff –git a/info.txt b/info.txt
diff –git a/readme.txt b/readme.txt

Here ‘a’ stands for the ‘master’ branch and ‘b’ stands for the development branch. The ‘a’ is always assigned to the first parameter and ‘b’ to the second parameter.  The /dev/null means that branch doesn’t have the file.


Comparing between commits

In our example, the ‘master’ branch has the following commits:

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

$ git log –oneline
caa0ddd C06: Modified readme.txt to add second line (master branch)
efaba94 C03: Added readme.txt (master branch)
ee60eac C01: Modified hello_world.py to add second hello (master branch)
22b4bf9 C00: Added hello_world.py (master branch)

The development branch has the following commits:

$ git status
On branch development
nothing to commit, working directory clean

$ git log –oneline
df3a4ee C05: Added info.txt (development branch)
0f0abb8 C04: Modified hello_world.py to add Development branch says Hello again (development branch)
3f611a0 C02: Modified hello_world.py to add Development branch says Hello (development branch)
22b4bf9 C00: Added hello_world.py (master branch)

Suppose we want to compare the hello_world.py for C01 and C02 commits. You can use the hashes to compare:

$ git diff ee60eac:hello_world.py 3f611a0:hello_world.py

diff –git a/ee60eac:hello_world.py b/3f611a0:hello_world.py
index e27f806..72a178d 100644
a/ee60eac:hello_world.py
+++ b/3f611a0:hello_world.py
@@2,7 +2,7 @@

def main():
print("First Hello!")
– print("Second Hello!")
+ print("Development branch says Hello")

if __name__ == "__main__":
main()

You can use the same principle to compare commits within the same branch also.


Visual Merge Tools

Looking at text-based comparisons can be difficult. If you set up the Git difftool with a visual merge application like DiffMerge or BeyondCompare, you will be able to see differences better.

Further Study:

References:

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

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

SSH Key Setup for GitHub

To understand Github you first need to make sure you are aware of a Git . Git is a open source version control system that...
29/12/2020

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...
28/12/2020
Bài Viết

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

Hướng Dẫn Chọn Dịch Vụ Thuê Địa Chỉ IPv4
10/03/2025

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