Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

ignore

chmod

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?

like image 774
Marcus Westin Avatar asked Oct 16 '09 21:10

Marcus Westin


People also ask

How do I get permission to ignore a file in git?

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.

How do I ignore files while pushing to git repository?

You can create a . gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the . gitignore file in to your repository.


1 Answers

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`) 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.

like image 180
Greg Hewgill Avatar answered Sep 20 '22 08:09

Greg Hewgill