21

I have been noticing that when I pull from my github repo on a development server(Red Hat) the ownership of the files change after the pull is completed. The .git file used to be owned by me but then I noticed that it would write files as me and I need it to write files as a different user. So i changed the ownership of the .git directory.

I stumbled on to git config core.filemode which was true. I since have set to it false. I have not seen a difference after setting this to false. What should I do to keep my files ownership from changing.

This doesn't happen to me locally.

Jules
  • 14,200
  • 13
  • 56
  • 101
user2108258
  • 803
  • 3
  • 13
  • 20

2 Answers2

43

If it suffices to preserve the group, you can set the setgid flag on the directories. See http://en.wikipedia.org/wiki/Setuid

Setting the setgid permission on a directory ("chmod g+s") causes new files and subdirectories created within it to inherit its group ID, rather than the primary group ID of the user who created the file (the owner ID is never affected, only the group ID). Newly created subdirectories inherit the setgid bit. Thus, this enables a shared workspace for a group without the inconvenience of requiring group members to explicitly change their current group before creating new files or directories. Note that setting the setgid permission on a directory only affects the group ID of new files and subdirectories created after the setgid bit is set, and is not applied to existing entities. Setting the setgid bit on existing subdirectories must be done manually, with a command such as the following:

[root@foo]# find /path/to/directory -type d -exec chmod g+s '{}' \;

Community
  • 1
  • 1
nafg
  • 2,424
  • 27
  • 25
  • 4
    Nice solution - Always prefer to see a solution to the problem rather than a comment based on what particular software does - Using gid bits are the easy win here. – Aaron Dobbing Mar 24 '16 at 10:16
10

Git isn't a deployment server. It writes files as whoever's writing the files, that would be you. If you need permissions and ownership set, you could cook up a post-checkout hook and have it read a config file or something to decide what to do next.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • From my experience with this I can't say that is entirely true. If i change the ownership of the files in the directory and then run 'git pull' I get different owner ship for different files. Some root and some me and some un touched. – user2108258 Feb 25 '13 at 21:25
  • 3
    git doesn't store file permissions so any behavior you encounter is not directly related to git as a VCS. – wRAR Feb 25 '13 at 21:35
  • Who should the .git directory be owned by? My user on the server is the one I use to pull from Github. However if I use my self as the user the files are written to the disk as me. But I want them to be owned by the web server. – user2108258 Feb 25 '13 at 22:02
  • 5
    Files git creates are created with the credentials of whatever user is effectively running the git command. On unix (including OS X) to run as another user, you can `sudo -u thatuser git somecommand`; on windows I'm sure [there's some equivalent somewhere](https://www.google.com/search?q=windows+sudo+equivalent), – jthill Feb 25 '13 at 23:09