Can I store the .git folder outside the files I want tracked?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

You just need to ensure that the repository knows where the work tree is and vice versa.

To let the repository know where the work tree is, set the configuration value core.worktree. To let the work tree know where it's git directory is, add a file named .git (not a folder!) and add a line like

gitdir: /path/to/repo.git

Since git 1.7.5 the init command learned an extra option for this.

You can initialize a new separate repository with

git init --separate-git-dir /path/to/repo.git

This will initialize the git repository in the separate directory and add the .git file in the current directory, which is the working directory of the new repository.

Previously to 1.7.5 you had to use slightly different parameters and add the .git file yourself.

To initialize a separate repository the following command links the work-tree with the repository:

git --git-dir=/path/to/repo.git --work-tree=. init && echo "gitdir: /path/to/repo.git" > .git

Your current directory will be the working tree and git will use the repository at /path/to/repo.git. The init command will automatically set the core.worktree value as specified with the --git-dir parameter.

You could even add an alias for this:

[alias]
    initexternal = !"f() { git --work-tree=. --git-dir=\"$1\" init && echo \"gitdir: $1\" >> .git; }; f"

Use git version control on a read-only working directory

With the knowledge above, you can even set up git version control for an working directory without having write permissions. If you either use --git-dir on every git command or execute every command from within the repository (instead of the working directory), you can leave out the .git file and therefore do not need to create any files within the working directory. See also Leos answer

Solution 2 - Git

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.

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.

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

Solution 3 - Git

The --separate-git-dir option for git init (and git clone) can be used to accomplish this on my version of git (1.7.11.3). The option separates the git repository from the work tree and creates a filesystem agnostic git symbolic link (in the form of a file named .git) in the root of the work tree. I think the result is identical to niks' answer.

git init --separate-git-dir path/to/repo.git path/to/worktree

Solution 4 - Git

I find it simpler to reverse the --work-tree and --git-dir directories used in niks' answer:

$ cd read_only_repos
$ git --work-tree=/some/readonly/location/foo/ --git-dir=foo init
$ cd foo
$ git status
On branch master

Initial commit

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

        .file_foo
        bar
        ...

This approach has two advantages:

  • It removes the need to have any command-line options or .git files. You just operate normally from within the root of the repository.
  • It allows you to version a file system even if you don't own it. Git will only write to the repository location.

The only caveat I have encountered is that instead of using a .gitignore file, you edit info/exclude.

You can then use the repository read_only_repos/foo as a remote in your own repositories even if the original files are not under version control.

Solution 5 - Git

It's conventional to name a directory that 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

Solution 6 - Git

Use git inside the repo:

cd ./backup/git_repos/myfiles
git init --bare
git config core.worktree ../myfiles
git config core.bare false

From now on, you can use git inside the ./backup/git_repos/myfiles directory, without setting any environment variables or additional parameters.

Solution 7 - Git

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.

Solution 8 - Git

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.

Solution 9 - Git

To clarify the difference between the two options below, you can tell your git repository to track files somewhere else (core.worktree) or you can put a breadcrumb in your main worktree that points to a git repository folder somewhere else (--separate-git-dir). The core.worktree option does not need a breadcrumb file and has been a feature of git since first release. The second option is newer and requires a breadcrumb file, but doesn't require altering the repository configuration file.

Option 1: core.worktree

Initialize a non-bare repository outside of the path you want to track and set core.worktree to the path you want to track. You can do this using terminal commands to set this value or directly edit the repository config file to add:

worktree = <path to files to backup>

Do not make the repository folder a subfolder of this path, that would be recursive. You could possibly try this and simply ignore the repository folder, but I think git won't allow this scenario.

In your case, you would go to backup/git_repos/ to run the init command and could use the --git-dir=./myfiles option to override the default repository folder name. The commands would look like this:

cd backup/git_repos 
git init --git-dir=./myfiles
git config core.worktree backup/myfiles

NOTE 1: This is not the same as the newer ADDITIONAL worktree functionality of git. This is your main working tree which is used by all non-bare repositories. Those additional worktrees use breadcrumbs to subfolders of the git repository folder, much like Option 2 below.

NOTE 2: I recently tested a great many git GUIs for windows and only Git Extensions supports using core.worktree to move the main working tree.

GUIs that do not support core.worktree

SourceTree, Fork, Tower, GitKraken, GitHub Desktop, GitAhead, SmartGit*, and Git-Cola. You will want to stick to the terminal when using core.worktree.

* SmartGit conflaits this feature with Option 2 and is asking for a .git file. This is not required for core.worktree.

Option 2: --separate-git-dir

Initialize a repository at the path you want to backup using --separate-git-dir=<path to hold repository data>. This will use the specified path to hold the repository data and create a .git file in the initialization location that contains a line like this:

gitdir: <path to hold repository data>

For you, the commands would look like this:

cd backup/myfiles
git init --separate-git-dir=backup/git_repos/myfiles/

And your .git file in backup/myfiles/ will contain gitdir: backup/git_repos/myfiles/

You now operate git treating the location of the .git file as if that was the repository location.

Solution 10 - Git

I create scripts that look like

~/bin/git-slash:

#!/usr/bin/sh

export GIT_DIR=/home/Version-Control/cygwin-root.git/
export GIT_WORK_TREE=/

git --git-dir=$GIT_DIR --work-tree=$GIT_WORK_TREE "$@"

exit $?

It's redundant to use --git_dir=$GIT_DIR, but reminds me that I can also set environment variables outside the script.

The above example is for tracking local changes to cygwin system files.

Can make one such script for any major project that needs this - but / without /.git is my main use.

The above is small enough to make a shell alias or function, if you eliminate the redundancy.

If I do this often enough, I would revive the workspace to repository mapping of

"Boxes, Links, and Parallel Trees: Elements of a Configuration Management System", 
in Workshop Proceedings of the Software Management Conference. 1989.

whose closest modern counterpart is Perforce mappings or views, supporting partial checkouts as well as non-colocation of workspace and repo.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAmandasaurusView Question on Stackoverflow
Solution 1 - GitniksView Answer on Stackoverflow
Solution 2 - GitBombeView Answer on Stackoverflow
Solution 3 - GitFryerView Answer on Stackoverflow
Solution 4 - GitLeoView Answer on Stackoverflow
Solution 5 - GitCB BaileyView Answer on Stackoverflow
Solution 6 - GitTo1neView Answer on Stackoverflow
Solution 7 - GitPaul HedderlyView Answer on Stackoverflow
Solution 8 - GitAbieView Answer on Stackoverflow
Solution 9 - GitCapinWinkyView Answer on Stackoverflow
Solution 10 - GitKrazy GlewView Answer on Stackoverflow