Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add chmod permissions to file in Git?

Tags:

git

I would git commit a .sh file, but would like it to be executable when I checkout that same file in another server.

Is there a way to do so without manually chmod u+x that file in the servers that checkout the file?

like image 399
Henley Avatar asked Oct 11 '22 15:10

Henley


People also ask

How do I add permissions to Git?

Open Project settings>Repositories. To set the permissions for all Git repositories, choose Security. For example, here we choose (1) Project settings, (2) Repositories, and then (3) Security. Otherwise, to set permissions for a specific repository, choose (1) the repository and then choose (2) Security.

How do I add permission to run a file in github?

The solution is to use the Git update-index command to assign the execute permissions. This will assign execute permissions to the bash file. After that you can commit the changes to the repo.

How do you change permissions on a file in Git?

Make a Git repository file on a local Windows machine executable by changing the CHMOD value, which can be transferred to the repository following a push.


1 Answers

According to official documentation, you can set or remove the "executable" flag on any tracked file using update-index sub-command.

To set the flag, use following command:

git update-index --chmod=+x path/to/file

To remove it, use:

git update-index --chmod=-x path/to/file

Under the hood

While this looks like the regular unix files permission system, actually it is not. Git maintains a special "mode" for each file in its internal storage:

  • 100644 for regular files
  • 100755 for executable ones

You can visualize it using ls-file subcommand, with --stage option:

$ git ls-files --stage
100644 aee89ef43dc3b0ec6a7c6228f742377692b50484 0       .gitignore
100755 0ac339497485f7cc80d988561807906b2fd56172 0       my_executable_script.sh

By default, when you add a file to a repository, Git will try to honor its filesystem attributes and set the correct filemode accordingly. You can disable this by setting core.fileMode option to false:

git config core.fileMode false

Troubleshooting

If at some point the Git filemode is not set but the file has correct filesystem flag, try to remove mode and set it again:

git update-index --chmod=-x path/to/file
git update-index --chmod=+x path/to/file

Bonus

Starting with Git 2.9, you can stage a file AND set the flag in one command:

git add --chmod=+x path/to/file
like image 543
Antwane Avatar answered Oct 14 '22 05:10

Antwane