问题
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
回答1:
I don't know how to do color but this will do the +/- rather than < and >.
diff -u file1 file2
回答2:
You can also use git diff --no-index -- A B (via manpage).
回答3:
Install colordiff.
Update your ~/.colordiffrc (copying /etc/colordiffrc first, if necessary):
# be more git-like: plain=off newtext=darkgreen oldtext=darkred diffstuff=darkcyanUse
colordiff -u file1 file2for two files orcolordiff -ruN path1 path2for recursively comparing paths.
It's not exactly the same, but it's very close.
回答4:
This is what I suggest and it's pretty close
diff -u FILE1 FILE2 | colordiff | less -R
colordiff: You'll have to install thisbrew install colordiffon my Mac.port install colordiffon some Macs.sudo apt-get install colordiffon Debian or Ubuntu- For other platforms, download the source from the main page or GitHub and follow the installation instructions
-R: this tells Less to show colors instead of the raw codes.
I ultimately used -w because I didn't want to see whitespace diffs.
diff -w -u FILE1 FILE2 | colordiff | less -R
Edit: As suggested by @Ciprian Tomoiaga in the comment, you can make this a function and put it in your ~/.bashrc file too.
function gdiff () { diff -u $@ | colordiff | less -R; }
回答5:
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' ]] && {
UNCH_GRP_FMT="${GRP_LABEL}${NL}%="
shift
}
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:
- Ultimately,
diffgets invoked with various formatting options to specify how changes within the files will be displayed. tputis used to insert ANSI color codes into those formatting options. Note that when using non-ANSI terminals, you may have to replacetput setafwithtput setf.- The output of
diffis piped intoless.-Rallows ANSI colors to be preserved.-Xpreventslessfrom clearing the screen upon exiting.-Fpreventslessfrom operating as a pager if the output fits within one screen. - 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:
git diffreports three lines of context surrounding each change. Unfortunately,diffseems 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). Thanksdiffprogrammers. 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@fullas the first parameter.- Because the lines of context are different from
git diff, the line numbers reported by this function will also vary from those reported bygit diff. - 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 diffdeals with this better, via its lines of context. You could try passing different options todiffto better deal with whitespace, if you prefer.
回答6:
GNU diff has a --color option since version 3.4 in late 2016 according to this answer on the Unix SE. That alongside -u should be enough to mimic the output of git diff:
diff -u --color=always file1 file2 | less -r
--color must be always when used in a pipe, auto will turn off color in pipes.
I've only tried this with Git Bash on Windows, where less -R would only color the first line of a hunk. less -r fixed it for me in that case.
回答7:
You are looking for colordiff:
sudo apt-get install colordiff
回答8:
Place this in your .bashrc or .zshrc :
diff() { git diff --no-index "$1" "$2" | colordiff; }
requirments : git and colordiff should be installed before-hand.
usage : diff file1 file2
example : for $diff .tmux.conf .zshrc.pre-oh-my-zsh
回答9:
The other option is to do it from outside the repository so git knows to diff between files. eg. a shell function something like:
gdiff() {
(
dir=`pwd`
cd ./$(git rev-parse --show-cdup)/..
git diff $dir/$1 $dir/$2
)
}
回答10:
Use colordiff:
Installation:
sudo apt-get install colordiff
Usage:
colordiff -u file_one file_two
Gives exactly same difference as shown by git diff.
回答11:
If you don't have colordiff or git diff, you can get color by vim.
cdiff() { diff -u $@ | vim -R -; }
or simply
cdiff() { diff -u $@ | view -; }
回答12:
I think the config setting :
[color]
ui = true
combined with "diff" command's --relative=<path> option would do what you wanted. Did you try ?
来源:https://stackoverflow.com/questions/4857310/how-to-get-diff-working-like-git-diff