小能豆

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

javascript

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?


阅读 225

收藏
2023-12-26

共1个答案

小能豆

Yes, you can make Git ignore changes to file permissions using the git update-index command. You can set the --chmod option to tell Git to ignore changes to the executable bit.

Here is how you can do it:

# Run this command for each file or directory where you want to ignore mode changes
git update-index --chmod=-x path/to/file_or_directory

This command tells Git to ignore changes to the executable bit (-x) for the specified file or directory. If you want to apply this to a directory and all its contents recursively, you can use the -r option:

git update-index --chmod=-x -r path/to/directory

Once you’ve marked the files or directories to ignore mode changes, Git won’t consider changes to their executable bit as modifications.

If you’ve already committed the changes to the repository, you might want to follow up with a commit to make sure the changes to file permissions are ignored in the repository’s history:

git commit -m "Ignore changes to file permissions"

Keep in mind that this approach makes Git ignore changes to the executable bit for the specified files or directories. If there are other changes to these files, those changes will still be detected by Git.

2023-12-26