Receiving "fatal: Not a git repository" when attempting to remote add a Git repo
I am introducing myself to Git by following this tutorial:
Everything works fine up until the part where the repo is added to my local machine:
git remote add nfsn ssh://[email protected]/home/private/git/REPONAME.git
(After replacing USERNAME, NFSNSERVER, and REPOAME with the correct names) I receive the error:
fatal: Not a git repository (or any of the parent directories): .git
Can you help me get past this step?
Did you init a local Git repository, into which this remote is supposed to be added?
Does your local directory have a
Read more... Read less...
You'll get this error if you try to use a Git command when your current working directory is not within a Git repository. That is because, by default, Git will look for a
.git repository directory (inside of the project root?), as pointed out by my answer to "Git won't show log unless I am in the project directory":
According to the official Linux Kernel Git documentation,
GIT_DIRis [an environment variable] set to look for a
.gitdirectory (in the current working directory?) by default:
GIT_DIRenvironment variable is set then it specifies a path to use instead of the default
.gitfor the base of the repository.
You'll either need to
cd into the repository/working copy, or you didn't initialize or clone a repository in the first place, in which case you need to initialize a repo in the directory where you want to place the repo:
or clone a repository
git clone <remote-url> cd <repository>
My problem was that for some hiccups with my OS any command on my local repository ended with "fatal: Not a git repository (or any of the parent directories): .git", with fsck command included.
The problem was empty HEAD file.
I was able to find actual branch name I've worked on in .git/refs/heads and then I did this:
echo 'ref: refs/heads/ML_#94_FILTER_TYPES_AND_SPECIAL_CHARS' > .git/HEAD
NOTE: this does not answer to the common problem, which was OP’s problem, but to different problem where this error message may come up. I didn’t feel like doing new question just to write this answer down, tell me if I should do that instead :P
I got to situation, most likely due to some corruption of certain crash I had, that I got this error even when
.git did exist.
[email protected] ~/P/Nominatim> git status fatal: Not a git repository (or any of the parent directories): .git [email protected] ~/P/Nominatim > ls .git COMMIT_EDITMSG config* FETCH_HEAD HEAD index logs/ modules/ objects/ ORIG_HEAD packed-refs
Since I didn’t have anything that really needed preserving, I just went with dummy way, and did...
[email protected] ~/P/Nominatim > git init Reinitialized existing Git repository in /home/smar/Projektit/Nominatim/.git/
Still not working though, as for example
git log returns
fatal: bad default revision 'HEAD'. Remotes were there though, so I did
git fetch --all and then just
git reset --hard origin/master to get myself to the state the repo was previously.
Note that if there is some uncommitted changes, you can see them with
git diff and so on. Then just
git diff yourfile > patch before running the reset.
At least for me reflog (
git reflog) disappeared completely. Hence, if you do the reset, and there was some changes you wanted to prevent, I’m not sure you can get them back after reset anymore. So, make sure that you have all changes you can’t lose backed up, ultimately by just copying the clone before trying this.
This issue occurred to me after I moved the location of a git project on the filesystem. When I ran some git commands the error occurred, e.g.:
$ git status fatal: Not a git repository: /home/rospasta/path_old/gitprojecta/.git/modules/.travis
I found in
/home/rospasta/path_old/gitprojecta/.travis/.git the absolute path of the old location of the project was written. Manually updating this path of the new location resolved the problem for me.
So my issue may or may not be a git problem, but HTH.