7

I have a master branch and a dev branch for current development(local working copy). I need to merge changes from master onto my dev branch. Prior to merge I ran git stash and discovered the following error:

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

To investigate this error I ran the following commands: git status git branch git remote -v. All produced the same error fatal: Not a git repository (or any of the parent directories): .git

I also tried resetting the origin of my branch git remote set-url origin https://github.com/Connexions/oer.exports.git which produces the same result: fatal: Not a git repository (or any of the parent directories): .git

I cannot commit any changes from my local working directory onto my dev branch either.

Does anyone have any suggestions on how to correct this issue.

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Jessica Burnett
  • 731
  • 2
  • 9
  • 20

4 Answers4

17

Check the following file:

  • your_repo_dir/.git/HEAD

If this content seems corrupted, this is the issue. I had the similar issue and I fixed it by replacing the content of the HEAD file with the git commit hash of the last commit from selected branch.

Commit hashes could be found under the .git/logs/refs/heads and branch_name_file

I hope it helps.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
DarkSideOfTheMoon83
  • 702
  • 1
  • 8
  • 17
  • This was the solution when I ran into this problem. The HEAD file had disappeared. – brian d foy Apr 16 '13 at 16:27
  • This worked in our case, too. Our code dir is mounted on a mac from a VirtualBox guest via samba and frequently after waking up from sleep we get this error. The .git/HEAD is empty, and restoring it with a legit sha fixes the problem. There was also a leftover .git/index.lock that had to be removed. – apinstein Feb 13 '14 at 16:38
4

One way to get that error message is to somehow not be in the git repo working tree.
See "After git clone I get a “ fatal: Not a git repository (or any of the parent directories): .git ” error" as an illustration.

It can also be cause because of a GIT_WORK_TREE environment variable being set to the wrong path.
Or it is seen in wrappers using git repo like rvm (see this thread for example)

In both case, resetting the situation in a new clone (which you did) usually is a good way to fix the situation.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

I had this error after having problems with visual studio git plugin.

The solution was to rename my_repo_dir/.git/HEAD.lock to my_repo_dir/.git/HEAD

Looks like something crashed after locking the head file and that killed the repo.

herostwist
  • 3,778
  • 1
  • 26
  • 34
2

Had the same problem after I tried to push the branch to remote from Visual Studio. I renamed .git/HEAD.lock to .git/HEAD after checking its content. Now it works fine.

rageit
  • 3,513
  • 1
  • 26
  • 38