TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Diff So Fancy: make Git diffs look good

270 pointsby aramover 9 years ago

29 comments

jimrandomhover 9 years ago
A personal pet peeve of mine when reading diffs, is when a file has some functions and you insert one and instead of looking like this:<p><pre><code> int someOldFunction() { &#x2F;&#x2F; Function body } + +int newFunction() +{ + &#x2F;&#x2F; New function body +} </code></pre> It looks like this:<p><pre><code> int someOldFunction() { &#x2F;&#x2F; Function body +} + +int newFunction() +{ + &#x2F;&#x2F; New function body } </code></pre> It&#x27;s a small thing, but given that these diffs are equivalent, the one that balances the curly braces within added blocks should be favored. But diff utilities seem to get this pretty consistently wrong.
评论 #11067961 未加载
评论 #11067951 未加载
评论 #11071410 未加载
评论 #11070896 未加载
评论 #11071271 未加载
评论 #11068313 未加载
paulirishover 9 years ago
[I&#x27;m the author of diff-so-fancy, Steve helped with shipping it as a standalone script]<p>NPM?!? :)<p>A lot of people below are asking why a bash script (that depends on a perl script) is being recommended to install via NPM? The short reason is that NPM is the most straightforward way to get a script installed as a global binary in a cross-platform manner. This approach has worked quite well with `git-open`[0]. Asking all users to deal with the PATH is not my ideal.<p>In addition, I wanted a reasonable upgrade path, in case there are neccessary bugfixes. It&#x27;s not a great experience if users identify bugs but the fix means they manually find it&#x2F;download&#x2F;PATH-ify each time. :&#x2F;<p>That said, I&#x27;ll add some Manual Install instructions to the readme so it&#x27;s clear how to do this on your own. :) ( Edit: Here they are… <a href="https:&#x2F;&#x2F;github.com&#x2F;stevemao&#x2F;diff-so-fancy&#x2F;blob&#x2F;master&#x2F;readme.md#manual-install" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;stevemao&#x2F;diff-so-fancy&#x2F;blob&#x2F;master&#x2F;readme...</a> )<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;paulirish&#x2F;git-open" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;paulirish&#x2F;git-open</a>
评论 #11068598 未加载
评论 #11068784 未加载
评论 #11068934 未加载
评论 #11068921 未加载
评论 #11108222 未加载
评论 #11069651 未加载
评论 #11068880 未加载
评论 #11069215 未加载
phinnaeusover 9 years ago
It looks like the diff-so-fancy script is bash, and the diff-highlight script is perl. Why is it set up in npm? WTF?
评论 #11068542 未加载
评论 #11068434 未加载
评论 #11067855 未加载
squiguy7over 9 years ago
Why do I need NPM to install this? I guess I will just have to manually get and link them...
评论 #11067768 未加载
chaitanyaover 9 years ago
Might be better to link to the source directly: <a href="https:&#x2F;&#x2F;github.com&#x2F;paulirish&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bin&#x2F;diff-so-fancy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;paulirish&#x2F;dotfiles&#x2F;blob&#x2F;master&#x2F;bin&#x2F;diff-s...</a>
FiloSottileover 9 years ago
If you don&#x27;t want your diff so fancy (pun intended, and I&#x27;m sorry) but you still want the inline highlights, the script comes with git (<a href="https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;tree&#x2F;master&#x2F;contrib&#x2F;diff-highlight" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;tree&#x2F;master&#x2F;contrib&#x2F;diff-highligh...</a>):<p><pre><code> ln -sf &quot;$(brew --prefix)&#x2F;share&#x2F;git-core&#x2F;contrib&#x2F;diff-highlight&#x2F;diff-highlight&quot; ~&#x2F;bin&#x2F;diff-highlight </code></pre> and add to .gitconfig<p><pre><code> [pager] log = diff-highlight | less show = diff-highlight | less diff = diff-highlight | less</code></pre>
pierrebeaucampover 9 years ago
I know he gave credit in the README, but why does this ~30 line shell script need its own repo? Seems more like a cheap grab for Github Stars rather than to provide actual value.<p>Edit: Even the screenshot is from Paul...
评论 #11068032 未加载
评论 #11068039 未加载
评论 #11068965 未加载
评论 #11070476 未加载
finnnover 9 years ago
So, the whole npm thing seems weird to me, then it occurred to me that it could be for malicious purposes. Would it be possible to upload a separate package.json to npm that had eg a post-install script? I don&#x27;t know much about how npm works from the package publication side of things, but I assumed it was similar to pypi where the code in the git repo doesn&#x27;t have to be at all related to the code in the package
timtadhover 9 years ago
<p><pre><code> git diff --word-diff=color </code></pre> provides a really nice word diff with coloring similar to this project. Just setup an alias in your global gitconfig:<p><pre><code> [alias] cdiff = diff --word-diff=color</code></pre>
landr0idover 9 years ago
Looks good, but why does it use npm for installation?
评论 #11067756 未加载
评论 #11067883 未加载
izolateover 9 years ago
Remove contextual +&#x2F;- in favour of colour highlights? As a person with red-green colourblindness all I can say is: Lol. Nope.
评论 #11067991 未加载
评论 #11068029 未加载
mschuster91over 9 years ago
what I&#x27;d like to see is that a&#x2F;b in front of the filenames disappear. Getting rid of that would FINALLY allow me to double-click on the filename (which is configured to select the part between the spaces and copy it to the clipboard) and paste it instantly for the next command... or to be able to do git diff &gt; foo.patch and on another system do patch &lt; foo.patch without having to remember the correct -p value.
评论 #11070712 未加载
oabmover 9 years ago
What happened to this line from the second file in the screenshot?<p><pre><code> - var optionsGlassPane = new WebInspector.GlassPane(document); </code></pre> An important part of viewing diffs for me is seeing what the old code was.
makecheckover 9 years ago
I handle this in a way that is more agnostic to the type of revision control, and fully flexible in coloring (using the most powerful scheme available).<p>For example, I shouldn&#x27;t have to put up with basic colors if the terminal can do better.<p>Here is how it works; starting with:<p><pre><code> #!&#x2F;bin&#x2F;bash if [ -r &quot;.svn&quot; ] ; then exec svn diff ${1+&quot;$@&quot;} | my_colorize_diff else git diff ${1+&quot;$@&quot;} | my_colorize_diff fi </code></pre> ...where the &quot;my_colorize_diff&quot; script at the end of the pipe is as follows:<p><pre><code> #!&#x2F;usr&#x2F;bin&#x2F;env perl # by Kevin Grant (kmg@mac.com) my $term_program = (exists $ENV{&#x27;TERM_PROGRAM&#x27;} &amp;&amp; defined $ENV{&#x27;TERM_PROGRAM&#x27;}) ? $ENV{&#x27;TERM_PROGRAM&#x27;} : &#x27;&#x27;; my $term = (exists $ENV{&#x27;TERM&#x27;} &amp;&amp; defined $ENV{&#x27;TERM&#x27;}) ? $ENV{&#x27;TERM&#x27;} : &#x27;vt100&#x27;; my $is_xterm = ($term =~ &#x2F;xterm&#x2F;); my $is_24bit = ($term_program =~ &#x2F;MacTerm&#x2F;); print &quot;\033#3BEGIN DIFF\n&quot;; print &quot;\033#4BEGIN DIFF\n\033#5&quot;; while (&lt;&gt;) { if (&#x2F;^\+&#x2F; &amp;&amp; !&#x2F;^\+\+&#x2F;) { if ($is_24bit) { print &quot;\033[48:2:150:200:150m&quot;, &quot;\033[2K&quot;, &quot;\033[38:2::88:m&quot;, &quot;\033[1m&quot;; } elsif ($is_xterm) { print &quot;\033[48;5;149m&quot;, &quot;\033[2K&quot;, &quot;\033[38;5;235m&quot;, &quot;\033[1m&quot;; } else { print &quot;\033[42m&quot;, &quot;\033[2K&quot;, &quot;\033[30m&quot;, &quot;\033[1m&quot;; } } elsif (&#x2F;^\-&#x2F; &amp;&amp; !&#x2F;^\-\-&#x2F;) { if ($is_24bit) { print &quot;\033[48:2:244:150:150m&quot;, &quot;\033[2K&quot;, &quot;\033[38:2:144:0::m&quot;; } elsif ($is_xterm) { print &quot;\033[48;5;52m&quot;, &quot;\033[2K&quot;, &quot;\033[38;5;124m&quot;; } else { print &quot;\033[41m&quot;, &quot;\033[2K&quot;, &quot;\033[37m&quot;; } } else { print &quot;\033[3m&quot;; } chomp; print; print &quot;\033[0m\n&quot;; } print &quot;\033#3END DIFF\n&quot;; print &quot;\033#4END DIFF\n\033#5&quot;;</code></pre>
评论 #11069009 未加载
jfountain2015over 9 years ago
Nice improvement to diff but I think I&#x27;ll still use `git difftool` with Diffmerge <a href="https:&#x2F;&#x2F;sourcegear.com&#x2F;diffmerge&#x2F;" rel="nofollow">https:&#x2F;&#x2F;sourcegear.com&#x2F;diffmerge&#x2F;</a>
hobarreraover 9 years ago
I didn&#x27;t quite like this, put it does reference diff-hightlight, which is part of git-contrib (so it may already be installed on your system, but just not in your $PATH!):<p><a href="https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;tree&#x2F;master&#x2F;contrib&#x2F;diff-highlight" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;tree&#x2F;master&#x2F;contrib&#x2F;diff-highligh...</a><p>For example, here&#x27;s a diff where it improved readability enormously:<p><a href="https:&#x2F;&#x2F;i.imgur.com&#x2F;8iQNaeu.png" rel="nofollow">https:&#x2F;&#x2F;i.imgur.com&#x2F;8iQNaeu.png</a>
评论 #11108211 未加载
felipesabinoover 9 years ago
I have an alias for the diff params bellow, which has basically the same visual result, without the need to install anything.<p><pre><code> $ git diff --color --color-words --abbrev</code></pre>
_ZeD_over 9 years ago
what are the advantages over colordiff[0]? (or a graphic differ like kdiff3[1])<p>[0] <a href="http:&#x2F;&#x2F;www.colordiff.org&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.colordiff.org&#x2F;</a> [1] <a href="http:&#x2F;&#x2F;kdiff3.sourceforge.net&#x2F;" rel="nofollow">http:&#x2F;&#x2F;kdiff3.sourceforge.net&#x2F;</a>
tremguyover 9 years ago
&gt; No pesky + or - at line-stars, making for easier copy-paste.<p>I wonder if easing copypasting is a good or bad thing..
mrinterwebover 9 years ago
Vim&#x27;s fugitive plugin also gives a similar split diff view. The command is &quot;Gdiff&quot;.
评论 #11068175 未加载
stevemaoover 9 years ago
We&#x27;ve grown and it&#x27;s out of control. diff-so-fancy moved to an org!!! <a href="https:&#x2F;&#x2F;github.com&#x2F;so-fancy&#x2F;diff-so-fancy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;so-fancy&#x2F;diff-so-fancy</a>
nikolayover 9 years ago
I find icdiff [0] (Improved Colored Diff) better.<p>[0]: <a href="https:&#x2F;&#x2F;github.com&#x2F;jeffkaufman" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jeffkaufman</a>
GreaterFoolover 9 years ago
Is there any language aware diff tool? I think I saw some commercial product for Java but other than that I haven&#x27;t seen any attempts to do that.
samuellover 9 years ago
When writing manuscripts in git, my favourite &quot;trick&quot; is git diff --word-diff (of course aliased to git wdiff).
module17over 9 years ago
It looks so good and real, I kept hitting `q` whilst looking at the image.
nqzeroover 9 years ago
the netbeans.team.diff tool is similar (showing the specific words that changed), allows interactive editing, and does a good job even with large insertions and deletions
AdmiralAsshatover 9 years ago
So what does this offer over simply using vimdiff?
merbover 9 years ago
this doesn&#x27;t look useful. the left one is just fine. for everything else you wouldn&#x27;t need a cli diff tool...
评论 #11068654 未加载
Animatsover 9 years ago
Hurts. Hurts bad. Tiny type in many primary colors on a black background. 1985 called, it wants its screen layout back.