How do I make Git ignore file mode (chmod) changes?
Asked Answered
D

12

2872

I have a project in which I have to change the mode of files with chmod to 777 while developing, but which should not change in the main repo.

Git picks up on chmod -R 777 . and marks all files as changed. Is there a way to make Git ignore mode changes that have been made to files?

Dacoit answered 16/10, 2009 at 21:43 Comment(2)
This is helpful when working with git on Windows + Bash on Ubuntu on WindowsPeptide
For anyone who just wants to ignore permission changes for a specific invocation of git diff, and who therefore does not want to alter their Git configuration files: you can use git diff -G. per Zed's answer here.Snowdrop
A
4777

Try:

git config core.fileMode false

From git-config(1):

core.fileMode
    Tells Git if the executable bit of files in the working tree
    is to be honored.

    Some filesystems lose the executable bit when a file that is
    marked as executable is checked out, or checks out a
    non-executable file with executable bit on. git-clone(1)
    or git-init(1) probe the filesystem to see if it handles the 
    executable bit correctly and this variable is automatically
    set as necessary.

    A repository, however, may be on a filesystem that handles
    the filemode correctly, and this variable is set to true when
    created, but later may be made accessible from another
    environment that loses the filemode (e.g. exporting ext4
    via CIFS mount, visiting a Cygwin created repository with Git
    for Windows or Eclipse). In such a case it may be necessary
    to set this variable to false. See git-update-index(1).

    The default is true (when core.filemode is not specified
    in the config file).

The -c flag can be used to set this option for one-off commands:

git -c core.fileMode=false diff

Typing the -c core.fileMode=false can be bothersome and so you can set this flag for all git repos or just for one git repo:

# this will set your the flag for your user for all git repos (modifies `$HOME/.gitconfig`)
# WARNING: this will be override by local config, fileMode value is automatically selected with latest version of git.
# This mean that if git detect your current filesystem is compatible it will set local core.fileMode to true when you clone or init a repository.
# Tool like cygwin emulation will be detected as compatible and so your local setting WILL BE SET to true no matter what you set in global setting.
git config --global core.fileMode false

# this will set the flag for one git repo (modifies `$current_git_repo/.git/config`)
git config core.fileMode false

Additionally, git clone and git init explicitly set core.fileMode to true in the repo config as discussed in Git global core.fileMode false overridden locally on clone

Warning

core.fileMode is not the best practice and should be used carefully. This setting only covers the executable bit of mode and never the read/write bits. In many cases you think you need this setting because you did something like chmod -R 777, making all your files executable. But in most projects most files don't need and should not be executable for security reasons.

The proper way to solve this kind of situation is to handle folder and file permission separately, with something like:

find . -type d -exec chmod a+rwx {} \; # Make folders traversable and read/write
find . -type f -exec chmod a+rw {} \;  # Make files read/write

If you do that, you'll never need to use core.fileMode, except in very rare environment.

