简体   繁体   English

如何使当前目录成为git工作目录

[英]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 ,则可以执行addcommit 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

The solution comes from this post . 解决方案来自此文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM