ValeriiVasin
ValeriiVasin

Reputation: 8726

Git change working directory

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....

Upvotes: 29

Views: 79825

Answers (5)

张馆长
张馆长

Reputation: 1859

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

Upvotes: 10

Radon8472
Radon8472

Reputation: 5001

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.

Upvotes: 2

Patrick Fink
Patrick Fink

Reputation: 81

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.

Upvotes: 3

ikandars
ikandars

Reputation: 526

Correction for --git-dir flag usage:

use:

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

instead of:

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

Upvotes: 15

devconsole
devconsole

Reputation: 7925

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.

Upvotes: 25

Related Questions