Bash – Specifying which git repo to use

bashgit

I have a bash script, called cgit, that acts as git for one specific git repo (located at /.git):

#!/bin/bash
cd /;
sudo git $@ > /dev/stdout

I use it to keep track of imporant system files:

cgit add /etc/rc.conf

The trouble is when I try to add content relative to the directory I'm in, for example:

cd /home/user
cgit add .ssh/config

cgit would try and add /.ssh/config to the /.git repo.

Any suggestions or work-arounds?

Best Answer

I'd update your cgit script to use --git-dir. From the man pages:

   --git-dir=<path>
       Set the path to the repository. This can also be controlled by
       setting the GIT_DIR environment variable. It can be an absolute
       path or relative path to current working directory.

So it would become:

#!/bin/bash
sudo git --git-dir=/.git $@ > /dev/stdout

FTR, I haven't tried it.

Related Question