16
devTea
4y

Accidentally rm -rf a git repo, one of the branch still not pushed to remote yet. I managed to use file recovery software to recover the .git folder, any idea what to do next?

Comments
  • 6
    From a clone with full history: git push <remote> --all

    That's it
  • 2
    Check the reflog
  • 1
    @NeatNerdPrime @gronostaj the repo folder has been deleted, I just managed to recover the .git folder. Copied the objects and refa folder to a fresh clone .git folder doesn’t worked
  • 1
    You should be able to execute some git commands inside the .git folder IIRC.
  • 3
    so wait, your local repo is gone, and that included a branch you were working on taht has not been committed and has been deleted...

    First i'd ensure the local git repo is back in pristine condition, so i'd simply clone from the remote first.

    then, if you're lucky, you can try to recover your file s with "trash-restore - Restore for Command line trash utility." it's simply searching with your package manager for this utility, read the manpage and start recovering your files.

    That way you will have recovered the files you were working on (the branch you accidentally deleted) and then, once you confirmed that all files have been recovered, do a git commit/push2remote and you're safe again
  • 2
    Were the changes in the branch committed?

    You can use reflog, to create a branch using a commits sha1 and restore it that way.

    Otherwise, well... I have some bad news.
  • 1
    @gronostaj when I checkout to the branch that hasn’t been pushed it shows error non monotonic index
  • 1
    @NeatNerdPrime @C0D4 I have commited to that branch, it just hasn’t been pushed to remote, how do I read branch commits from refs/heads branch name?
  • 2
    @devTea Then the .git might have been a little corrupted.
  • 1
    What to take from this:
    - don't -f on rm. Never.
  • 3
    @devTea backup the current .git

    Drop in your old .git and check your branches

    # git branch

    If the old branch is listed, you should be fine to

    # git checkout oldBranch

    Otherwise you can recreate the branch

    # git checkout -b oldBranch

    and

    # git reflog

    If it has tracked your commits, run

    # git reset --hard HEAD@{1}

    1 being the index of the commit you want to roll back too on the current branch.

    It that doesn't work, this is a one of those learning moments of why to push regularly 😉
  • 3
    @C0D4 yeah the branch name exist but I can’t checkout to that branch ☠️
  • 1
    @devTea Ok to avoid any confusion, which end is "pristine"? the local repo or the remote?

    Reading your last comment i ass-u-me it's the local repo that is pristine and the remote is broken.

    If that is the case it's not terrible to delete an end of a repo, since every clone of a git repo contains the full history of the project. It's the main property of git, every clone has the full history project.

    Knowing this, it's just a matter of cleaning the location of the remote, no need to "recover" files since your local copy of the repo contains everything.

    On your remote just re-init your repo (git init) and do a "git push <remote> --all" and you should be good!
  • 2
    @devTea what's the error?
  • 1
    @NeatNerdPrime the local got deleted with a branch that hasn’t been pushed to remote, I managed to restore the .git folder using file recovery software but seems like it restore all of the past repo commit. I just need a way to track my commits on the branch in that .git folder
  • 2
    @C0D4 bad config file in line 1, when I used the remote config file and copy all of the .git/objects and .git/refs folder to the new cloned repo it shows error non monotonic index
  • 3
    @devTea sounds like .git is corrupt. is it on MacOS by any chance, not judging its probably has ._ files in there somewhere

    Either way, i'd try following this, best to backup that project as things may go screwy.
    https://stackoverflow.com/a/...

    and yes, i have this bookmarked for those bad days where things or dev servers shit themselves.
  • 1
    @devTea Ah, in that case, please see what i said half hour ago, and try to:

    * clone from remote in pristine condition, ensure your local repo is clean, you can use "git fsck" for that (it will check integrity of your repo)

    * Create a NEW branch, name it "recovery" or something, be poëtic creative

    * use the "trash-restore" CLI utility (assuming you run linux on your machine) to try to recover as many files as possible. If you use another OS, use any file restore utility fit for that OS

    * While doing this do NOT overwrite any part of your .git folder , since that is now "pristine". Just concern yourself about the lost source code files.

    * if you have recovered your source code files, do check your files, and if all is well, commit the files into the branch

    * Push everything to remote.

    * Do a sigh of relief, everything si back okay now.
  • 1
    Do you still have the relevant files in your text editor's buffer by any chance?
  • 1
    @electrineer where does vscode buffer located?
  • 2
    @devTea idk, by luck you could still have the files open in it. Sublime has saved me that way.
  • 2
    @C0D4 @NeatNerdPrime will try both of your method

    Tldr for future reader: how to recover old repo git objects to fresh cloned repo
  • 2
    @devTea Please take my advise "as-is" without any warranty!

    If shit hits the fan, recover from backups if possible... you do make backups, right?
  • 1
    @NeatNerdPrime for sure if it's so fresh it hasn't even been pushed
  • 1
  • 1
    @pythonInRelay or just always push after commit
  • 2
    You can "git clone" from the .git folder (e.g. git clone file:///path/to/.git).
Add Comment