Automatically apply “git update-index --chmod=+x” to executable files

后端 未结 6 729
Happy的楠姐
Happy的楠姐 2020-11-29 19:53

I frequently add bash scripts to my git repository, and the scripts have executable permissions in the linux filesystem prior to the git add. But after pushing

6条回答
  •  清歌不尽
    2020-11-29 20:24

    There are several ways to do that.

    1. Git aliases
    2. Bash aliases
    3. Or even combine bash and git aliases
    1. Git aliases

      You can always use bash within your git alias.

      • Open your git config:

        vim ~/.gitconfig

      • Add an aliases section to it (if one does not exist):

        [alias]
            addscr = !sh -c 'if [[ ${0: -3} == ".sh" ]]; then git update-index --chmod=+x $0; git add $0'
        
    2. Bash aliases

      • Edit your bash profile file:

        vim ~/.bashrc

      • Add this at the end of the file:

        function gitadd(){
            if [[ ${1: -3} == ".sh" ]]
                then git update-index --chmod=+x $1
            fi
            git add $1
         }
         alias gitadd='gitadd'
        
    3. Combine git and bash aliases

      • Edit your bash profile file:

        vim ~/.bashrc

      • Add this to the end of the file:

        function checkShellFile(){
            return ${1: -3} == ".sh"
        }
        alias gitadd='checkShellFile ? git addsrcipt "$1" : && git add "$1"'
        
      • Edit your git config file:

        vim ~/.gitconfig

      • Add an aliases section to it (if one does not exist):

        [alias]
            addscript = !sh -c 'git update-index --chmod=+x $0 && git add $0'
        

    None of the above has been tested

提交回复
热议问题