26

I want to work with Git repository, but working tree should be remote. For example: if I have my project stored inside of ~/project and project.git stored inside of ~/git/project.git.

What I've changed working tree via config:

worktree=/Users/myuser/project

And I'm able to commit and view diff, but when I've tried to do git stash, I got error:

fatal: /usr/libexec/git-core/git-stash cannot be used without a working tree.

How to store .git directory far from working tree? And why I'm getting this error?

git config --get core.worktree returns correct working directory....

4
  • Try --git-dir or set environment var GIT_DIR, see accepted answer of stackoverflow.com/questions/5283262/…
    – devconsole
    May 28, 2013 at 13:07
  • I've seen the answer, but I assume, if I override GIT_DIR it will cause some mess during working with few repositories in short period of time. I've found worktree configuration option, but nothing about working directory... Repository also exist, and I can't run git init --git-dir. How to use --git-dir? any documentation? Could it be set via config? May 28, 2013 at 13:18
  • 2
    Normally git looks for a .git directory in the current working directory. If it is not there it looks in the parent directory and so forth until it reaches the root directory. In your setup it will never find .git and therefore it will also not find .git/config. So you have to specify the location of the .git directory. Maybe you could use different aliases for different project?
    – devconsole
    May 28, 2013 at 13:25
  • Thanks a lot. Explanation above is great. I've setup environment variables GIT_DIR and GIT_WORK_TREE and all becomes OK. Something I will discover later: config option for worktree. Why does it exist? May 28, 2013 at 13:33

5 Answers 5

24

The following seems to work, adjust to your needs:

mkdir git
mkdir work
git --git-dir git/test --work-tree work/test init
mkdir work/test
echo -n foo > work/test/foo.txt
git --git-dir git/test status
git --git-dir git/test add foo.txt
git --git-dir git/test commit -m 'commit 1'

EDIT: Notice that you don't have to specify --work-tree after the repo has been initialized since that value is stored in git/test/config.

You can also cd into work/test and commit from there:

cd work/test
echo -n bar > bar.txt
git --git-dir ../../git/test status
git --git-dir ../../git/test add .
git --git-dir ../../git/test commit -m 'commit 2'

Then use an absolute path for --git-dir or set GIT_DIR.

13

Correction for --git-dir flag usage:

use:

git --git-dir=git/test/.git ...

instead of:

git --git-dir git/test ...
4

could used -C $dir

$ pwd
/home/guanzhang/lede
$ git diff --name-only
feeds.conf.default
$ cd /root/
$ git -C /home/guanzhang/lede diff --name-only
feeds.conf.default

1
  • This was the only suggestion that worked for me. I needed to run git commands from a CI user as another user via sudo, and the CI user didn't have permission to cd to the dir first. (sudo is configured on this server to only allow specific commands to be run as another user)
    – ViktorNova
    Sep 20, 2022 at 23:55
1

I see different 4 options depending where your starting point is.

  1. You have a remote repository (e.g. on GitHub) that you want to work on within a remote file system (e.g. Samba). In this case, you can just clone like this:

    git clone --separate-git-dir=/your/local/repository/dir https://your_repo_url /your/remote/workspace/dir
    

    /your/local/repository/dir and /your/remote/workspace/dir should not exist already and will be created by Git correctly. Afterwards, you can just work within the remote file system as usual, but without the typical performance issues that occur on remote file systems.

  2. There is no remote repository and you want to create a new one with a working tree at the remote file system. For this, please consider the answer of devconsole and Radon8472.
  3. You already have an existing repository and workspace on a remote file system. In this case, just move the .git directory to a local path and create a .git file in your working tree with this content:

    gitdir: /path/where/you/moved/your/.git
    
  4. You already have a working directory on your remote file system that is not attached to your remote Git repository yet. In this case, just run

    git clone -b https://your_repo_url
    

    somewhere on your local file system and then create a .git file in your working dir as described in option 3 or as described in Radon8472's answer.

1
  • 1
    Thank you for taking the time to explain each option! Aug 25, 2020 at 14:14
1

You can use a .git-dir outside of your project folder without writing the path to every command.

Simply create a file named .git in your working directory and add content like this:

gitdir: /path/to/your/dir/storage/dir/project.git

As Path you can use absolute path and path relative to the working directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.