545

I am following this tutorial. Everything works fine until I run this on my local machine (after replacing the $VARIABLEs with their actual values):

git remote add nfsn ssh://$USERNAME@$SERVER/home/private/git/$REPONAME.git

I receive the following error message:

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

How do I get past this step?

Corey
  • 6,612
  • 4
  • 20
  • 28

38 Answers38

1120

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.

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
  • 3
    I had already done this step, but had to do it again after setting up my engine yard account installing engine yard. – AVProgrammer Feb 06 '12 at 01:04
  • 3
    my `.git` folder is gone. Is `git init` the same as delete the repository folder, and then `git clone ...`. All I need to do is a `pull` – Francisco Corrales Morales May 05 '14 at 21:34
  • 3
    @FranciscoCorralesMorales if your `.git` folder is gone, you can run `git init` again to make a new one. You'll need to re-add a remote before you can pull from it though. –  Jun 01 '14 at 02:18
  • 3
    I was getting the same error when submitting changes from local system. Tried **git init** and it worked. Thanks! – rbashish Feb 06 '16 at 20:51
  • 14
    mine does have .git and I already had done git init but I get error `jalal@klein:~/computer_vision/py-faster-rcnn$ git add -A fatal: Not a git repository: caffe-fast-rcnn/../.git/modules/caffe-fast-rcnn` – Mona Jalal Aug 31 '16 at 18:12
  • 2
    @MonaJalal: same for me. How does one go about solving the error in this case? – Hassan Baig Jan 29 '17 at 12:04
  • 2
    Generally we clone the fork and forget to cd into the directory which is actual git repo. just CD in the dir and try command again – Sumer Oct 03 '18 at 07:11
  • git init then I can add my repository. – asifaftab87 Mar 11 '19 at 02:57
  • with git init --bare there is no .git folder. – Gur Aug 26 '20 at 11:14
  • wow forgot to change into the cloned git repo before adding the upstream – trallnag Jan 11 '21 at 16:14
  • This will fix if you already have a .git folder and the remote got deleted, it will reinitialise the remote and you are good to go. Typically if you copy a project with git folder and deleted the remote repo to avoid big file size like me, this init will def help you – Brendon Oct 26 '21 at 05:30
100

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>
Community
  • 1
  • 1
  • 14
    +1 for mentioning GIT_DIR. within hooks in a bare repo, GIT_DIR is set to '.' instead of '.git'. – commonpike Sep 06 '14 at 11:40
  • +1 for `git init`. After reading your answer, I used this multiple times on repositories that I **did** clone, but were not recognized. So, it seems to be safe when run from a directory tree that is supposed to be a `git repository`, has all the files in the git directory, but is claiming to not be **`fatal: Not a git repository (or any parent up to mount point`** – ElderDelp Dec 29 '17 at 19:03
  • @commonpike indeed: my hook didn't work because of that, I wanted to pull another repo when something was pushed to my bare repo. I had to set GIT_DIR to ".../.git" – rambi Jun 15 '21 at 10:35
55

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.

