Executing git add in post-receive hook

If you want to add files to index in the post-receive hook, you will find it is possible to execute normal git add just as you usually do in other client hooks. In the post-receive hook, to add files to index, you have to first reset the environment variable GIT_WORK_TREE to the right directory, then git add would act normally as what you wish.

A post-receive hook that adds files and makes a commit

Suppose your remote repository is a non-bare one. Below is a full example of a post-receive hook that makes a commit.

#!/bin/sh

# Set GIT_WORK_TREE to the upper folder of .git
export GIT_DIR=`pwd`; cd ..; export GIT_WORK_TREE=`pwd`

# Make a commit
git add 
git commit -m 'my commit'

Environment variables used in the script:

  • GIT_DIR, the location of the .git folder.
  • GIT_WORK_TREE, the location of the root of the working directory for a non-bare repository.

Resources

Leave a Reply