gitdiffgit-diff

How to read the output from git diff?


The man page for git-diff is rather long, and explains many cases which don't seem to be necessary for a beginner. For example:

git diff origin/master

Solution

  • Lets take a look at example advanced diff from git history (in commit 1088261f in git.git repository):

    diff --git a/builtin-http-fetch.c b/http-fetch.c
    similarity index 95%
    rename from builtin-http-fetch.c
    rename to http-fetch.c
    index f3e63d7..e8f44ba 100644
    --- a/builtin-http-fetch.c
    +++ b/http-fetch.c
    @@ -1,8 +1,9 @@
     #include "cache.h"
     #include "walker.h"
     
    -int cmd_http_fetch(int argc, const char **argv, const char *prefix)
    +int main(int argc, const char **argv)
     {
    +       const char *prefix;
            struct walker *walker;
            int commits_on_stdin = 0;
            int commits;
    @@ -18,6 +19,8 @@ int cmd_http_fetch(int argc, const char **argv, const char *prefix)
            int get_verbosely = 0;
            int get_recover = 0;
     
    +       prefix = setup_git_directory();
    +
            git_config(git_default_config, NULL);
     
            while (arg < argc && argv[arg][0] == '-') {
    

    Lets analyze this patch line by line.

    The optional header shows the C function where each change occurs, if it is a C file (like -p option in GNU diff), or the equivalent, if any, for other types of files.


    So, for example, first chunk

         #include "cache.h"
         #include "walker.h"
         
        -int cmd_http_fetch(int argc, const char **argv, const char *prefix)
        +int main(int argc, const char **argv)
         {
        +       const char *prefix;
                struct walker *walker;
                int commits_on_stdin = 0;
                int commits;
    

    means that cmd_http_fetch was replaced by main, and that const char *prefix; line was added.

    In other words, before the change, the appropriate fragment of then 'builtin-http-fetch.c' file looked like this:

        #include "cache.h"
        #include "walker.h"
        
        int cmd_http_fetch(int argc, const char **argv, const char *prefix)
        {
               struct walker *walker;
               int commits_on_stdin = 0;
               int commits;
    

    After the change this fragment of now 'http-fetch.c' file looks like this instead:

        #include "cache.h"
        #include "walker.h"
         
        int main(int argc, const char **argv)
        {
               const char *prefix;
               struct walker *walker;
               int commits_on_stdin = 0;
               int commits;
    

    As Donal Fellows said it is best to practice reading diffs on real-life examples, where you know what you have changed.

    References: