5

I just jumped back into a project that I've been using Git on for about 6 months and saw this:

$ cd /d/DEVELOP/BlenderAe # My repo root
$ git status
fatal: not a git repository (or any of the parent directories): .git

That's definitely the right path, why does git not recognise my local clone?

My last actions were:

  • create local branch
  • work on local branch (switched to local branch in VSCode... culprit?)...
  • saved but not pushed to remote (I'll never miss this step again! Argh!).

The git folder contents are as follows:

$ cd /d/DEVELOP/BlenderAe # My repo root
$ ls .git
ORIG_HEAD  objects/  refs/

I do have the current code (backed it up, outside of git). How do I reconnect? And also to my github remote?


After fixing ORIG_HEAD I see:

$ git status
Not currently on any branch.
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    (old_file1).py
        ...all the older files that were previously renamed/deleted are listed here for deletion.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Current_file1.py
        ...all the current files are listed here as untracked.
7
  • 1
    My guess (and probably most common reason for this): you think you're in the right directory but for one reason or another you are not. Can you post the output of pwd and git status in the git shell (including the shell prompt) and maybe a screenshot of the folder in Explorer? Commented Mar 27, 2021 at 12:05
  • Please double check that you are running git commands from within the git repo. i.e. pwd should return correct path. Is it a multi-module project? If you want to reconnect, just check you have correct remotes configured by running git remote -v. If remote is not set or missing, try adding the remote first. Commented Mar 27, 2021 at 12:06
  • pwd - /d/DEVELOP/BlenderAe git status - ...MINGW64 /d/DEVELOP/BlenderAe $ git status fatal: not a git repository (or any of the parent directories): .git
    – Dan
    Commented Mar 27, 2021 at 12:08
  • 1
    please show, do not describe, things like this 'I can also see the hidden .git folder in the project folder' - e.g. edit the question to show ls .git; git status. please do not answer requests for info with a comment.
    – AD7six
    Commented Mar 27, 2021 at 12:10
  • 2
    My current plan is you can, for sure do that . It is worth IMO using the situation as a learning experience - one you get confidence recovering from any kind of 'git disaster' - the next time it's just not a problem.
    – AD7six
    Commented Mar 27, 2021 at 13:03

1 Answer 1

7

.git/HEAD is misssing

ls .git shows ORIG_HEAD objects/ refs/

That's incomplete, compare:

$ git init
$ tree .git
.git
├── HEAD
├── config
├── description
├── hooks
│   ├── applypatch-msg.sample
│   ├── commit-msg.sample
│   ├── fsmonitor-watchman.sample
│   ├── post-update.sample
│   ├── pre-applypatch.sample
│   ├── pre-commit.sample
│   ├── pre-merge-commit.sample
│   ├── pre-push.sample
│   ├── pre-rebase.sample
│   ├── pre-receive.sample
│   ├── prepare-commit-msg.sample
│   └── update.sample
├── info
│   └── exclude
├── objects
│   ├── info
│   └── pack
└── refs
    ├── heads
    └── tags

8 directories, 16 files

The file missing which git is looking for to detect the git repo is HEAD.

$ mv .git/HEAD .git/ORIG_HEAD
$ git status
fatal: not a git repository (or any of the parent directories): .git

Recovering ORIG_HEAD

The presence of ORIG_HEAD suggests git was left in the act of doing something destructive, to recover from that (or possibly to see the next problem 🤞not):

$ mv .git/ORIG_HEAD .git/HEAD 
$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)

Recovering from scratch

If the HEAD file is missing and there isn't an ORIG_HEAD file - that's still recoverable if that's the only problem. The HEAD file is plain text and indicates what branch is currently checked out:

Usually the HEAD file is a symbolic reference to the branch you’re currently on

example:

$ cat .git/HEAD
ref: refs/heads/main

Creating .git/HEAD with that content (replacing main with any valid branch name) will allow operating on the git repo, if the other contents of the git repo (the .git/objects and .git/refs folder contents) still exist.

11
  • Thank you! That at least got me somewhere... but now git status - Not currently on any branch ? (plus it's listed the current files as untracked and changes to be committed has old files to be deleted (were already deleted previously))
    – Dan
    Commented Mar 27, 2021 at 12:54
  • You can check out to a branch using git checkout master to get onto a branch. That should move you forward. Commented Mar 27, 2021 at 12:59
  • I've updated the question, can you add what you see to the question please. most likely you just need to check out the branch you were previously on. if you didn't already, you can just copy the whole project folder to fix the repository (either copy) without risking losing changes. Also git reflog shows what git actions were done previously.
    – AD7six
    Commented Mar 27, 2021 at 13:00
  • To recover from the next problem, best to treat as a new question - it's already been answered before :) stackoverflow.com/questions/4735556/… -
    – AD7six
    Commented Mar 27, 2021 at 13:15
  • Thanks again @AD7six ...just for anyone else with this issue, I ended up deleting the untracked files (basically all the files besides the .git folder) and git checkout to my branch... that brought me back to the last successful commit on that branch (which was not the most recent code), so I simply copied my backup files over the top of the existing ones and commited... now my issue is that the local project is disconnected from the remote (working on that).
    – Dan
    Commented Mar 27, 2021 at 13:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.