git - 如何在克隆后没有链接的情况下使用 bundle 创建 git 存储库备份

标签 git

有没有一种方法可以使用 git bundle 创建备份,在从备份克隆后没有到 bundle 文件的链接?我想将我的存储库备份到一个包含所有历史记录的文件中,并在以后准确地恢复它们。

目前,我正在使用以下方法创建备份:

$ mkdir here                  
$ cd here                                                                       
$ git init                                                                      
Initialized empty Git repository in /tmp/here/.git/                             
$ touch stuff                                                                   
$ git add stuff                                                                 
$ git commit -m "This is a file"                                                
[master (root-commit) c867bcf] This is a file                                   
 1 file changed, 0 insertions(+), 0 deletions(-)                                
 create mode 100644 stuff                                                       
$ git bundle create myrepo.bundle --all                                         
Counting objects: 3, done.                                                      
Writing objects: 100% (3/3), 212 bytes | 212.00 KiB/s, done.                    
Total 3 (delta 0), reused 0 (delta 0)

好的,现在我们有了一个带有简单存储库的包。现在,当我们尝试从包中复制这个存储库时:

$ cd ..
$ mkdir there                                                                   
$ cd there                                                                      
/tmp/there $ git clone ../here/myrepo.bundle .                                  
Cloning into '.'...                                                             
Receiving objects: 100% (3/3), done.                                            
/tmp/there $ git remote -v                                                      
origin→ /tmp/there/../here/myrepo.bundle (fetch)                                
origin→ /tmp/there/../here/myrepo.bundle (push)

这表明我们有一个指向原始捆绑文件的链接,我不想要它。我也尝试创建一个镜像,但问题仍然存在:

$ cd ..                                                                         
$ mkdir there                                                                   
$ cd there                                                                      
$ git clone --mirror ../here/myrepo.bundle ./.git                               
Cloning into bare repository './.git'...                                        
Receiving objects: 100% (3/3), done.                                            
$ git config --bool core.bare false                                             
$ git reset --hard HEAD                                                         
HEAD is now at c867bcf This is a file                                           
/tmp/there $ git remote -v                                                      
origin→ /tmp/there/../here/myrepo.bundle (fetch)                                
origin→ /tmp/there/../here/myrepo.bundle (push) 

虽然我们可以直接复制整个存储库来备份,但我真的只想备份一个只包含当前正在修订的文件的文件。我一直在备份目录,这导致所有剩余文件一团糟。因此,我真的只想存档一个文件,这就是我使用 bundle 的原因,并且我希望从备份中恢复是干净的,因为根本没有与 bundle 文件的链接。感谢您的帮助。

最佳答案

不是从包中克隆,而是创建一个新的存储库并使用通配符 refspec 获取。我在示例中添加了第二个分支 feature 和一个标记 v1.0 以使其更具说明性:

$ mkdir there
$ git init .
$ git fetch --update-head-ok ../here/myrepo.bundle '*:*'
Receiving objects: 100% (5/5), done.
From ../here/myrepo.bundle
 * [new tag]         v1.0       -> v1.0
 * [new branch]      master     -> master
 * [new branch]      feature    -> feature
$ git checkout

*:* refspec 会将所有远程引用提取到同名的本地引用中,如果它们不存在则创建它们。 --update-head-ok 参数是必需的,因为 git init 将创建一个 master 分支,它可能也存在于远程中。

由于这是使用直接 URL 的抓取,因此根本不需要远程。请注意,这对任何类型的源 repo URL 都同样有效,而不仅仅是 bundle 。

可以通过比较两个存储库中 git show-ref 的输出来检查新旧存储库的引用是否相等,它们应该相同。

关于git - 如何在克隆后没有链接的情况下使用 bundle 创建 git 存储库备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48371418/

相关文章:

git - 无法使用 vcsrepo checkout git ssh 存储库

git - 将 HG 项目从 Bitbucket 镜像到 Github

git - 读取 GIT merge 标记

linux - Git Clean 过滤器在空文件上中断

Git子模块绝对工作树路径配置

git - 从提交列表生成 git diff

git 一次应用多个存储

c# - 来自 C# 应用程序的 git

bash - 带有用户和密码的 GIT_ASKPASS

python - 如何使用docker进行部署开发?