5

I have a project in which there is a .sh file and it has to be in the executable mode once pulled by the others. Now, I already changed its permissions on my local machine. However I want it to be pushed/pulled as executable as well, so that the other users do not have to run chmod command all the time.

I have been informed about two possibile solutions: .gitattributes and git update-index --chmod=+x script.sh, however I am not sure what exactly should I follow, given my condition.

I've seen this post here and this answer there, and I am thinking which one would suit my case more. I want this process to be done automatically, not by the user everytime, added.

Any thoughts?

Community
  • 1
  • 1
Schütze
  • 1,044
  • 5
  • 28
  • 48

3 Answers3

3

Since you've tagged this question with , you can just check the file in. Now that you've changed it on your computer:

% chmod +x script.sh

Git will notice that the file has changed:

% git status
On branch old2
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   script.sh

And you can diff to see that the file is, in fact, now executable:

% git diff foo.sh
diff --git a/foo.sh b/foo.sh
old mode 100644
new mode 100755

Mode 100644 reflects a non-executable file, and mode 100755 reflects an executable file, similar to the Unix file permission bits. (Had you changed the file in addition to changing the permissions, you would also see the changes.)

When you check this in and your collaborators pull your changes, the file will be made executable for them, too.


Note that this does not work automatically on systems that do not have the notion of an execute bit (ie, Windows), and in that case, you will need to do something with update-index.

Note also that this relies on your system to have configured core.filemode being set correctly. This value should be set to true on all non-Windows systems.

% git config core.filemode
true

If, for some reason, that command returns false and you are on a non-Windows computer, run:

% git config core.filemode true

to re-enable it.

Community
  • 1
  • 1
Edward Thomson
  • 74,857
  • 14
  • 158
  • 187
  • Yes, all systems are UNIX-based systems, so yeah, I think Git does the magic all on its own. Thank you. – Schütze Jul 18 '16 at 12:13
2

You have to decide which is fit for you. Two methods those you given above they can acceptable and they almost the same.

You should know how to give working permissions to executable files. In Linux, true way to do it is "chmod". Also you can sue git hooks as well. For doing in normal method I preferred this:

git add file.sh #this could be py file or something
git update-index --chmod=+x file.sh #make it executable
git commit -m "here the commit" #commit it
git push #push it

So if you want to do it another way you should try this:

#!/usr/bin/bash
chmod +x file.sh

And you can run it. But before the run it, you should give working permissions to your script that you made for giving working permissions to other scripts :)

Or you can give the permissions dynamically:

su -c 'chmod +x file.sh'

In this way, you should give the working permission for one time and it runs.

0

Did you consider Git hooks?

Maybe this could be solution for you

.git/hooks/post-checkout:

#!/bin/sh
chmod +x script.sh

Here you can find more about Git hooks.

djm.im
  • 3,295
  • 4
  • 30
  • 45