Search code examples
gitgitlabbackuprestore

How to recover a gitlab repository from a git bundle?


By accident, I forcefully pushed from my local repository to the GitLab repository having mirroring active which then deleted all the hidden and GitLab related refs (for the merge request 'refs/merge-request') in the remote.

I told my coworkers to stop interacting with the remote repository and asked OPs if I could get the daily backup for that repository. I received a repositoryname.bundle file.

Now how can I recover the remote with this bundle file?


Solution

  • I solved it on my own like this.

    Create a temporary directory and cd there

    cd $(mktemp -d)
    

    Get the bundle from your OPs (full snapshot) of the repository there

    mv /tmp/yourrepo.bundle .
    

    Clone the broken repository from GitLab to BROKEN

    git clone --mirror URL_to_yourrepo BROKEN.git
    

    cd in BROKEN and run git bundle verify ../reponame.bundle. It should not report any errors, if so, continue

    cd BROKEN.git
    git bundle verify ../yourrepo.bundle
    

    Go back and then clone a new repository from the bundle file

    cd ..
    cd git clone --mirror yourrepo.bundle LASTKNOWNGOOD.git
    

    cd there and verify that all your refs are there as u expect them to be

    cd LASTKNOWNGOOD.git
    git show-ref
    

    Now set the repositories remote to the local BROKEN clone from GitLab

    git remote add BROKEN ../BROKEN.git
    

    Then push the contents of here to the BROKEN remote

    git push --tags --force --mirror BROKEN
    

    By then, the BROKEN repository should be healed. cd into the BROKEN and simulate a push to verify that it will do what you expect

    cd ../BROKEN.git
    git push --tags --verbose --dry-run --mirror origin
    

    If it looks like u expect it to be, run without --dry-run to heal the remote repository. It may report about remote rejected but as long as they are from the keep-around refs group you can safely ignore them. You also need to reopen all the automatically closed merge request in GitLab for that repository.