「Git」- 文件权限、所有者

  CREATED BY JENKINSBOT

在检出时,保持原文件的所属用户及组

Is there a way to trigger a hook after a new branch has been checked out in Git?

在 git checkout 时,如果分支里的文件和硬盘中的文件不同,git会创建新文件,并且新文件的用户和组为当前用户及其组。

现象如下:

# ll Config.php
-rwxr-xr-x 1 www www 2165 Feb 26 17:45 Config.php*

# git checkout master
Switched to branch 'master'
Your branch is behind 'origin/master' by 11 commits, and can be fast-forwarded.
  (use "git pull" to update your local branch)

# ll Config.php
-rw-r--r-- 1 root root 2162 Feb 26 17:45 Config.php

原文件的用户及组为www:www,我使用的root用户检出后成为了root:root。这就比较麻烦了,比如用Samba共享之后,由于Samba的匿名用户为默认的www,而checkout出的文件为root:root,这会导致访问Samba的用户无法保存该文件。

当然可以每次手动执行chown www:www . -R,但这不程序。

方法一、使用 git hook

在本地仓库的 .git/hooks 下创建 post-checkout 文件,文件的内容如下:

#!/bin/sh
chown www:www . -R

并赋予执行权限:chmod u+x post-checkout

如果是执行git pull(等价于git fetch; git merge操作)操作,需要创建post-update文件,文件内容与post-checkout相同。

方法二、使用www账户

即使用 www 账户来执行 git 相关操作。

忽略文件权限的变化

How do I make Git ignore file mode (chmod) changes? – Stack Overflow

问题描述:
当文件权限发生变化后,Git 也会将其视为变化。当这些文件被两个文件系统工具共享时,文件权限经常被工具改变,我们希望 Git 能够忽略这种变化,

解决方案:

git config core.fileMode false

注意事项:
1)这种方式并非最佳实践,仅作为某些特殊场景的 Workaround 来使用。