Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit git-diff to one directory only, without its children?

Tags:

git

git-diff

I'm using git diff --name-status to track which files have been modified/added/removed/renamed/copied between two commits, and it works great.

Now suppose I move file file1 to newdir/file1, commit, and then run git diff, I get this:

$ git diff --name-status -C HEAD~1 HEAD
R100    file1    newdir/file1

Is there a way to ask git to limit itself to the list of changes inside a given directory but not its children? I'd like to know the exact changes both for the root directory and for the newdirdirectory, separately. For newdir, it's easy:

$ git diff --name-status -C HEAD~1 HEAD -- newdir
A       newdir/file1

… but how can I obtain the "complementary" diff info in the root directory? I.e., this output:

$ git diff ???
D       file1

Note that I want to keep the -C option to detect renames and copies inside the same directory.

like image 927
Jean-Philippe Pellet Avatar asked May 25 '11 11:05

Jean-Philippe Pellet


1 Answers

Pass it through to grep:

git diff --name-status HEAD..HEAD~1 . | grep ^[^/]*$

This will filter out anything that contains a sub directory, if you need to get something from a deeper do this:

git diff --name-status HEAD..HEAD~1 <path> | grep ^<path>[^/]*$

You could easily alias this if you wanted to.

like image 161
Chris Nicola Avatar answered Oct 11 '22 18:10

Chris Nicola