91

I inadvertedly change the permissions of my entire tree and commit that change along with other content changes.

I use something like :

tar -czf deploy.tar git diff --name-only v1 v2

to generate a tar with the modified files between two tags, the problem is that now because of the permissions change almost all my tree is listed as modified.

Is there a way that I could tell git diff to ignore those files which only has the permissions changed?

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
Cesar
  • 4,076
  • 8
  • 44
  • 68
  • 3
    possible duplicate of [How do I make git ignore mode changes (chmod)?](http://stackoverflow.com/questions/1580596/how-do-i-make-git-ignore-mode-changes-chmod) – Leonel Jul 04 '12 at 11:35

3 Answers3

177

This will tell git to ignore permissions:

git config core.filemode false
Daniel Stutzbach
  • 74,198
  • 17
  • 88
  • 77
  • 4
    Maybe this could prevent adding files with changed permissions to a commit, but in my case the files are committed, pushed and tagged, and the command you propose doesn't help me. I guess I'll have to make some kind of bash script to parse the output of git diff to remove the lines about mode changes, and feed the result to tar. Any ideas are more then welcome – Cesar Jul 20 '10 at 15:06
  • @Cesar: The command tells git to ignore permissions differences between the index and the working copy. Isn't that what you want? – Daniel Stutzbach Jul 20 '10 at 16:10
  • i'm new to git, but the thing is that the differences is between commits. When i do git diff tag1 tag2, it show all the files to which i changed the permissions besides the files with modified content, i just want to find a way to ignore all those 'mode change 664 => 775', I hope I made myself clear – Cesar Jul 20 '10 at 22:14
  • 3
    @Cesar: Oh! So you've already committed the changed permissions? Sorry, I had missed that part. If you haven't already pushed the changes to another repository or merged them to another branch, I'd suggest amending/rebaseing the commit to fix it. – Daniel Stutzbach Jul 21 '10 at 06:46
  • 15
    `git diff -G.` (see http://stackoverflow.com/questions/22388811/show-git-diff-ignoring-file-permission-changes) – Fabian Schmengler Dec 03 '15 at 11:16
  • To make this setting global, you could use the --global option: $ git config --global core.filemode false – dxvargas Jan 27 '16 at 11:09
  • 4
    The dot (`.`) at the end of `gif diff -G.`**matters** – rodrigobb Apr 07 '16 at 10:27
33

Use the -G<regex> option ("Look for differences whose patch text contains added/removed lines that match <regex>.") searching for any changes at all - i.e. .. Permissions-only changes don't match this, so they are ignored.

So: git diff -G.

pkamb
  • 33,281
  • 23
  • 160
  • 191
lazysoundsystem
  • 2,039
  • 23
  • 23
  • For some reason this solution is incompatible with `--ignore-space-at-eol`. If I do `git diff -G. revA..revB` then I suppress reporting of mode changes, as desired, but I do see line-ending changes. If I add `--ignore-space-at-eol` I suppress line-ending changes, as desired, but the mode changes come back. Why would that be? They don't seem fundamentally related, to me. I want to suppress both. – jez Aug 10 '20 at 15:43
  • This does not work with changes to binary files, i.e. any binary file change will not show in the diff, if you use -G. – Malcolm Boekhoff May 05 '22 at 02:53
  • Found out that the trick works if you also add "--text" – Malcolm Boekhoff May 05 '22 at 03:03
15

I had this problem after intentionally removing the execute permission from source code files (~26K files). Then, git diff says that all files have changed! The answer with core.filemode does not help me since that only affects diffs against your working dir, not diffs against 2 commits in the repo.

The answer was to use the (big scary) filter-branch command. In particular, all you need to type is:

git filter-branch -f --tree-filter 'find * -type f | xargs chmod 644' -- --all

from the root of your working dir. Of course, be sure to make a copy of your repo first, i.e.

cp -pr ~/repo.git ~/repo-orig.git

or similar, in case you need to re-try.

Enjoy!

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • 1
    Interesting (+1). I have included it in my answer at http://stackoverflow.com/a/12979453/6309 (since your own answer was deleted) – VonC May 03 '13 at 05:51