简体   繁体   中英

Why does git not recognise my local repository?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM