Git pull in a Linux environment changes the owner of the files

21,449

When you use git pull (which is equivalent to git fetch; git merge), git create new files and does not care about previous ownership (git does not store that information).

There is different solution to solve this. The easier is probably to add a post-update hooks (in your .git/hooks directory) to automatically call chown john:john . -R after merge/pull (see that SO solution for an example).

Alternate solutions are:

  • Run git pull as the john user instead of root (that will require write permissions to john for .git directory).
  • Create a hook to make the deployment, which will either use john as user, either do the chown after update (so you will have to do git pull inside the post-receive hook of the bare repo).

In linux, it might be another solution with setuid / setgid

Share:
21,449

Related videos on Youtube

Andy
Author by

Andy

Keep on asking to gain more points! :P

Updated on September 18, 2022

Comments

  • Andy
    Andy almost 2 years

    Why will every time I execute a git pull on my live server, the owner of the files be changed? I keep on manually doing a chown john:john index.php.

    What do I need to change so that, when I do a git pull, the owner of the files will be automatically be or stay john?

    • user000001
      user000001 almost 10 years
      Is john the owner of the current directory?
    • Andy
      Andy almost 10 years
      Yes, john is the owner of the directory :)
    • cuonglm
      cuonglm almost 10 years
      Which user running git pull?
    • Andy
      Andy almost 10 years
      @Gnouc root user, I am logged in in our live server via SSH as root user :)
  • Wildcard
    Wildcard about 5 years
    What if you do git fetch as root and then git merge as user john? Will john still need write permissions for .git directory?
  • Asenar
    Asenar almost 5 years
    Yes, most of times. git fetch as root, new files in .git/objects` might have root as owner. git merge as user john will (most of time) try to overwrite at least .git/index and .git/HEAD (which might be deleted/created at each change I suppose), and write some files in .git/objects/. + any new files in your working directory. Maybe there is exceptions with fast forward merge for example.
  • run_the_race
    run_the_race over 2 years
    "git will update files without changing owner" not according to my tests: anocube@5153F344:~/terminal/conf/ubuntu $ ll -rw-r--r-- 1 root michael 956 Mar 10 11:56 terminal_sudoers michael@5153F344:~/terminal/conf/ubuntu $ git pull Updating 77b4d18..b281757 Fast-forward conf/ubuntu/terminal_sudoers | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) michael@5153F344:~/terminal/conf/ubuntu $ ll total 24 -rw-r--r-- 1 michael michael 1001 Mar 10 11:57 terminal_sudoers michael@5153F344:~/terminal/conf/ubuntu $ Here root:michael changes to michael:michael
  • Asenar
    Asenar over 2 years
    @run_the_race I'm pretty sure I tested before writing the answer, but it seems you're right. So maybe in 2015 it wasn't the case (or maybe I simply am wrong ^^). Thanks for the report, so I edited the answer