简体   繁体   English

如何从已删除的 Git 远程存储库中恢复

[英]how to recover from a Git remote repository deleted

We had a Gitlab CE on a vm in our server.我们在我们服务器的虚拟机上有一个 Gitlab CE。
There were 3 people working on a single repository on this Gitlab server.有 3 人在此 Gitlab 服务器上的单个存储库上工作。
This Gitlab CE vm has been deleted accidentally!这个 Gitlab CE vm 被意外删除了! These 3 people still have their local repositories with a lot of branches.这 3 个人仍然有很多分支的本地存储库。
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?如果没有,我应该怎么做才能创建另一个远程存储库并将所有提交合并到它?
  • Create an empty repo in GitLab/BitBucket/GitHub.在 GitLab/BitBucket/GitHub 中创建一个空仓库。

  • Add a new remote (say, another ) in your current repo with the URL of the new repo.使用新存储库的 URL 在当前存储库中添加一个新的远程(例如another )。 Then push your master branch commits/changes to another repo's master branch.然后将您的master分支提交/更改推送到another库的主分支。

     $ 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.如果您需要另一个分支的feature (例如, feature ),那么只需checkoutfeature分支并将更改推送到another库的feature分支。

     $ git checkout feature $ git push another feature # push changes to 'another' repo's 'feature' branch
  • Push all the branches and tags to another repo.将所有branchestags推送到another仓库。

     $ git push --all --tags another

NB Here, another represents your new repo's URL.注意这里, another代表你的新仓库的 URL。

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 | grep remotes The branch names are : 'remotes/origin/' so this will remove the remotes from the branch names | grep remotes分支名称是:'remotes/origin/' 所以这将从分支名称中删除遥控器

| grep -v HEAD | grep -v master
remove the master (current branch) and HEAD which is an alias to the latest commit删除 master(当前分支)和HEAD ,这是最新提交的别名

You can copy (or clone --bare) one of the local repositories and rebase/delete the commits that weren't merged to the remote repo.您可以复制(或克隆 --bare)本地存储库之一并重新设置/删除未合并到远程存储库的提交。 After that you can use this one as remote.之后,您可以将其用作遥控器。

I accidentally deleted my branch on "origin".我不小心删除了“起源”上的分支。 But I still had a local copy.但我仍然有本地副本。 Here's how I recreated the branch on "origin":这是我在“原点”上重新创建分支的方法:

When I switched to the local copy, I saw this message:当我切换到本地副本时,我看到了这条消息:

Switched to branch 'my-special-branch'
Your branch is based on 'origin/my-special-branch', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)

I used:我用了:

git branch --unset-upstream

That got the branch back to a normal unpushed state.这使分支恢复到正常的未推送状态。 To get it back on origin, I just ran为了让它回到原点,我只是跑了

git push --set-upstream origin my-special-branch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM