Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making git diff --stat show full file path

Tags:

git

git-diff

People also ask

How do I get the full path of a filename?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get the path of a file in git?

While browsing your Git repository, start typing in the path control box to search for the file or folder you are looking for. The interface lists the results starting from your current folder followed by matching items from across the repo.

How do I show more lines in git diff?

Using the -U parameter you can change how many lines are shown above and below a changed section. E.g. git diff -U10 will show 10 lines above and below.

What does git diff show you?

The git diff command shows the differences between the files in two commits or between your current repository and a previous commit. This command displays changes denotes by headers and metadata for the files that have changed.


By default git diff truncates its output to fit into a 80-column terminal.

You can override this by specifying values using the --stat option:

--stat[=<width>[,<name-width>[,<count>]]]
       Generate a diffstat. You can override the default output width for
       80-column terminal by --stat=<width>. The width of the filename
       part can be controlled by giving another width to it separated by a
       comma. By giving a third parameter <count>, you can limit the
       output to the first <count> lines, followed by ...  if there are
       more.

       These parameters can also be set individually with
       --stat-width=<width>, --stat-name-width=<name-width> and
       --stat-count=<count>.

For example, by setting the output value to a very large number:

git diff --stat=10000

Note that produces the path relative to the root of the git repository.

(For scripting you might want to use git diff-tree directly since it's more of a "plumbing" command, although I suspect you'll be fine either way. Note that you need the same extra text with --stat when using git diff-tree. The essential difference between using the git diff "porcelain" front end, and the git diff-tree plumbing command, is that git diff looks up your configured settings for options like diff.renames to decide whether to do rename detection. Well, that, plus the front end git diff will do the equivalent of git diff-index if you're comparing a commit with the index, for instance. In other words, git diff reads your config and invokes the right plumbing automatically.)


For script processing, it might be better to use one of the following:

# list just the file names
git diff --name-only
path/to/modified/file
path/to/renamed/file


# list the names and change statuses:
git diff --name-status
M       path/to/modified/file
R100    path/to/existing/file   path/to/renamed/file


# list a diffstat-like output (+ed lines, -ed lines, file name):
git diff --numstat
1       0       path/to/modified/file
0       0       path/to/{existing => renamed}/file

These each become more handy for robust script processing when combined with the -z option, which uses NUL as the field terminators.


For Bash users, you can use the $COLUMNS variable to automatically fill the available terminal width:

git diff --stat=$COLUMNS

Very long path names might still be truncated; in this case, you can reduce the width of the +++/--- part using --stat-graph-width, for example this limits it to 1/5 of the terminal width:

git show --stat=$COLUMNS --stat-graph-width=$(($COLUMNS/5))

For a more generic solution, you could use the output of tput cols to determine the terminal width.


There’s an option --name-only: git diff --name-only. The option is also supported by other git commands like show and stash.

Paths don’t get shortened with the option.


A simple solution I found was to do this: (only works on *nix, sorry no osx)

git diff --stat=$COLUMNS --relative | head -n -1 | cut -c 2- | xargs -d '\n' -P4 printf "$(pwd)/%s\n"

This version works for both, but it doesn't look great on osx.

git diff --stat=$COLUMNS --relative | sed -e '$ d' | cut -c 2- | xargs -n4 -I{} echo "$(pwd)/{}"