More Useful Diffs

The default output of the hg diff command is backwards compatible with the regular diff command, but this has some drawbacks.

Consider the case where we use hg rename to rename a file:

$ hg rename a b
$ hg diff
diff -r be6d2cb7c776 a
--- a/a	Tue May 05 06:44:29 2009 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-a
diff -r be6d2cb7c776 b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/b	Tue May 05 06:44:29 2009 +0000
@@ -0,0 +1,1 @@
+a

The output of hg diff above obscures the fact that we simply renamed a file. The hg diff command accepts an option, --git or -g, to use a newer diff format that displays such information in a more readable form:

$ hg diff -g
diff --git a/a b/b
rename from a
rename to b

This option also helps with a case that can otherwise be confusing: a file that appears to be modified according to hg status, but for which hg diff prints nothing. This situation can arise if we change the file’s execute permissions:

$ chmod +x a
$ hg st
M a
$ hg diff

The normal diff command pays no attention to file permissions, which is why hg diff prints nothing by default. If we supply it with the -g option, it tells us what really happened:

$ hg diff -g
diff --git a/a b/a
old mode 100644
new mode 100755

Get Mercurial: The Definitive Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.