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:
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:
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:
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:
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:
- Git Diff Documentation
- Git DiffTool Documentation
- DiffMerge Setup with DiffTool
- BeyondCompare Setup with DiffTool
References:
- Git Tutorial: Diff and Merge Tools, YouTube
- http://coding4streetcred.com/blog/post/configure-diffmerge-for-your-git-difftool
- http://gitbaby.com/how-to-diff-one-file-to-an-arbitrary-version-in-git.html
- https://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
- https://git-scm.com/book/id/v2/Git-Branching-Branches-in-a-Nutshell
- https://git-scm.com/docs/git-diff
- https://git-scm.com/docs/git-difftool
- https://sourcegear.com/diffmerge/
- https://sourcegear.com/diffmerge/webhelp/sec__git__linux.html
- https://stackoverflow.com/questions/9834689/comparing-two-branches-in-git
- https://veerasundar.com/blog/2011/06/git-tutorial-comparing-files-with-diff/
- https://www.scootersoftware.com/features.php
- https://www.scootersoftware.com/support.php?zz=kb_vcs