Git更改工作目录

26
我想使用Git存储库,但是工作树应该是远程的。 例如:如果我的项目存储在~/project中,并且project.git存储在~/git/project.git中。 通过配置更改了工作树: worktree=/Users/myuser/project 我能够提交和查看差异,但是当我尝试执行git stash时,我遇到了错误: fatal: /usr/libexec/git-core/git-stash cannot be used without a working tree. 如何将.git目录远离工作树? 为什么会出现这个错误? git config --get core.worktree返回正确的工作目录...

尝试使用--git-dir或设置环境变量GIT_DIR,请参见https://dev59.com/am435IYBdhLWcg3wrSBB的被接受答案。 - devconsole
我已经看到了答案,但是我认为如果我覆盖GIT_DIR,在短时间内使用几个仓库时会造成一些混乱。我找到了worktree配置选项,但没有关于工作目录的内容...... 仓库也存在,我不能运行 git init --git-dir。如何使用 --git-dir?有文档吗?它可以通过配置设置吗? - ValeriiVasin
2
通常,git会在当前工作目录中查找.git目录。如果没有找到,则会在父目录中继续查找,直到达到根目录为止。在您的设置中,它永远不会找到.git,因此也不会找到.git/config。因此,您必须指定.git目录的位置。也许您可以为不同的项目使用不同的别名? - devconsole
非常感谢。上面的解释很好。我已经设置了环境变量GIT_DIR和GIT_WORK_TREE,一切都正常了。稍后我会发现一些东西:worktree的配置选项。为什么它存在? - ValeriiVasin
5个回答

24
以下代码似乎可以使用,根据您的需要进行调整:
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'

编辑:请注意,初始化后您不必再指定--work-tree,因为该值存储在git/test/config中。

您还可以进入work/test并从那里提交:

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'

然后使用绝对路径作为 --git-dir 或设置 GIT_DIR


14

修正--git-dir标志的使用:

使用:

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

而不是:

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

8

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


这是唯一对我有效的建议。 我需要通过sudo以另一个用户身份从CI用户运行git命令,而CI用户没有权限首先cd到该目录。(在此服务器上配置sudo仅允许特定命令作为另一个用户运行) - ViktorNova

1
你可以在项目文件夹外使用 .git-dir,而无需在每个命令中写入路径。
只需在你的工作目录中创建一个名为 .git 的文件,并添加以下内容即可:
gitdir: /path/to/your/dir/storage/dir/project.git

作为路径,您可以使用绝对路径和相对于工作目录的路径。

1
我看到有4个不同的选项,具体取决于你的起点在哪里。
  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
感谢您抽出时间解释每个选项! - Tyler Mammone

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接