首页 > 解决方案 > 为本地浅拷贝切换 git 标签/分支

问题描述

我正在尝试在本地浅层 git 存储库中从一个标签/分支切换到另一个标签/分支。然而,我注意到一些奇怪的行为。

# Initial creation of the repository

git init localrepo
git fetch --no-tag --depth 1 <repo-url> <branch/tag>
git checkout -b shallow --no-track FETCH_HEAD

# Switching to a new branch/tag

git fetch --no-tag --depth 1 <repo-url> <branch/tag>
git reset --hard FETCH_HEAD

我不确定这是否是正确的方法——但我注意到了一些奇怪的事情。当我切换到新的分支/标签时,文件.git/shallow修订开始堆积。

有没有办法以某种方式重置 ```.git/shallow`` 文件,还是缺少其他东西?

标签: git

解决方案


By default, Git clones the entire repository and therefore has a complete list of all the objects. With a shallow clone, the repository contains revisions back to one or more shallow points, and the approach you're using creates a new shallow point.

So what you're seeing is that Git is tracking that in each of those cases, it has the objects only back to those shallow points. If you removed the shallow points, your repository would be missing objects it was supposed to have, so you don't want to remove them.

Git is not designed to delete objects that are referenced, so as long as you have something in your repository (including the reflog) referencing those objects, they won't be pruned. If you desperately need your local repository to be as small as possible, you'll need to perform a fresh shallow clone. Otherwise, there's no harm in the current approach (other than the inefficiency of fetching into a shallow clone) and you're probably fine.


推荐阅读