[git] 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://USERNAME@NFSNSERVER/home/private/git/REPONAME.git

(After replacing USERNAME, NFSNSERVER, and REPONAME with the correct names) I receive the following error:

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

Can you help me get past this step?

This question is related to git

The answer is


Happened to me when my client Smartgit put a newline in my .git/HEAD file. Deleting the empty line fixed it.


When you see this trying to push to github you may have to initialize this repo at github first: https://github.com/new.


in my case, i had the same problem while i try any git -- commands (eg git status) using windows cmd. so what i do is after installing git for window https://windows.github.com/ in the environmental variables, add the class path of the git on the "PATH" varaiable. usually the git will installed on C:/user/"username"/appdata/local/git/bin add this on the PATH in the environmental variable

and one more thing on the cmd go to your git repository or cd to where your clone are on your window usually they will be stored on the documents under github cd Document/Github/yourproject after that you can have any git commands

thank you


Go to your source folder where local repo is stored , example mine is found in c:/GitSource , right click while in the folder , click git bash here , then git status....


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

It worked.


restore the .git/ORIG_HEAD and other root .git repo files

I got this error after restoring from backup, apparently the files contained in the .git directory root didn't make it to the target,but all the subfolders did so at first I thought the repo was intact.

I fixed it by restoring the root files.


If you are seeing this error in your self-hosted GitHub actions logs then it might be you are not using the latest git version supported by Github actions.

For knowing which version it is supported, go to actions/checkout logs and upgrade your server git version to the mentioned version or above this.


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.

smar@aaeru ~/P/Nominatim> git status
fatal: Not a git repository (or any of the parent directories): .git
smar@aaeru ~/P/Nominatim [128]> 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...

smar@aaeru ~/P/Nominatim [128]> 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 status, 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.


Below error seems like Gits didn't find .git file in current directory so throwing error message.

Therefore change to directory to repository directory where you have checkout the code from git and then run this command.

  • $ git checkout

GIT_DIR should be unset: unset GIT_DIR


hit git init using terminal/cmd in your desired folder. It'll do the job.


In my case I found that git in windows became case sensitive for the drive letter from some point.

After upgrading git binary in windows cli commands that used to work stopped. for example the path in the script was D:\bla\file.txt while git command accepted only d:\bla\file.txt


It seems like you are not going to your specific folder. For example, if I am working on a project named bugsBunny and it is saved in the folder d:/work:code , so first you have to go to that folder using cd d:/work/code/bugsBunny , then after that you can continue using your git commands.


In command line/CLI, you will get this error if your current directory is NOT the repository. So, you have to first CD into the repo.


For that you need to enter one command that is missing from bitbucket commands

Please try git init.


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.


In case it helps somebody else, I got this error message after accidentally deleting .git/objects/

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

Restoring it solved the problem.


For me, this was related to malformed ownership in my .git/ path. root owned .git/HEAD and .git/index, preventing the jenkins user from running the job.


Even i had the same problem. i wrote a shell script which will backup all my codes to my git repo on working days of a week at 17:55 by using crontab. by seeing the logs of cron i found the above mentioned problem.

the above problem comes only when you are trying to execute git commands from a non-gir dir(ie from other dir which is not the working copy). to fix this add -C <git dir> in the git command you are executing such that git status will be git -C /dir/to/git status and git add -A will be git -C /dir/to/git -A.


Probably too late but Another solution that might help future visitors. First delete the old .git directory -

rm .git

Then initialize the git repo again

git init

In my case I used Tortoise SVN and made the mistake to also use the Visual Studio GIT functions at the same time. That made Visual Studio lock the HEAD file inside the .git folder so neither VS or Tortoise could access the repo and i got the "fatal: Not a git repo..." error from both applications.

Solution:

  1. Go inside the .git folder and rename "HEAD.lock" to just "HEAD"
  2. Decide for one GIT admin application and don't touch the other one

I had this issue with the Jenkins Git plugin after authentication issues with GitLab. Jenkins was reporting 'hudson.plugins.git.GitException:[...]stderr: GitLab: The project you were looking for could not be found. fatal: Could not read from remote repository.'

However if I did a 'git clone' or 'git fetch' direct from the Jenkins box (command line) it worked without issue.

The issue was resolved by deleting the entire /workspace directory in the Jenkins jobs folder for that particular job, e.g.

rm -Rf $JENKINS_HOME/jobs/myJenkinsJob/workspace/

Presumably the local .git folder had got stale/corrupted ?


$ git status
fatal: Not a git repository:

Just type the following in your cmd or git shell or any other terminal:

$ git init

I reached this question because encountered the error message

fatal: not a git repository: '~/repos/abc'

and because I was afraid there was some incompatibilities between git versions (fortunately not),

none of the answers I read here was the solution for my case and I find some of this answers dangerous and misleading.

I got the error because I moved a repository from OpenBSD to Linux, but that could happen just by changing the shell, in OpenBSD I was using (in .kshrc with ksh) to invoke git (note the form ~/ in the paths):

alias git-abc='git --git-dir=~/repos/abc --work-tree=~/Development/abc'

in OpenBSD with ksh it works with that syntax to define aliases, while in linux with bash the ~ inside such exact same alias definition quoted doesn't expand when the alias is invoked, I solved by removed the quotes in the alias definition.

alias git-abc=git --git-dir=~/repos/abc --work-tree=~/Development/abc

git was working fine for be and all of sudden it started showing this fatal: Not a git repository (or any of the parent directories): .git message.

For me not sure what was corrupted in .git folder, I did git clone ** newfolder and copied the entire .git folder to my corrupted/old folder where I was making changes before git started showing error message..

Everything got back to normal and git also recognized my changed/un-staged files.


This may also be due to permissions. Check the owner / group permissions and make sure you have adequate permissions to access that data. In my case, I came across this error when running "git status" in a repo whose ownership was set to root:root. Running "git status" as root solved my issue. Alternatively, if you don't want the user/group ownership to be root:root, chown the repo to something you have access to.


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_DIR is [an environment variable] set to look for a .git directory (in the current working directory?) by default:

If the GIT_DIR environment variable is set then it specifies a path to use instead of the default .git for 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:

git init

or clone a repository

git clone <remote-url>
cd <repository>

I had this issue and fixed it by adding README.md file


In my case the file .git/HEAD was corrupted (contained only dots). So I edited it and replaced its content with:

ref: refs/heads/master

and it started working again.


In my case a system crash had caused the HEAD file to become corrupted. This guide shows how to fix that and other problems you may encounter.

https://git.seveas.net/repairing-and-recovering-broken-git-repositories.html