Mastering git, Part 4, git merge, git diff with GUI tools

Before following this post, make sure you have already set up everything from my other post regarding the configuration of git environment.

Diff

Diff command will allow you to compare two different commits, let’s set up a repository:

Now in the second repository, we make some changes:

now let’s back to the first repository and see the changes:

now you should see all the difference in meld:

To find diff between current and previous commit:

As of Git 1.8.5, @ is an alias for HEAD, so you can use:

If you want to know the diff between head and any commit you can use:

You can add -t to select your difftool such kdiff3, meld, etc.
To see the diff with gitk, first go to Edit>Preference and set the external difftool.
Now click on your revision, then right-click on the revision that you want to compare to and click on Diff this-> selected. Now on the bottom window right click on any of changed file and select External diff

Compare the local repo with its remote branch

to see what the push will do to your local repo.

Merge and conflict

Now let’s merge all the changes. If you make some changes on different files you will do fast-forward. That means you only incorporate new changes and everything is ok afterward. But if you made some changes on the same file in same lines then you have a merge conflict and you should do a three-way merge.

To show you the point let’s make some changes in the first repository in the file A.txt.

now if you call git merge, you will get a merge conflict, to solve this call the following:

and you will get this in meld:

and this in kdiff:

+————————————–+
| LOCAL | BASE | REMOTE |
+————————————–+
|                MERGED               |
+————————————–+

LOCAL: Your side of the conflict – ie, your branch (HEAD) that will contain the results of the merge
REMOTE: The remote side of the conflict – the branch you are merging into HEAD.
BASE: The common ancestor of both local and remote. useful for feeding into a three-way merge tool
MERGED: The result of the automatic merge.

go through the conflict and choose the side (A, B or C) and see the merged file in the window below and save and close.

Use command line and interpret the output of git diff

diff uses Levenshtein distance and tries to determine the smallest set of deletions and insertions. The output is called a “diff”, or a patch, since the output can be applied with the Unix program patch

The POSIX standard specifies the behavior of the “diff” and “patch” utilities and their file formats. You can call the following to see the difference between and two files or directories:

For instance in our case:

-u  indicate unified format

Git also uses the unified format. If you call

You will get the following:

Let’s interpret the output:
1) The first line diff --git a/A.txt b/A.txt is a “git diff” header in the form diff –git a/file1 b/file2. The a/ and b/ filenames are the same unless rename/copy is involved. The –git is to mean that diff is in the “git” diff format.
2) In the last line in extended diff header is:

100644 is the mode of given file which means that it is ordinary file and not e.g. symlink, and that it doesn’t have executable permission bit, and about shortened hash of preimage (the version of file before given change) and post image (the version of file after change).
3) Next is two-line unified diff header

The original file is preceded by “” and the new file is preceded by “+++“.

4) Following this are one or more change hunks that contain the line differences in the file. Unified format hunks starts with line like:

5) Next comes the description of where files differ. The lines common to both files begin with a space character. The lines that actually differ between the two files have one of the following indicator characters in the left print column:

  • ‘+’ — A line was added here to the first file.
  • ‘-‘ — A line was removed here from the first file.

 

Refs 1, 2, 3

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x