有啥方法可以将所有 git 分支变成存储库的文件夹?

Posted

技术标签:

【中文标题】有啥方法可以将所有 git 分支变成存储库的文件夹?【英文标题】:Any way to turn all git branches into folders for a repository?有什么方法可以将所有 git 分支变成存储库的文件夹? 【发布时间】:2020-12-02 12:24:18 【问题描述】:

我在一个 git repo 中有几个分支。

-repo
  -- branch1
  -- branch2

出于某种原因,我需要将回购移动到另一个没有历史记录的回购中。由于 branch1branch2 的代码不同,所以如果我可以在新位置的 repo 内自动创建分支到文件夹映射,这对我会有帮助。 有什么办法吗?

实际问题是我在 GitHub 中有一个名为 archives 的 git repo,我需要在其中保留几个带有可浏览源代码的 repo。 我手上还有哪些其他选择?

【问题讨论】:

man git-bundle 【参考方案1】:

我不确定是否存在仅使用 git 的解决方案,因为这不是该工具的目的。但是,基本命令存在,您可以在此基础上构建自己的脚本。

你需要:

    列出远程分支:git ls-remote --heads origin 只克隆一个分支名称:git clone -b BRANCH_NAME --single-branch REMOTE_URL DESTINATION

使用这两个命令,您可以像这样在 bash 中自动执行该过程:

#!/usr/bin/env bash

####
#
# $1 = Remote URL to be clonned
# $2 = New name of the repository
#
####

remote=$1
repository=$2
workdir=$(pwd)
destination="$workdir/$repository"

echo "Creating repository at $destination"
mkdir "$destination"

git clone "$remote" "$repository"
cd "$repository"

for branch in $(git ls-remote --heads origin | grep -o "refs/heads/.*$" | cut -d / -f 3-)
do
        echo "Cloning into branch $branch"
        git clone -b $branch --single-branch $remote "$destination/$branch"
done

echo "Current dir: $(pwd)"
echo "Deleting all .git and .gitignore files"

rm -rf "$destination"/*/.gitignore
rm -rf "$destination"/*/.git
rm -rf "$destination"/.gitignore
rm -rf "$destination"/.git

cd $workdir

echo "All done! cur dir: $(pwd)"

将这段代码保存在像my-repo-cloner.sh 这样的文件中,您可以使用./my-repo-cloner.sh REMOTE_URL MYREPO 克隆您的repo(具有您想要的结构)

【讨论】:

谢谢,是否可以创建一个带有 repo 名称的文件夹并将所有分支文件夹移动到里面。还需要完全删除 .git,因为我想在新存储库中推送代码。

以上是关于有啥方法可以将所有 git 分支变成存储库的文件夹?的主要内容,如果未能解决你的问题,请参考以下文章

如何将分支内容移动到另一个存储库保留历史记录并避免复制原始存储库的完整历史记录?

有啥方法可以让多个设备(人)在数据块中同时在不同的分支中工作?

Git:在分支中创建子模块

如何请求提交到开源 git 存储库的权限? [复制]

将本地新工程与 git 关联

如何将代码从新的本地文件夹推送到现有 Github 存储库的主分支并保留提交历史记录?