我正在为本地 git 存储库做一个备份脚本。 我查看了各种可能性,然后选择 bundle 来完成任务。 我采取的步骤:
创建一个新的 repo,进行初始提交
当我使用
git branch -a
检查 repo 时,我得到以下信息:* 大师
git bundle create ./test.bundle --all
现在,当我使用 git bundle list-heads
检查包时
我得到 2 个引用:1 个用于 HEAD,另一个用于 refs/heads/master。
当我使用 git clone
将这个包 pull 入一个新的存储库时,分支看起来像这样:
*master
remotes/origin/HEAD -> remotes/origin/master
remotes/origin/master
为什么会这样?有没有办法只导入第一个存储库中没有 Remote 的分支?
编辑:
我的问题可能有点不清楚。这是我想要实现的目标:
- 有一个包含 2 个分支的 repo,master 和 test。
- 捆绑所有分支(按照建议使用
git bundle --branches
完成) rm
整个 repo- 使用 git clone 恢复 repo。不幸的是,我必须提供一个分支参数,因为没有它我会收到以下错误:
警告:远程 HEAD 引用不存在的 ref,无法 check out 。
出现的唯一问题是我在克隆后得到以下分支:
*master
remotes/origin/master
remotes/origin/test
切换到测试后,我收到一条消息,提示已创建一个新分支。有没有办法克隆所有分支,使其看起来像原始存储库?
*master
test
最佳答案
当您从包中克隆时,包中的分支将看起来像克隆中的 Remote 。这很正常。
尽管 git bundle list-heads
显示了原始存储库的远程引用,但您不会在新克隆中看到它们。我的意思是例如:
$ git bundle list-heads test.bundle
a742ee94e8fcef80eb5619f76674bb79d7011742 refs/heads/test2
a8bf0cf603a686ccb75179c64b3392d50ff2f4af refs/heads/master
a8bf0cf603a686ccb75179c64b3392d50ff2f4af refs/remotes/origin/HEAD
a8bf0cf603a686ccb75179c64b3392d50ff2f4af refs/remotes/origin/master
a8bf0cf603a686ccb75179c64b3392d50ff2f4af refs/remotes/origin/test1
a8bf0cf603a686ccb75179c64b3392d50ff2f4af HEAD
然后从这里克隆:
$ git clone test.bundle clone2
$ cd clone2
$ git branch -a
* master
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/test2
也就是说,没有原始 Remote 及其 test1
分支的迹象。
您可以创建一个没有远程引用的包,如下所示:
$ git bundle create test.bundle --branches HEAD
但我不确定这是否会产生重大影响。不过,我从未使用过 bundle ,所以也许可以改进这个答案。
更新
当您从 bundle 克隆时,从您的新克隆的角度来看,bundle 被视为一个远程的新源。克隆时,只为 HEAD 创建一个本地分支,其他分支留在原点。如果你想完全摆脱 bundle,你可以为 bundle remote (= origin) 中的所有分支创建本地分支,然后删除 remote 并删除 bundle,像这样:
for b in $(git branch -r | grep -v HEAD | cut -f2 -d/); do
git show-ref --verify --quiet refs/heads/$b || git checkout $b
done
在运行此之前,请确保您处于干净状态,没有未决或暂存的更改。
关于Git bundle 添加远程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20446528/