17

fatal: not a git repository (or any of the parent directories): .git is the error I'm getting when I try to perform any git operation on my repository. I made some changes to my project which is the repository. Then I tried git status to look over my changes and encountered this error. I Googled this error but didn't make any progress. It seems this error is most common when trying to perform git operations in a directory that's outside the repository which isn't the case for me. Also I tried checking my ./git/HEAD and ./git/logs/refs/heads both of which are empty files. ./git/logs/refs/remotes/origin/master is also empty.

So from the beginning:

  • I made an Android project in this directory on one computer.
  • I initiated a git on this directory and posted it on github.
  • I followed the usual online guides for pulling this repository onto 2 other computers.
  • Made some changes on another computer, pushed the changes to the repository.
  • Then on the original computer I successfully pulled the changes.
  • After not working on the project for a few days, I made some changes on the original computer, tried to git status and this happened.
Henke
  • 4,445
  • 3
  • 31
  • 44
xyals
  • 347
  • 1
  • 2
  • 9
  • Does this answer your question? [Git: Receiving "fatal: Not a git repository" when attempting to remote add a Git repo](https://stackoverflow.com/questions/4630704/git-receiving-fatal-not-a-git-repository-when-attempting-to-remote-add-a-git) – Henke Apr 13 '22 at 11:21

5 Answers5

19

In my case, accidentally .git/HEAD file was messed up with non unknown characters, but .git/ORIG_HEAD file was ok (it has commit id). So, I copied .git/ORIG_HEAD content to .git/HEAD file. Then restarted IDE (Aptana) and everything worked fine.

Sunny
  • 5,825
  • 2
  • 31
  • 41
Ikrom
  • 4,785
  • 5
  • 52
  • 76
  • 2
    I ran into this issue when I saw a bunch of NULL characters in the .git/HEAD file after my computer crashed. The first symptom I noticed was a complete loss of everything in the history after seeing the "'git log' failed with code 128: fatal: Not a git repository..." message. This smooth move restored things. – RacerNerd Aug 22 '17 at 15:59
  • For me also the `.git/ORIG_HEAD` was damaged, but when you restore the `.git/HEAD` with a revision you think you had, everything works again and you can quickly figure out which branch/commit you actually were on. – dCSeven Dec 23 '19 at 09:19
  • I've got into same situation after blue screen of death. In my case copying contents from `.git/ORIG_HEAD` to `.git/HEAD` did not work. Solution was to manually update content of `.git/HEAD` to e.g. `ref: refs/heads/your-branch-name`. You can find your existing branches under `.git/refs` folder (can just copy path from there). – Pavel Gurecki Jan 21 '21 at 13:27
7

Make sure you don't have a GIT_DIR or GIT_WORK_TREE environment variable set in your current session, which would point to an incorrect folder.

In doubt, try a:

cd /path/to /your/repo
git --git-dir .git --work-tree . status

If that still fails, try at least to clone that repo again from GitHub, and add your recent changes in that new repo:

cd /path/to/new/clone
git --git-dir .git --work-tree /path/to /your/repo add .

(and go on working in that new clone)

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I can't use any git commands in my repo's path. Any git commands just returns that "not a repo" error. How else can I check my GIT_DIR or GIT_WORK_TREE? – xyals Sep 22 '13 at 21:03
  • @user2804884 did your try the first git command I mentioned in my answer though? – VonC Sep 22 '13 at 21:11
  • 1
    i tried that at my repo's path and it returned the error in question – xyals Sep 22 '13 at 21:49
  • @user2804884 does your repo path still contain a `.git` folder? – VonC Sep 23 '13 at 05:45
6

I too had the same issue. The problem was my git folder was being synced by UbuntuOne cloud service. It had messed up the files by adding files appended with u1conflict. And my .git/HEAD was also messed up as .git/HEAD.u1conflict.

Are you using any cloud collaborating services on the git? Dropbox, UbuntuOne etc. Chances are that your .git folder might have got messed up in the process of syncing.

Bharathwaaj
  • 2,627
  • 2
  • 22
  • 35
1

git init solved my issue. In fact, there was not any .git folder

Nagibaba
  • 4,218
  • 1
  • 35
  • 43
0

Ensure the directory/repository name has not changed in any way from the original. I just dealt with this error, and that was the cause.

M. Thompson
  • 95
  • 11