如何在 SHA1 和 SHA256 之间转换 Git 仓库

基本技巧是导出 git bundle,然后在新初始化的仓库中重新导入。

注意,新仓库与旧仓库不兼容 - 虽然它包含相同的提交,但此迁移策略不提供任何直接兼容性

如何从 SHA1 仓库迁移到 SHA256

git_bundle_migrate.sh
# Create new repo with SHA-256
git init --object-format=sha256 new-repo

# Move into old repo directory and create bundle
cd old-repo
git bundle create ../repo-bundle --all

# Move to new repo and unbundle
cd ../new-repo
git bundle unbundle ../repo-bundle

# Verify branches
git branch -a

如何从 SHA256 仓库迁移到 SHA1

git_bundle_migrate_back.sh
# Create new repo with SHA-1
git init --object-format=sha1 new-repo

# Move into old repo directory and create bundle
cd old-repo
git bundle create ../repo-bundle --all

# Move to new repo and unbundle
cd ../new-repo
git bundle unbundle ../repo-bundle

# Verify branches
git branch -a

Check out similar posts by category: Git