Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to recover from a Git remote repository deleted

We had a Gitlab CE on a vm in our server.
There were 3 people working on a single repository on this Gitlab server.
This Gitlab CE vm has been deleted accidentally! These 3 people still have their local repositories with a lot of branches.
Our branching strategy was like this:
We had a Master branch and some feature branches per user.
users used to:

  • pull master from remote,
  • make a branch from it,
  • make changes,
  • push to master again (via merge requests)

Now, I have some questions:

  1. Is there any way-strategy to recreate-rebuild remote repo from local ones?
  2. If not, what should I do to create another remote repo and merge all commits to it?
like image 449
MeeDNite Avatar asked Feb 25 '17 07:02

MeeDNite


People also ask

How do I restore a deleted git repository?

In the top right corner of GitHub.com, click your profile photo, then click Your organizations. Next to the organization, click Settings. In the left sidebar, click Deleted repositories. Next to the repository you want to restore, click Restore.

Is it possible to recover deleted git branch?

Yes, it is possible. By using git reflog find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha]. And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.

How do I undo a delete in GitHub?

If you have deleted the file and already committed the changes, you need to use the ` git checkout` command to restore the file. First, you need to find out the checksum of the commit that deleted the file, and then check out the file from the previous commit.


2 Answers

  • Create an empty repo in GitLab/BitBucket/GitHub.

  • Add a new remote (say, another) in your current repo with the URL of the new repo. Then push your master branch commits/changes to another repo's master branch.

    $ git remote add another <new-repo-url>
    $ git remote -v                 # see if the remote is added correctly 
    $ git checkout master
    
    $ git push another master       # push the changes to another/master
    
  • If you need features of another branch (say, feature) then simply checkout to the feature branch and push the changes to another repo's feature branch.

    $ git checkout feature
    $ git push another feature      # push changes to 'another' repo's 'feature' branch
    
  • Push all the branches and tags to another repo.

    $ git push --all --tags another
    

N.B. Here, another represents your new repo's URL.

like image 112
Sajib Khan Avatar answered Nov 05 '22 18:11

Sajib Khan


Since your remote was deleted you don't have any origin yet so you will have to checkout the "remote" branches locally and assign them the original name and than push all the branches to the remote.

If you dont want the local branches simply push the ones you need.

Here is a scrip which im using to checkout all branches and than push them to the new remote

#!/bin/bash

# add the new origin 
git remote add origin2 <url>

# loop over all the original branches and set the new remote as the new track origin
for branch in `git branch -a | grep remotes | grep -v HEAD | grep -v master `; do
    git branch --track ${branch#remotes/origin2/} $branch
done

# now push all branches and tags
git push origin2 --all    
git push origin2 --tags

What does the script do?

git branch -a
get a list of all the local branches

| grep remotes The branch names are : 'remotes/origin/' so this will remove the remotes from the branch names

| grep -v HEAD | grep -v master
remove the master (current branch) and HEAD which is an alias to the latest commit

like image 29
CodeWizard Avatar answered Nov 05 '22 16:11

CodeWizard