Git: Receiving "fatal: Not a git repository" when attempting to remote add a Git repo
GitGit Problem Overview
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?
Git Solutions
Solution 1 - Git
Did you init a local Git repository, into which this remote is supposed to be added?
Does your local directory have a .git
folder?
Try git init
.
Solution 2 - Git
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>
Solution 3 - Git
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.
Solution 4 - Git
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.
Solution 5 - Git
$ git status
fatal: Not a git repository:
Just type the following in your cmd or git shell or any other terminal:
$ git init
Solution 6 - Git
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.
Solution 7 - Git
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.
Solution 8 - Git
For me the problem comes only when one is trying to execute git commands from a non-git 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 become git -C /dir/to/git status
and git add -A
will be git -C /dir/to/git -A
.
Solution 9 - Git
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.
Solution 10 - Git
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:
- Go inside the .git folder and rename "HEAD.lock" to just "HEAD"
- Decide for one GIT admin application and don't touch the other one
Solution 11 - Git
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.
Solution 12 - Git
For that you need to enter one command that is missing from bitbucket commands
Please try git init.
Solution 13 - Git
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
NB: The first step does destroy all git metadata saved locally on your device and git starts "afresh" so only turn to this answer as a last resort.
Solution 14 - Git
I just had re-initialize git in my directory
git init
and it worked
Solution 15 - Git
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
Solution 16 - Git
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....
Solution 17 - Git
GIT_DIR should be unset: unset GIT_DIR
Solution 18 - Git
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.
Solution 19 - Git
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.
Solution 20 - Git
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
Solution 21 - Git
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
Solution 22 - Git
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.
Solution 23 - Git
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
Solution 24 - Git
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 removing the quotes in the alias definition.
alias git-abc=git --git-dir=~/repos/abc --work-tree=~/Development/abc
Solution 25 - Git
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 ?
Solution 26 - Git
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.
Solution 27 - Git
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.
Solution 28 - Git
I had this issue and fixed it by adding README.md file
Solution 29 - Git
Happened to me when my client Smartgit put a newline in my .git/HEAD file. Deleting the empty line fixed it.
Solution 30 - Git
hit git init using terminal/cmd in your desired folder. It'll do the job.
Solution 31 - Git
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.
Solution 32 - Git
When you see this trying to push to github you may have to initialize this repo at github first: https://github.com/new.
Solution 33 - Git
I know there are a lot many answer already, but if you modified a bunch of files, and you are still afraid of losing the work.
Note: most of above solutions did not work for me.
i faced the same issue , so i did a workaround.
git clone <same project>
git checkout <to desired branch>//changed to respective branch
git branch // verify if you have same branch set
git pull
and than copied .git dir to the old one(please save a copy of the repo for code safety).
and do a git status , this will re-index the files. and you will have it working again.
Thanks i hope it will help a few.
Solution 34 - Git
If you run the
> git status
and viewing the files are downloaded and then deleted or rejected ,start to check this ways step by step and in every part check the directory again:
1- git init (perhaps you have not the right git directory)
2- git status (see where are you and what happened in cloning)
3- git reset --hard HEAD~1 (lose all the last changes in locally committed by cloning process)
Now you have returned to last HEAD. Because you used --hard, your files are reset to their state at last commit in Cloning. Check your project folder again
Solution 35 - Git
I was doing git branch
in the parent folder which did not have git initialised. doing git init
did not help me.
Finally I realised I was in the parent folder and did cd into the child directory, did git branch
again, and it worked.
If git init
is not working, and you have the required permissions, this might be the case with you.
Try changing the directory and it will work.