276

Just turned an some.sh file into an executable (chmod 755 ...), the permissions were updated but not the content. Is there a way to commit the file into git, so that the executable bit will be restored/set on clone / checkout / pull ?

Update: how can I track that the new permissions were submitted to github?

Cœur
  • 37,241
  • 25
  • 195
  • 267
BreakPhreak
  • 10,940
  • 26
  • 72
  • 108

3 Answers3

333

@fooMonster article worked for me

# git ls-tree HEAD
100644 blob 55c0287d4ef21f15b97eb1f107451b88b479bffe    script.sh

As you can see the file has 644 permission (ignoring the 100). We would like to change it to 755:

# git update-index --chmod=+x script.sh

commit the changes

# git commit -m "Changing file permissions"
[master 77b171e] Changing file permissions
0 files changed, 0 insertions(+), 0 deletions(-)
mode change 100644 => 100755 script.sh
ewwink
  • 18,382
  • 2
  • 44
  • 54
  • 16
    It should be noted that you actually have to use '-x/+x'. You can not set any other permissions or a bitmask. – Devolus Sep 21 '18 at 11:50
  • 2
    note using `git commit -a` didn't do anything for me, however setting message on the command line did. Bit of a quirk – JonnyRaa Dec 24 '18 at 11:39
  • 1
    The command order should be : `# git update-index --chmod=+x script.sh` `# git ls-tree HEAD` `# git commit -m "Changing file permissions"` `# git push` – SimonDepelchin Sep 09 '19 at 07:18
254

By default, git will update execute file permissions if you change them. It will not change or track any other permissions.

If you don't see any changes when modifying execute permission, you probably have a configuration in git which ignore file mode.

Look into your project, in the .git folder for the config file and you should see something like this:

[core]
    filemode = false

You can either change it to true in your favorite text editor, or run:

git config core.filemode true

Then, you should be able to commit normally your files. It will only commit the permission changes.

Dan Anderson
  • 2,265
  • 1
  • 9
  • 20
Vincent B.
  • 4,038
  • 1
  • 23
  • 18
  • thank you! how can I track that the permission changes were submitted to `github`? – BreakPhreak May 09 '12 at 13:01
  • 2
    You can see it when you view a file (for example, on [Rails gitignore file](https://github.com/rails/rails/blob/master/.gitignore), you will find 100644 as the file permission) – Vincent B. May 09 '12 at 13:38
  • 11
    Making permissions changes on Windows with git (actually changing the file permissions and committing): http://blog.lesc.se/2011/11/how-to-change-file-premissions-in-git.html – fooMonster May 17 '13 at 19:08
  • 52
    This answer is wrong! Git only tracks if a file is executable or not. It does not track other file permission like writable or readable. Read https://stackoverflow.com/a/11231682/2311074 for more. – Adam Jul 11 '17 at 08:22
  • For me webstorm did not catch the change , but in `git status`, I see changes.. – Townsheriff Apr 17 '20 at 11:25
  • Is there a way to do this globally? `git config --global` didn't seem to work. – Ariel May 17 '23 at 22:27
0

As Adam wrote: Git does not track the file mode in its entirety. https://git-scm.com/docs/git-config#Documentation/git-config.txt-corefileMode

stefan
  • 674
  • 1
  • 5
  • 13