tags:

views:

1042

answers:

4

I have an unusual idea to use git as a backup system. So let's say I have a directory ./backup/myfiles and I want to back that up using git. To keep things clean I don't want to have a .git directory in the myfiles folder, so I thought I could create ./backup/git_repos/myfiles. From looking at the git docs, I've tried doing this:

$ cd backup/myfiles
$ mkdir ../git_repos/myfiles
$ git --git-dir=../git_repos/myfiles init
Initialized empty Git repository in backup/git_repos/myfiles/
$ git --git-dir="../git_repos/myfiles/" add foo
fatal: pathspec 'foo' did not match any files

You can see the error message I get there. What am I doing wrong?

A: 

Assuming your myfiles directories already exists and has some content, could you live with this:

cd ~/backup
git init
git add myfiles

The .git directory will be in backup, not in myfiles.

Abie
Although that would sorta fix what I want, I'd rather just store the myfiles folder under git, and nothing else.
Rory
+7  A: 
git --git-dir=../repo --work-tree=. add foo

This will do what you want but will obviously suck when you have to specify it with every git command you ever use.

[Edit] You can export GIT_WORK_TREE=. and GIT_DIR=../backup and Git will pick them up on each command. That will only comfortably allow you to work in a single repository per shell, though. [/Edit]

I’d rather suggest symlinking the .git directory to somewhere else, or creating a symlink to the .git directory from your main backup directory.

Bombe
+3  A: 

It's conventional to name a directory rhat is a git repository that has its working tree in an unusual place with a '.git' extension, much like a bare repository.

mkdir ../git_repos/myfiles.git

If you had provided the --work-tree option at init time then this would have automatically set up the core.worktree config variable that means that git will know where to find the working tree once you specify the git directory.

git --git-dir=../git_repos/myfiles.git --work-tree=. init

But you can set this variable after the fact as well.

git --git-dir=../git_repos/myfiles.git config core.worktree "$(pwd)"

Once you've done this, the add command should work as expected.

git --git-dir=../git_repos/myfiles.git add foo
Charles Bailey
I've found if you cd to ../git_repos/myfiles.git first, rather than being in the real work tree, 'git add foo' will just work, and you don't need to specify --git-dir all the time.
Steve Folly
It's true, but I think that most people tend to work in their working tree rather than their repositories. Of course, if you're using a detached working tree you're probably doing something 'special' and may well be using some macros to help.
Charles Bailey
A: 

You could create a "nodgit" script (No Dot GIT) with somet like

#!/bin/sh
gits=/usr/local/gits
    x=`pwd`
    testdir() {( cd $1; pwd; )}
    while [ "$x" != "/" ]; do
      y=`echo $x|sed -e "s/\//__/g"`
      if ([ -d "$gits/$y" ]); then
        export GIT_DIR="$gits/$y"
        export GIT_WORK_TREE="$x"
        if ([ "$1" = "nodinit" ]); then
          mkdir -p "$GIT_DIR"
          git init --bare; exit $?
        elif ([ "$1" = "shell" ]); then
          bash; exit $?
        else
          exec git "$@"
        fi
      fi
      x=`testdir "$x/.."`
    done

You can call nodgit in place of git and it will set variables as necessary by looking for a git repo. For example say you have a (bare) repo in /usr/local/gits/__home__foo_wibbles and you are in in /home/foo/wibbles/one then it will find the correct working directory (/home/foo/wibbles) and repo.

Oh you can also use "nodgit shell" to get a shell with the correct vars set so you can use plain old git commands.

Paul Hedderly