6

Is there a way to clone a remote git repo while keeping any unreachable commits that may be in that repo?

The scenario is this: I need to recover a branch that was deleted from a bare git repo on a server that I maintain. I have the hash of the last commit to this branch, but The regular git commands do not seem to be able to function on bare repos, so as far as I am aware there is not any way I can recover it directly on the server.

I also do not have access to any non-bare repos that contain this commit. If I did, I could recover it easily by checking out the commit hash and then creating a branch from it. This is why I would like to be able to clone this repo while keeping any unreachable commits in it, so I can do exactly that.

Chris L
  • 4,270
  • 1
  • 17
  • 12
  • For what it's worth, many (not all) git commands *do* work on bare repos, e.g., you can `git show `, `git branch `, etc. The `git branch` method would be the quickest way to fix the problem, assuming the commit is still actually there. – torek Aug 21 '14 at 00:56
  • @torek I had already fixed my problem, but I tried this out anyway. It does work! The exact syntax I used was `git branch `. This form of the command evidently works fine on bare repos. Thanks! – Chris L Aug 21 '14 at 17:36

1 Answers1

5

What you want to do is:

  1. Copy the bare repo to another folder using normal file copy commands.
  2. Convert the bare repo copy to a normal repo.
  3. Do what you need to do to locate the commit object you seek.
  4. Create a new branch at the lost commit once its located.
  5. Add the original bare repo as a remote.
  6. Push the new branch up to the bare repository.
Community
  • 1
  • 1
Carl
  • 43,122
  • 10
  • 80
  • 104
  • Hi, I don't maintain the server (BitBucket), but I really need to get to an orphaned commit. This commit was on a branch that nobody has a copy of. I can see it in BB, but cannot fetch it or clone it. Please help =) – XedinUnknown Sep 21 '15 at 14:11
  • 4
    If you can see the commit, then you know its hash. You can fetch it using `git fetch origin :refs/remotes/origin/orphaned-commit` – Carl Sep 25 '15 at 19:17
  • @Carl the "git fetch origin..." command worked for me with GitHub, you should probably add to your answer! – Ben Dec 28 '22 at 19:24