Search code examples
gitgithooksgit-bare

Post-receive hook causing untracked files in production folder


I checked the google for similar questions and the only thing that I found with similar problem is in this topic: Post-receive script results in untracked/modified files

The situation is this. I guide from the 4.4 Git on the Server - Setting Up the Server tutorial.

Making bare repo on computer 1:

$ cd /srv/git
$ mkdir project.git
$ cd project.git
$ git init --bare
Initialized empty Git repository in /srv/git/project.git/

Pushing the 1st version to bare repo on computer 2:

# on John's computer
$ cd myproject
$ git init
$ git add .
$ git commit -m 'Initial commit'
$ git remote add origin git@gitserver:/srv/git/project.git
$ git push origin master

Cloning in production folder on computer 1

git clone git@gitserver:/srv/git/project.git
$ cd project
$ vim README
$ git commit -am 'Fix for README file'
$ git push origin master

All looking good push/pull from both machines.

Now making the post-receive hook in hook folder:

#!/bin/bash
while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        git --work-tree=/var/www/html/project --git-dir=/srv/git/project.git checkout -f
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done

OK. Pushing from computer 2 when I commit 2 files "test_file1" and "test_file2" to bare. Success. Browsing to see if the hook worked in production folder on computer 1. Ok the files "test_file1" and "test_file2" are there.

get status 

In production folder echoing this:

On branch master
Your branch is ahead of 'origin/master' by 2 commits.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        public/test_file1
        public/test_file2

nothing added to commit but untracked files present (use "git add" to track)

I have .git folders in both production folder and local(testing) folder. Any idea if this is normal? And how to fix it?


Solution

  • You normally would not have a .git folder in production folder: only your post-receive hook in the bare repo is allowed to modify (checkout -f) said production folder.

    A production folder should not know anything about Git (unless Git is an essential element to actually run the program/project stored in "production")