Apr 272015
 

One of the most common operations in the development of a project that makes use of a version control system, is that of reviewing the differences between the local copy of a given file in the working area, and the last version of the same file stored in the repository.

Using a graphical comparison tool for this operation may help greatly to perform this task. A tool of this kind will show both versions of the file side-by-side, highlighting the lines that have been added, deleted of modified.

This post explains how to do this and other similar operations in a project that uses git as version control system.

1. The working and staging areas, and the git repository

Unlike other control version systems, such as subversion, every git installation is a fully autonomous repository. For this reason, in git there are three locations where a file can be found, and the file could have a different content in each of them:

  • The file in the working area – The working area is the place where the project files are edited
  • The file in the staging area – When the “git add” command is used on a file that has been created or modified in the working area, a copy of the file is saved in a intermediate area known as the staging area. If the file is later modified in the working area, the “git add” command needs to be executed again to update the copy in the staging area.
  • The versioned file in the repository – When the “git commit” command is executed, the copy of the file in the staging area is saved in the repository as a new version.

2. Comparing visually two copies of a file

The usual way to compare two copies of a given file in git is by means of the “git diff” command. The copies to be compared are specified using modifiers:

  • git diff <file> – compare the file in the working are with the same file in the staging area
  • git diff HEAD <file> – compare the file in the working area with the most recent version saved in the repository.
  • git diff –staged <file> – compare the file in the staging area with the most recent version saved in the repository

Using git diff, the differences are output to the terminal in patch format.

A visual comparison using an external tool can be done, replacing “git diff” with “git difftool –tool=<tool>” in any of the above commands.

Example:

Create a file “test.txt” with the content:

This is a test file
The first version has three lines
This is the last line

Add the test file to the staging area:

$ git add test.txt

Then, modify the file in the working area. The content of the modified file is:

This is a test file
The second version has four lines
This is a line added in the second version
This is the last line

The comparison performed with the usual “git diff” command produces the following output:

$ git diff test.txt
diff --git a/test.txt b/test.txt
index 671f981..f13cd37 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,4 @@
 This is a test file
-The first version has three lines
+The second version has four lines
+This is a line added in the second version
 This is the last line
$

But, using “git difftool” to perform the comparison with a visual tool, such as “meld”, we get a much more readable side-by-side comparison:

$ git difftool --tool=meld test.txt

Viewing: 'test.txt'
Launch 'meld' [Y/n]:

git-visual-diff-en

meld highlights the lines that have been added, deleted or modified, and can be used to edit the copy of the file in the working area, reverting to their saved content some or all the modified lines.

Other visual tools available

Besides “meld”, other visual tools to be used for the comparison can be specified in the –tool option of “git difftool”. In the “git-difftool” manpage, the following tools are mentioned:

araxis, bc3, deltawalker, diffuse, emerge, ecmerge, gvimdiff, kdiff3, 
kompare, meld, opendiff, p4merge, tkdiff, vimdiff and xxdiff

 Posted by at 7:50 am

 Leave a Reply

(required)

(required)