1

As a means to a backup, I push a folder structure to a GIT repository by committing new changes every once in a while. In this case, it's most parts of my Linux user's $HOME folder. As the backup also includes local folders of various applications (such as mailing client, browsers, ...), naturally, even seconds after a recent git-push there is a whole bunch of new and uncommitted changes.

As it happens, I accidentally destroyed my ~/.git/ folder (overwrote it with a ~/.git/ folder from another repository, so it's really gone). I'd like to recover it by sort-of-cloning the GIT history from the remote and retain all files' connection to their repository counter-part, yet don't want the local files overwritten by git-clone or git-pull. I tried to recover the .git/ folder by following this answer, using the git fetch then git reset version. All four commands finished without errors. However, as far as my local repository is concerned, I get stuck in kind of a limbo:

  • git branch -av shows the correct remote master with the correct last commit there - but only that; there is no local branch/master listed.

  • git status shows:

    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        [...]
    

    The local files changed since the last commit and git-push do not get listed.

  • git log shows: fatal: your current branch 'master' does not have any commits yet

  • I tried stashing local changes in order to git-pull and re-apply the stash; however, git stash save results in:

    fatal: bad revision 'HEAD'
    fatal: bad revision 'HEAD'
    fatal: Needed a single revision
    You do not have the initial commit yet
    
  • git checkout master aborts with:

    error: The following untracked working tree files would be overwritten by checkout:
        [...]
        [... looooong list of files and folders ...]
        [...]
    Please move or remove them before you can switch branches.
    Aborting
    
  • git rev-parse --abbrev-ref HEAD results in "HEAD" – it should output a branch name or "master" if everything was alright (I use this command to have the current branch name displayed in bash's prompt).

Can someone point me into a helpful direction as to how to "recover" the local repository and all file references without overwriting (uncommitted) changes? What seems to be missing in the answer from the other topic?

  • 1
    Clone the repository to a separate folder, check out the branch you want to continue on, this will prepare the .git folder with all the right references. Then you move the .git folder from this separate folder into the right place, and only the .git folder. If you now do a `git status` in the real folder you'll get status as though you just changed all the files to what they currently are, compared to what the repository think they should be. Then you should be able to just pick up where you left off. – Lasse V. Karlsen May 23 '18 at 18:10
  • Thank you, Lasse and VonC. Yes, I considered that, but hoped it wouldn't have to come to this as I fear my disk space running out while cloning. :D – R. Cocinero May 24 '18 at 15:40

1 Answers1

1

One trick is to consider your current broken repo (the one with the missing .git) as a working tree (where your modified files reside)

Clone the repo elsewhere, and then add your files from the first working tree into your second cloned repo:

cd /path/to/second/cloned/repo
git --work-tree=/path/to/first/repo add .
git commit -m "import work in progress"
git push
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250