Git 更改工作目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16792737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git change working directory
提问by ValeriiVasin
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
.
我想使用 Git 存储库,但工作树应该是远程的。例如:如果我有存储的在我的项目~/project
和project.git
存储的内部~/git/project.git
。
What I've changed working tree via config:
我通过配置改变了工作树:
worktree=/Users/myuser/project
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:
而且我能够提交和查看差异,但是当我尝试这样做时git stash
,出现错误:
fatal: /usr/libexec/git-core/git-stash cannot be used without a working tree.
致命:没有工作树就不能使用 /usr/libexec/git-core/git-stash。
How to store .git
directory far from working tree?
And why I'm getting this error?
如何存储.git
远离工作树的目录?为什么我会收到这个错误?
git config --get core.worktree
returns correct working directory....
git config --get core.worktree
返回正确的工作目录....
采纳答案by devconsole
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
.
编辑:请注意,您不必--work-tree
在 repo 初始化后指定,因为该值存储在git/test/config
.
You can also cd into work/test and commit from there:
您还可以 cd 进入工作/测试并从那里提交:
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
.
然后为--git-dir
or set使用绝对路径GIT_DIR
。
回答by ikandars
Correction for --git-dir
flag usage:
--git-dir
标志使用的更正:
use:
用:
git --git-dir=git/test/.git ...
instead of:
代替:
git --git-dir git/test ...
回答by Radon8472
You can use a .git-dir outside of your project folder without writing the path to every command.
您可以在项目文件夹之外使用 .git-dir ,而无需写入每个命令的路径。
Simply create a file named .git
in your working directory and add content like this:
只需.git
在您的工作目录中创建一个名为的文件并添加如下内容:
gitdir: /path/to/your/dir/storage/dir/project.git
As Path you can use absolute path and path relative top the working directory.
作为路径,您可以使用工作目录的绝对路径和相对路径。
回答by Patrick Fink
I see different 4 options depending where your starting point is.
我看到不同的 4 个选项,具体取决于您的起点在哪里。
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.
- 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 devconsoleand Radon8472.
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
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.
您有一个要在远程文件系统(例如 Samba)中使用的远程存储库(例如在 GitHub 上)。在这种情况下,您可以像这样克隆:
git clone --separate-git-dir=/your/local/repository/dir https://your_repo_url /your/remote/workspace/dir
/your/local/repository/dir 和 /your/remote/workspace/dir 不应该已经存在并且将由 Git 正确创建。之后,您可以像往常一样在远程文件系统中工作,但不会出现远程文件系统上发生的典型性能问题。
- 没有远程存储库,您想在远程文件系统上创建一个带有工作树的新存储库。为此,请考虑devconsole和Radon8472的回答。
您已经在远程文件系统上有一个现有的存储库和工作区。在这种情况下,只需将 .git 目录移动到本地路径并
.git
在您的工作树中创建一个包含以下内容的文件:gitdir: /path/where/you/moved/your/.git
您的远程文件系统上已经有一个尚未附加到远程 Git 存储库的工作目录。在这种情况下,只需运行
git clone -b https://your_repo_url
在本地文件系统上的某个地方,然后
.git
按照选项 3 中的描述或Radon8472的回答中的描述在工作目录中创建一个文件。