Git changes my file permissions upon checkout

后端 未结 5 822
误落风尘
误落风尘 2020-12-02 22:51

Our workflow is develop on a local machine, commit the changes to a central repository, then check out the branch of that repository that we need.

The problem is tha

相关标签:
5条回答
  • 2020-12-02 23:00

    Git does not change file permissions or ownership. It's just that it (mostly) doesn't store it either, it doesn't exist in your repo, so they get changed to whatever your user has. Just like with any file creation.

    Git supports two permission sets: executable bit on and executable bit off. Nothing else. Ownership information is not stored at all.

    See this thread - "If you want specific permissions, you'll need to do it manually."

    There are some solutions suggested: you can use a separate tool to do it for you, use a proper combination of user account and umask to set them properly by default or write a git hook yourself to do it. A hook would've to be installed on the user doing the checkout.

    Like @ikke said in the comments, Git is not really a deployment tool and should not be used as such. It is a version control system for source code.

    0 讨论(0)
  • 2020-12-02 23:07

    The easiest solution is to just run git as user_a.

    0 讨论(0)
  • 2020-12-02 23:11

    For me, the best solution was creation of a shell script that fixes the permissions. For example:

    .git/hooks/post-checkout:
    
    #!/bin/sh
    chmod +x  tools/*
    

    Btw, checkout is not the only case when git does mess with permissions, it's also when you pull. I handle that with .git/hooks/post-merge hook.

    Ideally, you can create a shell script that fixes permissions somewhere in your repo (e.g. tools/fixpermissions.sh), and call it in both hooks. Don't forget to change the permissions for that file manually ;)

    #!/bin/sh
    chmod a+x tools/fixpermissions.sh
    tools/fixpermissions.sh
    
    0 讨论(0)
  • 2020-12-02 23:14

    I often run a

    git checkout -f file.xml
    

    ... on a versioned file.xml with world-write permissions, as I continually modify it and want to return it to the normal state.

    But that resets the permissions. A slightly longer version:

    git show HEAD:./file.xml > ./file.xml
    

    Just resets the content.

    0 讨论(0)
  • 2020-12-02 23:16

    Alternatively you can set a default permission for the folder in question like so: https://unix.stackexchange.com/questions/1314/how-to-set-default-file-permissions-for-all-folders-files-in-a-directory

    0 讨论(0)
提交回复
热议问题