0

I've written multiple *.cpp files in the location ~/Code/CPLUS before I know the existence of git.

Now I want to use git for version control.

I created a folder ~/git_repo/, and in this folder, I ran git init command. When I tried to run the command git add my_first_c.cpp under the path ~/Code/CPLUS, the following message appeared:

fatal: Not a git repository (or any of the parent directories): .git

Then I typed git init ~/git_repo/ under the path ~/Code/CPLUS, the same error still appeared when git status was typed.

If I type git init under the path ~/Code/CPLUS, the add and commit can be executed. The only problem is that .git is stored in ~/Code/CPLUS/, while I'd like it be stored in ~/git_repo.

My question is how to make the folder ~/Code/CPLUS a working directory while the repo info is stored in ~/git_repo/? And my machine has no GUI.

user3813057
  • 891
  • 3
  • 13
  • 31
  • Is this what you are trying to do? http://stackoverflow.com/questions/7060401/working-on-git-repo-without-cd-into-directory – alariva Jul 22 '15 at 23:45
  • Why would you want your git working directory to be anything other than the root directory of the source tree? This isn't typically how you use git and I don't think what you want to do is a very smart way to use it. – Matt Jul 22 '15 at 23:59
  • @Alariva The suggested solution indeed solved my question. With `.git` created in `~/git_repo/`, typing `git --git-dir=~/git_repo/.git add my_first_c.cpp` works. – user3813057 Jul 23 '15 at 00:44
  • The solution to another [post](http://stackoverflow.com/questions/7060401/working-on-git-repo-without-cd-into-directory) applies to this question, I don't know if this post should be closed or marked as duplicate. – user3813057 Jul 23 '15 at 00:47

2 Answers2

0

You could try exporting the variables export GIT_WORK_TREE=~/git_repo/ and export GIT_DIR=../Code/CPLUS from terminal (or in your ~/.bashrc) so Git uses these.

Pepedou
  • 787
  • 1
  • 13
  • 35
0

Thanks @Alariva , the suggested solution indeed solved this question. With .git created in ~/git_repo/, typing git --git-dir=/abs/path/to/repo/git_repo/.git add my_first_c.cpp works.

The solution comes from this post.

Community
  • 1
  • 1
user3813057
  • 891
  • 3
  • 13
  • 31