Gita

Git Compare Two Branches

Git Compare Two Branches

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:

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:

Control & manage mouse movement between multiple monitors in Windows 10
Dual Display Mouse Manager lets you control & configure mouse movement between multiple monitors, by slowing down its movements near the border. Windo...
WinMouse lets you customize & improve mouse pointer movement on Windows PC
If you want to improve the default functions of your mouse pointer use freeware WinMouse. It adds more features to help you get the most out of your h...
Mouse left-click button not working on Windows 10
If you are using a dedicated mouse with your laptop, or desktop computer but the mouse left-click button is not working on Windows 10/8/7 for some rea...