gitgit-diff

How can I get git diff to produce simpler output between 2 commits?


I have looked at many explanations about what git diff between 2 commits does, and I am still completely baffled about the output I am getting. I believe what I am seeing is "combined format", said to be the "default" format, but if so I don't know what other formats are available.

Example:

>git diff af738ab0..bbbec26d > gitdiff_2025-04-09B.diff

Typically I get this kind of thing:

...
+diff --git a/src/core/history_table_view.py b/src/core/history_table_view.py
+index 5b18236..05b262a 100644
+--- a/src/core/history_table_view.py
++++ b/src/core/history_table_view.py
+@@ -14,188 +14,279 @@ class HistoryTableView(QtWidgets.QTableView):
+     @thread_check(True)
+     def __init__(self, *args):
+         super().__init__(*args)
+-        self.setObjectName('history table')
+-        self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
+-        self.horizontalHeader().setStretchLastSection(True)
+-        self.horizontalHeader().hide()
+-        self.verticalHeader().hide()
+-        self.setModel(HistoryTableModel(self))
...

... in this part of the output it is comparing the file history_table_view.py in the first commit with the same file in the second commit.

All the above lines are present in both files. So why might this line

+     def __init__(self, *args):    

start with a simple "+", whereas this line

+-        self.setObjectName('history table')

starts with "+-"? What does this mean? As I say both lines are present in both versions of this file. I would expect a git diff in principle not to show any of these lines (although there are some differences in terms of empty lines between the lines with text).

Secondly, when I look at a simple intro to git diff, such as this, the presenter there does a git diff on 2 commits, at 4:40 into that video we see output ... but in his case he is NOT seeing double symbols at the start of lines, but simple "+" or "-", i.e. as far as I understand it what one might expect from a BASH diff command done between two files. THAT SIMPLE FORMAT IS ALL I WANT.

I suspect that there may be an alternative to git diff "combined format" which does indeed produce this simpler output, but I haven't been able to find it. Secondly, the video I reference above was made 7 months ago: why is that Youtuber getting by default, when comparing 2 commits, simple git output (which is perfectly comprehensible to me), while I'm getting (to me) incomprehensible output? My git version is:

git version 2.48.1.windows.1

A simple link to a page which genuinely explains this specific business of git diff comparing 2 commits, in clear terms, and crucially showing how to get a simple diff, might be all I need. I have searched long and hard and not found.

Later.
Can the daoh-nvohter please say why this question is not worthy of being asked?


Solution

  • There is a patch file in your second commit, your a reading a section of the diff which describes "that patch file is added".


    If you want to spot such patch file(s) in a commit, you can for example look for the pattern ^diff --git in the files of said commit:

    git grep -l -e "^diff --git" <commit sha>
    

    run it for example on your bbbec26d commit: git grep -l -e "^diff --git" bbbec26d

    If you see lines with leading -- or -+, this means you also have patch file(s) in your first commit af738ab0

    If you want to have git diff only on a set of files, use the glob syntax for [path...]:

    git diff af738ab0..bbbec26d -- src/ test/   # only files under src/ and test/
    git diff af738ab0..bbbec26d -- '*.py'       # only files with that suffix
    # or
    git diff af738ab0..bbbec26d -- ':!deploy/'  # not that directory
    git diff af738ab0..bbbec26d -- ':!*.patch'  # not files ending with that suffix