execute git command inside bash script

As mentioned here or there, you could precede all your git commands with:

 env -i

in order to make sure there is no side effect with, for instance, a GIT_DIR environment variable which could have been previously set.
(env -i or simply: unset GIT_DIR)


For me, preceding with env -i does not work. I always get this error:

*** Please tell me who you are.

Although I've set the global config. This is what works for me by specifying --git-dir parameter:

#!/bin/sh

GIT='git --git-dir='$PWD'/.git'

$GIT checkout hotfix
$GIT merge --no-ff master -m "Merged master"
$GIT push

I run the script inside the working directory hence I use $PWD to get current working directory. You may adjust as necessary.

Tags:

Git

Bash

Githooks