Git

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:

Jocuri HD remasterizate pentru Linux care nu au avut niciodată lansare Linux mai devreme
Mulți dezvoltatori și editori de jocuri vin cu remasterizarea HD a jocurilor vechi pentru a prelungi durata de viață a francizei, vă rog fanilor să so...
Cum se utilizează AutoKey pentru automatizarea jocurilor Linux
AutoKey este un utilitar de automatizare desktop pentru Linux și X11, programat în Python 3, GTK și Qt. Folosind funcțiile sale de scriptare și MACRO,...
Cum se arată FPS Counter în jocurile Linux
Jocurile cu Linux au primit un impuls major când Valve a anunțat suportul Linux pentru clientul Steam și jocurile acestora în 2012. De atunci, multe j...