Can I make 'git diff' only display the line numbers AND changed file names?

155,167

Solution 1

Note: if you're just looking for the names of changed files (without the line numbers for lines that were changed), see another answer here.


There's no built-in option for this (and I don't think it's all that useful either), but it is possible to do this in Git, with the help of an "external diff" script.

Here's a pretty crappy one; it will be up to you to fix up the output the way you would like it.

#! /bin/sh
#
# run this with:
#    GIT_EXTERNAL_DIFF=<name of script> git diff ...
#
case $# in
1) "unmerged file [email protected], can't show you line numbers"; exit 1;;
7) ;;
*) echo "I don't know what to do, help!"; exit 1;;
esac
path=$1
old_file=$2
old_hex=$3
old_mode=$4
new_file=$5
new_hex=$6
new_mode=$7
printf '%s: ' $path
diff $old_file $new_file | grep -v '^[<>-]'

For details on "external diff", see the description of GIT_EXTERNAL_DIFF on the Git manual page (around line 700, pretty close to the end).

Solution 2

Use:

git diff --name-only

Go forth and diff!

Solution 3

Line numbers as in number of changed lines or the actual line numbers containing the changes? If you want the number of changed lines, use git diff --stat. This gives you a display like this:

[[email protected]:~/newsite:master]> git diff --stat
 whatever/views/gallery.py |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

There is no option to get the line numbers of the changes themselves.

Solution 4

Use:

git diff master --compact-summary

The output is:

 src/app/components/common/sidebar/toolbar/toolbar.component.html   |  2 +-
 src/app/components/common/sidebar/toolbar/toolbar.component.scss   |  2 --

This is exactly what you need. The same format as when you are making a commit or pulling new commits from the remote.

PS: It's weird that nobody answered this way.

Solution 5

1) My favorite:

git diff --name-status

Prepends file status, e.g.:

A   new_file.txt
M   modified_file.txt 
D   deleted_file.txt

2) If you want statistics, then:

git diff --stat

will show something like:

new_file.txt         |  50 +
modified_file.txt    | 100 +-
deleted_file         |  40 -

3) Finally, if you really want only the filenames:

git diff --name-only

Will simply show:

new_file.txt
modified_file.txt
deleted_file
Share:
155,167

Related videos on Youtube

wei
Author by

wei

Updated on June 15, 2021

Comments

  • wei
    wei over 1 year

    This question calls for "line numbers". If you do not care about line numbers in the output, see this question and answer.


    Basically, I don't want to see the changed content, just the file names and line numbers.

  • wei
    wei almost 11 years
    I was thinking the actual line numbers. Thanks though.
  • ThiefMaster
    ThiefMaster almost 11 years
    I somehow doubt he wants a GUI tool for this.
  • Pieter Müller
    Pieter Müller about 9 years
    This is possible the answer most people are looking for when they view this page (it was for me). However, it doesn't answer the original question, which specifically mentions line numbers.
  • adamwong246 almost 9 years
    This should NOT be the accepted answer as it only solves half the problem- you still need to output which lines (for each file) that were changed.
  • Adam Arold
    Adam Arold over 6 years
    That's it! I was looking for this switch exactly!
  • Naveen Paul
    Naveen Paul about 6 years
    Why not use "git status"? It also tells you the untracked files.
  • Hettomei
    Hettomei almost 6 years
    @JimmyPaul because sometimes you already have commited. For example you need to list changed file between master and your current advanced branch git diff --name-only master..HEAD
  • Yury Bondarau
    Yury Bondarau over 5 years
    So easy. Why this answer was not marked as best. It can confuse people who come and not read answers after the one marked as best.
  • Daniel B
    Daniel B over 4 years
    @PieterMüller I agree with you. But that's usually how it works. When you ask more than a question in one go, you usually don't get answer for all of them.
  • poshaughnessy
    poshaughnessy almost 4 years
    Does this require a particular version of git? It's not working for me, passing a hash to diff against (it just spits back the command usage instructions)... This alternative worked for me though: git diff [hash] | grep diff
  • Evan Carroll
    Evan Carroll over 2 years
    @PieterMüller I do agree this is problematic as it's what I was looking for too.

Related