75

我经常将 bash 脚本添加到我的 Git 存储库中,并且这些脚本在git add. 但是在将添加的文件推送到远程存储库并拉入另一个位置后,这些文件显示为具有不可执行的权限。似乎有两种方法可以解决这个问题:

  1. chmod u+x $script 
    git commit -am "fixing the script permissions... again..."
    
  2. git update-index --chmod=+x $script
    

不是每次都修复权限,有没有办法让 Git 在 期间简单地查看脚本上的文件权限git add,识别“嘿,这是一个可执行文件!” 并直接将其添加到具有可执行权限的存储库中?

4

6 回答 6

40

git 2.9.X/2.10(2016 年第三季度)带来chmodgit add

请参阅Edward Thomson ( )的提交 4e55ed3(2016 年 5 月 31 日) 。 帮助者:Johannes Schindelin ( )(由Junio C Hamano 合并 -- --提交 c8b080a中,2016 年 7 月 6 日)ethomson
dscho
gitster

add:添加--chmod=+x/--chmod=-x选项

对于设置为 false 的存储库中的路径,将不会检测到可执行位(因此不会设置)core.filemode,尽管用户可能仍希望将文件添加为可执行文件以与其他具有 功能的用户兼容。 例如,添加 shell 脚本的 Windows 用户可能希望将它们添加为可执行文件,以便与非 Windows 上的用户兼容。core.filemode

Although this can be done with a plumbing command (git update-index --add --chmod=+x foo), teaching the git-add command allows users to set a file executable with a command that they're already familiar with.

You can see the origin of this new feature in "How to create file execute mode permissions in Git on Windows?" (Feb. 2011)

于 2016-07-09T19:14:19.303 回答
23

有几种方法可以做到这一点。

  1. Git 别名
  2. Bash 别名
  3. 甚至结合 bash 和 git 别名
  1. Git 别名

    你总是可以在你的 git 别名中使用 bash。

    • 打开你的 git 配置:

      vim ~/.gitconfig

    • 向其中添加别名部分(如果不存在):

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

    • 编辑您的 bash 配置文件:

      vim ~/.bashrc

    • 在文件末尾添加:

      function gitadd(){
          if [[ ${1: -3} == ".sh" ]]
              then git update-index --chmod=+x $1
          fi
          git add $1
       }
       alias gitadd='gitadd'
      
  3. 结合 git 和 bash 别名

    • 编辑您的 bash 配置文件:

      vim ~/.bashrc

    • 将此添加到文件末尾:

      function checkShellFile(){
          return ${1: -3} == ".sh"
      }
      alias gitadd='checkShellFile ? git addsrcipt "$1" : && git add "$1"'
      
    • 编辑你的 git 配置文件:

      vim ~/.gitconfig

    • 向其中添加别名部分(如果不存在):

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

以上均未经过测试

于 2014-03-28T10:53:34.120 回答
8

这是一个自动应用于git update-index --chmod+x可执行文件的脚本:

for f in `find . -name '*.sh' -o -regex './s?bin/[^/]+' -o -regex './usr/sbin/[^/]+' -o -regex './usr/lib/[^/]+' `;do
 ( cd `dirname $f` && git update-index --chmod=+x  `basename $f` )
done 
于 2014-07-11T19:13:44.057 回答
7

没有花哨的 bash 脚本的解决方案

  1. fileMode = true在您的.git/config文件中设置(或git config core.filemode true按照其他人指出的那样运行)
  2. 更改文件权限的可执行位并提交此更改。(chmod u+x $script正如你所指出的)。您只需执行一次。
  3. 推送到遥控器

下次您从那里拉取时,git 将在文件上设置已提交的可执行位。我也有类似的问题,这解决了。

fileMode = true告诉 git 跟踪它唯一可以跟踪的权限:可执行位。这意味着对可执行位的更改将被 git 识别为工作树中的更改,并且这些更改将与您的下一次提交一起存储在 repo 中。

一旦您提交了所需的可执行位,您还可以将您重置fileMode为,false这样下次当您不想提交时,git 就不会因为此类更改而打扰您。

于 2016-03-30T13:36:46.380 回答
1

我不认为这可以在git add命令上完成,但您可能能够在运行git commit命令后立即运行脚本,但在实际创建提交之前。

看一下预提交钩子。

http://git-scm.com/book/en/Customizing-Git-Git-Hooks

基本上只需在 .git/hooks/ 文件夹中创建一个名为 pre-commit 的文件。(你的钩子文件夹中应该已经有样本,重命名它们以删除末尾的“.sample”以激活一个。)

有一个问题。确保您的脚本git stash -q首先运行,以便您处理文件的实际分阶段版本。

于 2013-04-01T05:18:56.147 回答
0

I just added thru' Tortoise Git folder update. Right click on all files and update the execute permission checkbox to true and commit/push with message. Git Command line add/commit should also work.

于 2019-09-26T15:56:33.163 回答