Auroraauroral answered 16/10, 2009 at 21:53 Comment(40)
If you do git config --global core.filemode false you'll only need to do this once for all repos.Deserving
this didn't work for me until I've fixed the case it should be fileMode instead of filemodeBernardinabernardine
@tishma: Git configuration section and variable names are case insensitive according to the documentation, see the CONFIGURATION FILE section, so if the above didn't work for you then it was for a different reason.Auroraauroral
If you get into the situation where you have to change file permission and commit the in git from a windows machine: blog.lesc.se/2011/11/how-to-change-file-premissions-in-git.htmlHeyes
This is not working for me :(, however solution given by @Sinan Eldom worked for me. But that is per project basis. What is the global solution to it.Hub
This is all nice, but which config file does this go to?Azalea
@donquixote: The git config command writes the setting to the correct config file (.git/config for just the current repository, or ~/.gitconfig if used with --global).Auroraauroral
Thanks. Maybe would be useful to add that to the answer for completeness?Azalea
@GregHewgill: Does this ignore mode changes on commit? It seems as though samba sets files to +x when saving from windows. Will setting fileMode to false do an update-index if I'm committing it anyway? Or will it just commit the text changes but not the chmod changes? Thanks!Googins
If it is not working try this: git filter-branch -f --tree-filter 'find * -type f | xargs chmod 644 ' -- --allSmetana
I had to git init for this config to take effect.Conquer
After recovering from a corrupted git repository my files had the wrong permissions. It was better to actually re-set them instead of making git ignore them: find . -type d | xargs chmod 0755 find . -type f | xargs chmod 0644 (#14892789)Feer
filemode or fileMode exactly?Helmut
@zx1986: It doesn't matter. From git config: "The variable names are case-insensitive, ..."Auroraauroral
@greg even after doing a global edit, it will not work if the local config already had filemode=True. The local value will override the global setting... i had to go to every repo in my machine and change the local config value of filemode to false for each of the local reposFlynt
@greg i tried and failed with your suggestion... Hence, i reached you out.Flynt
@syedrakib Oh wow, a 2012 flashback... It was the right Greg after all.Deserving
@greg yeah... Posts on the internet don't have an expiry dateFlynt
@GregHewgill you should have created a secondary account and upvoted your post to thank yourself.Unapproachable
I tryed git config core.fileMode false and worked for me, thanks for sharing. For those interested, it seems that this setting is not inherited and should be reapplied if the repo is included as submodule.Halford
Whenever I initialize a new repository (git init), the setting is back to "true" for the new repo.. (after doing git config --global core.filemode false)Azalea
To get the file/directory permissions to work on OSX and ubuntu (I was getting an find: unknown predicate '-t' error), I had to change it to this: find . -type d -exec chmod 755 {} \; and find . -type f -exec chmod 644 {} \;Trembly
PLEASE add a second warning to this answer, stating that the global setting won't be applied to existing repos! For each repo you need to run the local command (or it seems "git init" has the same effect). This will affect pretty much everyone and can be extremely confusing (esp. when you first interact with a second repo and have no idea why the global setting isn't working when it worked on the first repo, where you ran both the global and local versions of the setting change.)Passageway
@GregHewgill If I use git config core.sharedRepository true then would I need to change fileMode?Bustup
with chmod you can use +/-X tu change the executable bit for folders only, for example, chmod +X somefile is a noop, while chmod +X somedir will set the execute bit on that dirPas
The description has been changed and reflects the problem more correctly now core.fileMode ... Some filesystems lose the executable bit when a file that is marked as executable is checked out, or checks out an non-executable file with executable bit on. git-clone[1] or git-init[1] probe the filesystem to see if it handles the executable bit correctly and this variable is automatically set as necessary.Dacha
FAT is not "broken" in this sense because obviously it's not designed for Unix, so can't store those permissions. The same problem can occur in NTFS, exFAT or when mounting via NFS... A repository, however, may be on a filesystem that handles the filemode correctly, and this variable is set to true when created, but later may be made accessible from another environment that loses the filemode (e.g. exporting ext4 via CIFS mount, visiting a Cygwin created repository with Git for Windows or Eclipse). In such a case it may be necessary to set this variable to false. See git-update-index[1].Dacha
--global will only take effect with newly cloned repositories.Quadratic
very rare environment = vagrant on ubuntu with ubuntu? :(Feretory
I don't know whether the behaviour has changed, but core.fileMode is now set explicitly in the per-repo git config on clone. So git config --global core.filemode false as suggested in the top comment will have no effect, as it's overridden by the local setting.Yann
@Yann Like all other git settings, the local setting only overrides the global if it is present. If not present, the global setting is used. See man git-config.Auroraauroral
@GregHewgill sure, I just wanted to highlight that unlike most other settings (such as autocrlf), the git binary will set core.filemode locally for each repo on clone: git-clone or git-init probe the filesystem to see if it handles the executable bit correctly and this variable is automatically set as necessary.Yann
I may have came back to this line around 9876 times, easier to find this than to remember :)Spain
In our production environment for a client, the index.php file needs to be 655. IN dev and staging, 755. So I set the index.php to 655, deployed to prod and dev. Set file perms to false on dev, and there changed the permissions on dev to 755. One reconciled git repo with different permissions!Remanent
Note that git init and git clone will ignore the --global setting and explicitly set core.filemode to true. See #30392818 for workaround ideas.Nanon
I needed to add sudo for it to work. So the full command was: sudo git config core.fileMode falseUnyielding
It seems that Changes of the global AND system (--system) setting won't be applied to existing repositories.Neuromuscular
You've visited this page many times. Last visit: 8/5/21 Indeed.Oxen
This is the 50th time I came here to remember the command.Conquistador
I needed this when I had mounted a remote directory with sshfs and ran git pull. Got the "fatal detected dubious ownership in repository"; not sure but suspecting it was because the files and folders on the remote machine mismatched user and group uids (and none of those uids existed on my local machine).Italic
R
327

undo mode change in working tree:

git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -d'\n' chmod +x
git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -d'\n' chmod -x

Or in mingw-git

git diff --summary | grep  'mode change 100755 => 100644' | cut -d' ' -f7- | xargs -e'\n' chmod +x
git diff --summary | grep  'mode change 100644 => 100755' | cut -d' ' -f7- | xargs -e'\n' chmod -x

Or in BSD/macOS

git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x
git diff --summary | grep --color 'mode change 100755 => 100644' | cut -d' ' -f7- | tr '\n' '\0' | xargs -0 chmod -x
Renferd answered 18/1, 2010 at 2:20 Comment(12)
On OS X Lion, omit the -d'\n' part from xargs as this is an illegal argument (and not needed).Toinette
You can ignore any errors about "chmod: missing operand after `+x'"Exciseman
Pascal: the -d "\n" is necessary to handle filenames with embedded spaces; normal xargs splits on whitespace.Emission
is this up to date? I get 'chmod: too few arguments' in mingwKrishna
@Toinette @Emission The -d specifies the delimiter to be newline instead of any whitespace. BSD xargs doesn't have that option, but instead you can pipe the output through tr '\n' '\0' and then use the -0 arg to xargs to use NUL as the delimiter.Valladolid
For me it gave chmod: missing operand after â+xâShortwave
Works for me for 'proper' filenames, but fails when there are spaces in the filenames (ipython notebooks). How can I catch that?Granjon
Cool, the tr thing worked! Here's the full command for OSX: git diff --summary | grep --color 'mode change 100644 => 100755' | cut -d' ' -f7-|tr '\n' '\0'|xargs -0 chmod -xGranjon
How does grep --color help?Cerography
@K.-MichaelAye also works on ubuntu zsh.Neuromuscular
What is in cut -d' ' -f7- -f7-? I know the field separator -f separating nth field, but why the second dash?Neuromuscular
returns chmod: missing operand after ‘+x’Clambake
E
165

If you want to set this option for all of your repos, use the --global option.

git config --global core.filemode false

If this does not work you are probably using a newer version of git so try the --add option.

git config --add --global core.filemode false

If you run it without the --global option and your working directory is not a repo, you'll get

error: could not lock config file .git/config: No such file or directory
Eddra answered 21/4, 2012 at 12:0 Comment(3)
Looks like later GIT uses --add, as in git config --add --global core.filemode falsePollen
If the repo's local config already has filemode=true then changing the global config won't help as the local config will override the global config. Will have to change local config of each repo of the machine onceFlynt
PLEASE: Update this answer with syedrakib's warning! Everything felt insane before I found it, and made perfect sense after.Passageway
P
120

If

git config --global core.filemode false

does not work for you, do it manually:

cd into yourLovelyProject folder

cd into .git folder:

cd .git

edit the config file:

nano config

change true to false

[core]
        repositoryformatversion = 0
        filemode = true

->

[core]
        repositoryformatversion = 0
        filemode = false

save, exit, go to upper folder:

cd ..

reinit the git

git init

you are done!

Phenomena answered 6/8, 2013 at 13:10 Comment(5)
Instead of editing .git/config, a simple git config core.fileMode false in the root of your project is enough. If you edit the config file, you're better of removing the directive entirely, so that the global one is picked up.Gernhard
-1 if git config --global doesn't work it means you don't have the permissions to do it at the system level, removing global option does exactly the same thing as manually editing .git/configGlazunov
@Glazunov incorrect - the answered provided a workaround by putting the option directly in the project, making it project-specific. This will not work with other git projects that you make/checkout in the future, but does work for the project you're working on. (let's make sure we disambiguate ~/.gitconfig, and ~/project/.git/config)Euplastic
Once this has run git init should we set filemode back to true?Googins
git init returns filemode back to TRUE!Ischia
H
58

Adding to Greg Hewgill answer (of using core.fileMode config variable):

You can use --chmod=(-|+)x option of git update-index (low-level version of "git add") to change execute permissions in the index, from where it would be picked up if you use "git commit" (and not "git commit -a").

Haggadist answered 16/10, 2009 at 22:3 Comment(3)
This should have been edited into Greg Hewgill's answer rather than added as a separate answer, thus creating one supreme answer with a single unambiguous representation.Deserving
@Greg: One needs to have enough points to edit not own answer; I think I didn't have enough for editing permissions at that time.Aiaia
@Jakub I think you have enough reputation now :) What would this command look like for an example file?Bonzer
C
51

You can configure it globally:

git config --global core.filemode false

If the above doesn't work for you, the reason might be your local configuration overrides the global configuration.

Remove your local configuration to make the global configuration take effect:

git config --unset core.filemode

Alternatively, you could change your local configuration to the right value:

git config core.filemode false

Clinch answered 21/1, 2015 at 9:12 Comment(2)
If the main answer doesn't help you - try this one. If you want to check your local config without modifying it, check git config -l (list current config - both local and global)Sher
Removing local configuration is why global didn't work for me. Thanks!Everetteverette
C
26

If you have used chmod command already then check the difference of file, It shows previous file mode and current file mode such as:

