[英]How to make current directory a git working directory
I've written multiple *.cpp files in the location ~/Code/CPLUS
before I know the existence of git. 在知道git的存在之前,我已经在~/Code/CPLUS
位置写入了多个* .cpp文件。
Now I want to use git for version control. 现在,我想使用git进行版本控制。
I created a folder ~/git_repo/
, and in this folder, I ran git init
command. 我创建了一个文件夹~/git_repo/
,在这个文件夹中,我运行了git init
命令。 When I tried to run the command git add my_first_c.cpp
under the path ~/Code/CPLUS
, the following message appeared: 当我尝试在~/Code/CPLUS
路径下运行命令git add my_first_c.cpp
,出现以下消息:
fatal: Not a git repository (or any of the parent directories): .git 致命:不是git存储库(或任何父目录):.git
Then I typed git init ~/git_repo/
under the path ~/Code/CPLUS
, the same error still appeared when git status
was typed. 然后我在路径~/Code/CPLUS
下键入git init ~/git_repo/
,当键入git status
时仍然出现相同的错误。
If I type git init
under the path ~/Code/CPLUS
, the add
and commit
can be executed. 如果我在~/Code/CPLUS
路径下键入git init
,则可以执行add
和commit
。 The only problem is that .git
is stored in ~/Code/CPLUS/
, while I'd like it be stored in ~/git_repo
. 唯一的问题是.git
存储在~/Code/CPLUS/
,而我希望将其存储在~/git_repo
。
My question is how to make the folder ~/Code/CPLUS
a working directory while the repo info is stored in ~/git_repo/
? 我的问题是,当回购信息存储在~/git_repo/
如何使文件夹~/Code/CPLUS
成为工作目录? And my machine has no GUI. 而且我的机器没有GUI。
您可以尝试从终端(或在〜/ .bashrc中)导出变量export GIT_WORK_TREE=~/git_repo/
并export GIT_DIR=../Code/CPLUS
,以便Git使用它们。
Thanks @Alariva , the suggested solution indeed solved this question. 感谢@Alariva,建议的解决方案确实解决了这个问题。 With .git
created in ~/git_repo/
, typing git --git-dir=/abs/path/to/repo/git_repo/.git add my_first_c.cpp
works. 在~/git_repo/
创建.git
,输入git --git-dir=/abs/path/to/repo/git_repo/.git add my_first_c.cpp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.