24 November 2011

How to change file premissions in Git on Windows

Git manages file permissions for each file in the repository. It may be appropriate to have the executable bit set for shell/bash files to make them easier to execute in Linux systems.

Windows own file permissions does not map to the Git file permissions so it may be a bit hard to change file permissions when you are on Windows. This is how you do it:

Lets assume the file script.sh needs to have the executable bit set. Use the command git ls-tree to inspect the file permissions:
C:\views\myproject>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:
C:\views\myproject>git update-index --chmod=+x script.sh

C:\views\myproject>git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       modified:   script.sh
#


The file is now staged. Note that the file contents is not changed, only the meta data. We must commit the file so save the change:
C:\views\myproject>git commit -m "Changing file permissions"
[master 77b171e] Changing file permissions
 0 files changed, 0 insertions(+), 0 deletions(-)
 mode change 100644 => 100755 script.sh


Running git ls-tree again to see the change:
C:\views\myproject>git ls-tree HEAD
100755 blob 55c0287d4ef21f15b97eb1f107451b88b479bffe    script.sh

7 comments:

  1. Thank you, this was just what I needed. :)

    ReplyDelete
  2. Thanks a lot for this solution !!

    ReplyDelete
  3. but how to do for folder and it's complete file and folder

    ReplyDelete
  4. I don't think it can be done for folders?
    At least I am pretty sure Git doesn't actually track folders, only the files in them. I've found it impossible to add an empty folder to Git for example.

    ReplyDelete
  5. Cant we make file executable from git UI?

    ReplyDelete