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.
Dan
  • 229
  • 2
  • 6
  • 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? – Joachim Sauer Mar 27 '21 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. – Asif Kamran Malick Mar 27 '21 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 Mar 27 '21 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 Mar 27 '21 at 12:10
  • @AD7six Ok I'll have a look... – Dan Mar 27 '21 at 12:12
  • My current plan is - delete local folder and re-clone to bring in the last pushed code (a few days old). Replace older files with backed up copy of most recent files and commit, then push... will that cause any issues? – Dan Mar 27 '21 at 12:30
  • 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 Mar 27 '21 at 13:03

1 Answers1

6

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

AD7six
  • 63,116
  • 12
  • 91
  • 123
  • 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 Mar 27 '21 at 12:54
  • You can check out to a branch using `git checkout master` to get onto a branch. That should move you forward. – Noufal Ibrahim Mar 27 '21 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 Mar 27 '21 at 13:00
  • To recover from the next problem, best to treat as a new question - it's already been answered before :) https://stackoverflow.com/questions/4735556/git-not-currently-on-any-branch-is-there-an-easy-way-to-get-back-on-a-branch - – AD7six Mar 27 '21 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 Mar 27 '21 at 13:30
  • For reconnecting to existing remote repo see here - https://stackoverflow.com/questions/66832049/reconnect-git-local-to-github-remote-without-losing-a-new-local-branch-and-commi – Dan Mar 27 '21 at 20:55
  • @AD7six Can you believe something has gone wrong again! Jumped on this morning and getting the same error! Now `ls .git` doesn't even show a HEAD or ORIG_HEAD? Something is messing with my system. ```$ ls .git objects/ packed-refs refs/ ``` – Dan Mar 29 '21 at 23:42
  • I've added a section to the answer about that, but you really need to figure out how that file is being deleted - it won't be git doing it :). Pushing frequently to the remote will ensure you don't lose any work (since even if you lose everything locally, you can clone again). – AD7six Mar 30 '21 at 07:37
  • @Dan *Something is messing with my system* is a typical symptom if you have your directory synchronized with a cloud service. Doing that is a BIG no-no for a Git repository. – j6t Mar 30 '21 at 07:45
  • @j6t @AD7six I might have found the answer!... My antivirus (bitdefender) has blocking connections to unknown sites like `gitcdn.link` and `gitcdn.xyz` over the last. We have a number of laptops shared with family so I thought one of the other laptops was infected with something. Turns out it's a recent cloudfare issue with `uBlock Origin` caching GitHub connections (which I have installed) https://www.reddit.com/r/antivirus/comments/m936ew/bitdefender_and_gitcdn/gs1yaxt?utm_source=share&utm_medium=web2x&context=3 I think GitHub desktop might have been trying to connect and having issues. – Dan Mar 30 '21 at 11:18
  • (In short Bitdefender is fire-walling a lot of git related connections so I'm guessing it's messing with my git setup somehow... still not 100% sure) – Dan Mar 30 '21 at 11:28