new mode : 755

old mode : 644

set old mode of all files using below command

sudo chmod 644 .

now set core.fileMode to false in config file either using command or manually.

git config core.fileMode false

then apply chmod command to change the permissions of all files such as

sudo chmod 755 .

and again set core.fileMode to true.

git config core.fileMode true

For best practises don't Keep core.fileMode false always.

Carnes answered 13/10, 2015 at 7:34 Comment(7)
Are you saying that an entire project (in development, staging, and production) should be 755?Ditch
@Ditch Feb: No. change the mode of necessary files only.Carnes
For best practises don't Keep core.fileMode false always what do you mean, you should explain that.Goulette
For best practises don't Keep core.fileMode false always. Some filesystems (FAT for example) don't support file permissions, so the OS will report a default value (766 on my system anyway). In this case, core.filemode is absolutely necessary in the local config, unless you want to bloat the commit history with unnecessary and unintentional permission changesThose
Also, why do you bother changing the perms back at all? If you set core.filemode=false then git will ignore execute bit changes, no need to change local permissions. Unless you've already added permission changes to the index, in which case you're missing the step where you would need to git add after you turn off core.filemode.Those
I was wondering what happens if I have this situation (with modified permissions) and then I use git config core.fileMode false. while after setting this some files will not be seen as modified (the ones that will have only permissions changes) but others that had other changes beyond permissions e.g valid code modifications etc will still be seen as modified due to code changes. If I commit these files will the mod change also be committed as part of it?Feck
@RajaEhtesham No, though you probably already figured that out. The only things that are commited are what you stage for commit (i.e. the files that you git add, and the changes you see when you rungit status)Those
B
22

By definining the following alias (in ~/.gitconfig) you can easily temporarily disable the fileMode per git command:

[alias]
nfm = "!f(){ git -c core.fileMode=false $@; };f"

When this alias is prefixed to the git command, the file mode changes won't show up with commands that would otherwise show them. For example:

git nfm status
Bjork answered 24/7, 2015 at 15:1 Comment(1)
Why not just a pre-commit hook instead of a forgettable parameter?Stome
H
14

If you want to set filemode to false in config files recursively (including submodules) : find -name config | xargs sed -i -e 's/filemode = true/filemode = false/'

Haste answered 16/7, 2013 at 12:23 Comment(1)
This won't work if that line is not in the config file. If you want to change it for submodules, try this: git submodule foreach git config core.fileMode falseIdeogram
I
6

Simple solution:

Hit this Simple command in project Folder(it won't remove your original changes) ...it will only remove changes that had been done while you changed project folder permission

command is below:

git config core.fileMode false

Why this all unnecessary file get modified: because you have changed the project folder permissions with commend sudo chmod -R 777 ./yourProjectFolder

when will you check changes what not you did? you found like below while using git diff filename

old mode 100644
new mode 100755
Institutional answered 19/3, 2018 at 13:13 Comment(1)
This answer duplicates Greg Hewgill's answer from 10 years before.Elissaelita
H
3

This works for me:

find . -type f -exec chmod a-x {} \;

or reverse, depending on your operating system

find . -type f -exec chmod a+x {} \;
Hellenist answered 30/4, 2018 at 15:6 Comment(2)
This would change the permissions of the file, but not make git ignore the file permissions of the file.Manifestation
Well, you are right, that does not resolve git ignore thing.Hellenist
U
1

You don't need to change the config file.

Just run:

git diff -G.

Note that the trailing dot is not end of sentence, but regex expression that matches everything, and must be included.

Unseasoned answered 28/5, 2022 at 20:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.