Marcin T.P. Łuczyński
  • 3,255
  • 1
  • 20
  • 15
  • 4
    My HEAD file was for any reason corrupted, so I had tor restore it and fixed the issue – Steven Dec 20 '18 at 12:16
  • 1
    You are great pal. Saved a Ton of my time – Vaibhav Sah Jan 24 '19 at 13:09
  • This was my issue also. Thanks – Carlos Rafael Ramirez Feb 05 '19 at 18:03
  • This worked for me. I also had a few project files that were blank (because my PC crashed during a branch command). Stashing them worked and everything was back in order. – William W Feb 27 '19 at 20:01
  • 4
    I had Visual Studio open during a git rebase and Visual Studio noticed a file change and shortly afterwards I had a blue screen of death. After the reboot .git/HEAD was filled with NULLS and when I replaced them with a full hash (5621afeffbabed40e3f386676068c45643644b7d) read from somewhere like .git\refs\remotes\origin\master, git started working again I also needed https://stackoverflow.com/questions/1115854/how-to-resolve-error-bad-index-fatal-index-file-corrupt-when-using-git as this told me to delete .git/index and I needed to remove .git/index.lock before doing git reset. Fine now – Ivan Mar 08 '19 at 11:28
  • Yes, somehow my `.git/HEAD` file was missing. But I had a `.git/ORIG_HEAD`, so I copied that to `HEAD`. It wasn't in the right state, so then I just did `git checkout master` and back in business. – wisbucky Jun 14 '19 at 22:09
  • Same problem here as @Ivan... blue screen when vscode was open. Thank goodness I didn't lose anything. I ended up changing the contents of `.git/HEAD` from a bunch of NULL chars to `ref: refs/heads/master` and then `rm -f .git/index` and `git reset` – Redtopia Jul 13 '19 at 21:40
  • tff this post #saved – olisteadman Oct 17 '19 at 11:12
  • 1
    I almost lost my hope to get my work back. Just I copy and paste your solution and it did magic. Thanks – Mukesh Kumar Oct 26 '19 at 19:26
  • In my case a `git init .` worked, it said something about recreating existing repo, so it understood the problem. – Peter Kofler Mar 01 '20 at 15:56
  • 1
    That was the issue for me too. I also had local changes to a lot of files and this saved me of some extra work. Thanks! – Ady Moldo Nov 25 '20 at 08:12
  • 1
    Thanks, actually my .git/HEAD was owned by root and could not be read by user. Changing ownership solved it :) – XonqNopp Jul 18 '23 at 09:38
22

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.

Smar
  • 8,109
  • 3
  • 36
  • 48
  • I got the issue. And did try the way @Smar mentioned, but the same result "fatal....". So, what the next to do? any suggestion? – Aaron Oct 31 '18 at 02:43
  • 3
    This issue occured while using Visual studio, `git init` and `git fetch --all` did fix the issue. – Morse Nov 09 '18 at 22:18
  • It looks the issue is from long time ago, but still happens. I was using VS 2022 to update projects to newer .Net - 4.7.2 and suddenly `git` commands stopped working. The `.git` folder was there. And using `git init` and `git fetch --all` fixed the issue. Another thing that could cause the issue is that I was on a branch that was merged/deleted on the remote. – mihkov Jul 29 '22 at 12:23
8

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.

IsaacS
  • 3,551
  • 6
  • 39
  • 61
8
$ git status
fatal: Not a git repository:

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

$ git init
der_michael
  • 3,151
  • 1
  • 24
  • 43
8

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.

eduherminio
  • 1,514
  • 1
  • 15
  • 31
7

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.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Mani
  • 5,401
  • 1
  • 30
  • 51
6

I just had re-initialize git in my directory

git init 

and it worked

Pavithra B
  • 141
  • 2
  • 6
4

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.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
sid smith
  • 533
  • 1
  • 6
  • 18
4

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
Adrian Rosca
  • 6,922
  • 9
  • 40
  • 57
4

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.

Vikrant singh
  • 433
  • 1
  • 7
  • 25
4

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

Please try git init.

VIKAS KOHLI
  • 8,164
  • 4
  • 50
  • 61
3

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.

kevthanewversi
  • 3,686
  • 4
  • 27
  • 27
  • 8
    It should be noted that this destroys all git metadata such as commits. You will keep your data, but Git will "start fresh" - this may not be a good thing. – Joseph Aug 06 '16 at 13:03
  • 3
    @JosephA.: you're right, that's suboptimal. Is there a better solution you can suggest? I get the `fatal` error even though `.git` folder exists. – Hassan Baig Jan 29 '17 at 12:06
  • But hey, aren't the commits already on the remote repo? Or what would you really need the commits on your HD/locally for...? @JosephA. – kevthanewversi Mar 15 '17 at 14:52
  • Coz I'm thinking you can still do stuff like git revert , git reset without having the commit info locally...right? @JosephA. – kevthanewversi Mar 15 '17 at 14:54
  • @kevthanewversi Yes, the commits could be on the remote repo - but we're starting to assume things here, thus in general this answer would be inadmissible for most-- – Joseph Mar 17 '17 at 09:01
  • 2
    This is wiping everything! Probably the worst way to resolve this. – Richard Lalancette Mar 04 '18 at 12:53
  • I had a file named `.git` (not a folder). Rename/delete that file and things started working. It is a minor twist to your answer. – tgolisch Feb 09 '20 at 20:44
  • You will lose all your local branches/commits if you do this. This shouldn't be the recommended solution. If someone doesn't understand what this does and blindly copy pastes this, they could lose everything. – Roshan Pal Oct 11 '21 at 10:56
3

GIT_DIR should be unset: unset GIT_DIR

petertc
  • 3,607
  • 1
  • 31
  • 36
2

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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
keyo
  • 81
  • 1
2

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....

wesley7
  • 101
  • 3
2

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.

cryss
  • 4,130
  • 1
  • 29
  • 34
2

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

mattbloke
  • 1,028
  • 1
  • 12
  • 26
2

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
Marco Munari
  • 132
  • 4
1

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.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Hugh Esco
  • 67
  • 1
  • 6
  • 1
    This doesn't appear to be reproducible on the command line. After setting ownership of `.git/HEAD` and `.git/index` to `root:root`, I am still able to add remotes without error. When I change ownership of the entire `.git` directory to `root`, I get a different error: `error: could not lock config file .git/config: Permission denied`. – Kyle Strand Jan 14 '16 at 23:21
1

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
Ajay Kumar
  • 4,864
  • 1
  • 41
  • 44
1

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

ilane
  • 11
  • 1
1

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.

Neel
  • 13
  • 2
1

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.

EliT
  • 75
  • 8
  • Yes. I set up `etckeeper` which must be initialized with root. So `sudo etckeeper`. Henceforth, all git-operations must use root, too. I found it by running `git status` and finding that my etc-dir was `not a git repository (or any of the parent directories): .git`. – Chris Nov 10 '22 at 13:29
  • This. I was running as root and forgot, so git status gave me the fatal error "detected dubious ownership in repository", which enlightened me. Chowning a couple of files and exiting root made everything work perfectly again. – ivanlan May 04 '23 at 19:59
1

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

ganji
  • 752
  • 7
  • 17
1

In my case, the directory I was working in was assigned to the other user on the system. After verifying that I had the .git/ folder present in this directory, I ran

git config --global --add safe.directory <absolute_path_to_the_directory>

and that solved it for me.

Derek T. Jones
  • 1,800
  • 10
  • 18
svitanok
  • 175
  • 4
  • 9
1

For me the issue was that the directory was apparently cloned by a different user inside of jenkins, this call fixed it

git config --global --add safe.directory $PWD

Ron Serruya
  • 3,988
  • 1
  • 16
  • 26
0

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 ?

GreensterRox
  • 6,432
  • 2
  • 27
  • 30
0

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.

AndrewD
  • 4,924
  • 3
  • 30
  • 32
0

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

Supun Kavinda
  • 1,355
  • 14
  • 13
0

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

b15
  • 2,101
  • 3
  • 28
  • 46
0

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

Aman Singh
  • 347
  • 2
  • 4
0

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.

Zubair Hassan
  • 776
  • 6
  • 14
0

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

dr0i
  • 2,380
  • 2
  • 19
  • 36
0

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.

CuriousDev
  • 400
  • 3
  • 11
0

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.

Vani Gupta
  • 505
  • 6
  • 13
0

Check $GIT_DIR. I did not know that it has a special meaning to git and set it to my git repo path. Then git did not work anymore.

Galdor
  • 1,636
  • 4
  • 23
  • 37