Robot Rocker
Robot Rocker

Reputation: 474

Can git diff cycle through changed files via command line?

Like everyone I often have multiple files with changes and it would be nice to be able to call a command that will diff each file individually and cycle through the changes, is that possible or will I need to write a script for this?

Thank you

Edit: For clarification the type of flow I'm looking for is I do "git diff cycle-files" and it brings up the first file with changes, I look over those changes then I hit N or something and it takes me to the next file, rinse repeat until I've looked at all files with changes individually.

Upvotes: 2

Views: 1996

Answers (4)

Joe Atzberger
Joe Atzberger

Reputation: 3306

To cycle through each changed file with diffed blocks, selecting which parts to include in the next commit, use:

git add -p .

That is "partial add". If you answer "no" to each block, it is equivalent to the git diff you are looking for.

Upvotes: 2

mockinterface
mockinterface

Reputation: 14860

If you have GNU xargs at your disposal you can:

git diff --name-only | xargs -I{} sh -c 'git diff {} | less'

to pipe the individual diffs into less.

Upvotes: 2

asm
asm

Reputation: 8898

This seems to do what you want. The 3 way output is pretty ugly. You'll need diff3 from diffutils. Then put the following in a script named dif (I use zsh you may use something else).

#!/bin/sh 

diff3 $1 $BASE $2

Then run this with git difftool -x ./dif. This will run diff3 on each changed file which spits out a change summary. You'll then have the option to step to the next file. You can tweak the diff3 parameters but I haven't found anything that looks nice.

We can get a much nicer two column diff similar to what you see in a graphical diff tool with this script

#!/usr/bin/env zsh

diff -y --suppress-common-lines $1 $2 | less

Upvotes: 0

TimWolla
TimWolla

Reputation: 32701

I don't know what you mean by cycle through, but git diff will show every changed file (in the specified folder):

[timwolla@/tmp/test master]git diff
diff --git a/a b/a
index f70f10e..0653421 100644
--- a/a
+++ b/a
@@ -1 +1,2 @@
 A
+a
diff --git a/b b/b
index 223b783..167f258 100644
--- a/b
+++ b/b
@@ -1 +1,2 @@
 B
+b

If you really need to call a command on every file you can use git diff --numstat in combination with awk to get a list of changed files:

[timwolla@/tmp/test master]git diff --numstat |awk '{print $3}'
a
b

If you pipe that output through xargs you can call the command on every file:

[timwolla@/tmp/test master]git diff --numstat |awk '{print $3}' |xargs cat
A
a
B
b

Upvotes: 0

Related Questions