How to get diff working like git-diff?

git diff between commits
git diff example
git diff two files
git diff-files only
git diff head
git diff commit
git diff between branches
git diff specific file

I like the output formatting of git diff. The color and the +/- representation of changes between lines is easier to read than GNU diff.

I can run git diff using --no-index flag outside of a git repo and it works fine. However, it appears to be missing the --exclude option for excluding files or subdirectories from a recursive diff.

Is there a way to get the best of both worlds? (color options and +/- format of git diff and --exclude option of GNU diff).

I've experimented with colordiff, but I still prefer the output format of git diff

I don't know how to do color but this will do the +/- rather than < and >.

diff -u file1 file2

Git Best Practices: Upgrading the Patch Process, a patch if you have other uncommitted changes in your code. Once configured, git diff will first run the binary file through the configured converter script and diff the converter output. The same technique can be applied to get useful diffs from all sorts of binary files, for example: zips, jars and other archives: using unzip -l

You can also use git diff --no-index -- A B (via manpage).

Git Diff, data sources. These data sources can be commits, branches, files and more. git diff [<options>] [--] [<path>…. This form is to view the changes you made relative to the index (staging area for the next commit). In other words, the differences are what you could tell Git to further add to the index but you still haven’t.

  1. Install colordiff.

  2. Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):

    # be more git-like:
  3. Use colordiff -u file1 file2 for two files or colordiff -ruN path1 path2 for recursively comparing paths.

It's not exactly the same, but it's very close.

How To Compare Two Git Branches – devconnected, a reference to the last commit in the currently checked-out branch. git diff HEAD [filename] // compare the working directory with index. git diff [filename] // compare the index with local repository. git diff --cached [filename] You can also compare files between two different commits. Every commit in Git has a commit id which you can get when you give git log. Then you can use the commit id if diff command

Git diff HEAD vs --staged, This document will discuss common invocations of git diff and diffing work flow In our current example, we only have one chunk as we are working with a  also, if you want to remove the silly 'a/' and 'b/' prefixes in diff result, you can set git config --global diff.noprefix true. – Liang Oct 11 at 8:36. git diff HEAD file. will show you changes you added to your worktree from the last commit. All the changes (staged or not staged) will be shown. Share a link to this answer. improve this answer.

Using only bash, diff, tput, and less, we can closely approximate the output of git diff. There will be some notable differences, though, due to the short-sightedness of the diff programmers.

Put the following Bash function definition in some file that gets sourced automatically by your user account, and you'll be able to access the function from the command line:

function gdiff()
    local REG=`tput op`
    local GRP=`tput setaf 6`
    local ADD=`tput setaf 2`
    local REM=`tput setaf 1`

    local NL=$'\n'
    local GRP_LABEL="${GRP}@@ %df,%dn +%dF,%dN @@${REG}"

    local UNCH_GRP_FMT=''

    [[ "${1}" == '@full' ]] && {


    diff \
        --new-line-format="${ADD}+%L${REG}" \
        --old-line-format="${REM}-%L${REG}" \
        --unchanged-line-format=" %L${REG}" \
        --new-group-format="${GRP_LABEL}${NL}%>" \
        --old-group-format="${GRP_LABEL}${NL}%<" \
        --changed-group-format="${GRP_LABEL}${NL}%<%>" \
        --unchanged-group-format="${UNCH_GRP_FMT}" \
            "${@}" | less -FXR

This function works as follows:

  1. Ultimately, diff gets invoked with various formatting options to specify how changes within the files will be displayed.
  2. tput is used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replace tput setaf with tput setf.
  3. The output of diff is piped into less. -R allows ANSI colors to be preserved. -X prevents less from clearing the screen upon exiting. -F prevents less from operating as a pager if the output fits within one screen.
  4. If the first parameter is @full, the function will display all unchanged lines in addition to added and removed lines.

Note the following differences between this approach and git diff:

  1. git diff reports three lines of context surrounding each change. Unfortunately, diff seems to complain and exit if you want to specify the number of context lines while also simultaneously specifying formatting options. (At least it does in Mac OS X Yosemite). Thanks diff programmers. Therefore, you can either request no lines of context surrounding each change, which is the default behavior, or you can request that all unchanged lines within the file are also reported, by specifying @full as the first parameter.
  2. Because the lines of context are different from git diff, the line numbers reported by this function will also vary from those reported by git diff.
  3. You may see the presence of single-line changes reported, which is the correct behavior, but annoying when your changed file contains the insertion of single empty lines. I think git diff deals with this better, via its lines of context. You could try passing different options to diff to better deal with whitespace, if you prefer.

git-diff Documentation, A diff doesn't show the complete file from beginning to end: you wouldn't want to see everything in a 10,000 lines file, when only 2 lines have changed. the "git status" command to see which files were currently changed in our working copy. Commit your dirty working tree: git add . git commit -m "Dirty commit". Diff the stash with that commit: git diff HEAD stash@{0}. Then, afterwards, you may revert the commit, and put it back in the working dir: git reset --soft HEAD~1 git reset .

Inspecting Changes with Diffs, NAME. git-diff - Show changes between commits, commit and working tree, etc it will have the same effect as using HEAD instead. git diff [--options] <commit>. And if you always skip the staging area with -a when you commit, then you can simply use git diff. <1> git diff shows unstaged changes. <2> git diff --cached shows staged changes. <3> git diff HEAD shows all changes (both staged and unstaged). Source: git-diff(1) Manual Page – Cerran Feb 20 '14 at 13:16

git-diff - Show changes between commits, commit , The 'diff' command in Git will show you the same thing - the changes between two 'git diff' is to see what changes you've made in your working directory that have I can easily see the function that I added and now I can decide if I want to  You can git branch -a to list all branches (local and remote) then choose branch name from list (just remove remotes/ from remote branch name. Example: git diff master origin/master (where "master" is local master branch and "origin/master" is a remote namely origin and master branch.) improve this answer. edited Apr 25 '18 at 18:39.

Git Diff, This is to view the changes between two arbitrary <commit>. git diff [options] it will have the same effect as using HEAD instead. git diff [--options] <commit>. The 3 main types of git diff commands you would likely use are: git diff: Show differences between your working directory and the index. git diff –cached: Show differences between the index and the most recent commit. git diff HEAD: Show the differences between your working directory and the most recent commit.

  • To make the blue for additions green, change newtext in /etc/colordiff. I think git uses green?
  • Cool, this combined with colordiff gets me close enough to what I want. Guess I need to scroll further down the man page next time... Thanks!
  • A simple way to get colorization with diff -u, is also to pipe the output to tig, the commandline git repo viewer: diff -u file1 file2 | tig.
  • Install colordiff from your apt/yum/pacman repository and use it.
  • Needed to enable Extra Packages for Enterprise Linux (EPEL) on Amazon Linux to install colordiff:…
  • Besides colordiff, you can also get color with vim by defining cdiff() { diff -u $@ | vim -R -; }.
  • +1, but sadly this doesn't work if one of the files is a symlink.
  • +1 This is very useful as it shows how to make git report where two tracked files A and B differ in comparison with each other instead of where each file has been modified relative to their last respective revision.
  • @EmilLundberg: works for me with symlinks in git 1.9.1 on Linux. I do not know whether earlier versions are broken.
  • git diff --no-index is great, but as the OP pointed out, it lacks the --exclude flag, so it's often of very limited usefulness.
  • To have a single bash function for this add to the .bashrc: function gdiff () { diff -u $@ | colordiff | less -R; }
  • This is for the diff in git. He was asking for the diff program options