簡體   English   中英

如何從已刪除的 Git 遠程存儲庫中恢復

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

我們在我們服務器的虛擬機上有一個 Gitlab CE。
有 3 人在此 Gitlab 服務器上的單個存儲庫上工作。
這個 Gitlab CE vm 被意外刪除了! 這 3 個人仍然有很多分支的本地存儲庫。
我們的分支策略是這樣的:
我們每個用戶都有一個主分支和一些功能分支。
用戶曾經:

  • 從遠程拉主,
  • 從中做一個分支,
  • 做出改變,
  • 再次推送到主(通過合並請求)

現在,我有一些問題:

  1. 是否有任何方法策略可以從本地重新創建-重建遠程存儲庫?
  2. 如果沒有,我應該怎么做才能創建另一個遠程存儲庫並將所有提交合並到它?
  • 在 GitLab/BitBucket/GitHub 中創建一個空倉庫。

  • 使用新存儲庫的 URL 在當前存儲庫中添加一個新的遠程(例如another )。 然后將您的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
  • 如果您需要另一個分支的feature (例如, feature ),那么只需checkoutfeature分支並將更改推送到another庫的feature分支。

     $ git checkout feature $ git push another feature # push changes to 'another' repo's 'feature' branch
  • 將所有branchestags推送到another倉庫。

     $ git push --all --tags another

注意這里, another代表你的新倉庫的 URL。

由於您的遙控器已被刪除,您還沒有任何來源,因此您必須在本地簽出“遠程”分支並為其分配原始名稱,然后將所有分支推送到遙控器。

如果您不想要本地分支,只需推送您需要的分支。

這是我用來檢查所有分支並將它們推送到新的遠程的腳本

#!/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

腳本有什么作用?

git branch -a
獲取所有本地分支機構的列表

| grep remotes | grep remotes分支名稱是:'remotes/origin/' 所以這將從分支名稱中刪除遙控器

| grep -v HEAD | grep -v master
刪除 master(當前分支)和HEAD ,這是最新提交的別名

您可以復制(或克隆 --bare)本地存儲庫之一並重新設置/刪除未合並到遠程存儲庫的提交。 之后,您可以將其用作遙控器。

我不小心刪除了“起源”上的分支。 但我仍然有本地副本。 這是我在“原點”上重新創建分支的方法:

當我切換到本地副本時,我看到了這條消息:

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)

我用了:

git branch --unset-upstream

這使分支恢復到正常的未推送狀態。 為了讓它回到原點,我只是跑了

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM