2023-11-29 09:00:07

如何归档git分支?

我的git存储库中有一些旧的分支,它们不再处于活跃的开发状态。我想存档这些分支,这样它们在运行git branch -l -r时默认不会显示。我不想删除它们,因为我想保留历史。我该怎么做呢?

我知道可以在refs/heads之外创建一个ref。例如:refs/archive/old_branch。这样做有什么后果吗?


当前回答

步骤0。检查以确保工作树是干净的,以免 失去工作:

git status

步骤1。从本地存储库的根签出分支和 然后将其标记为存档:

git checkout <branch_name> && git tag archive/<branch_name>

步骤2。上传标签到远程,并确保在另一个分支之前 继续,例如main:

git push origin --tags && git checkout main

步骤3。从本地和远程存储库中删除分支:

git branch -D <branch_name> && git push origin -d <branch_name>

在哪里您应该替换<branch_name>与分支的名称 要存档,并将原点与远程存储库的名称联系起来 如果不是原点。

评论:

在第1步之前和之后,你可能想要运行git标记来注意 添加标签。 在第三步之前和之后,你可能想要观看 https://github.com/ < github-username > / < github-repository-name > /分支 和/或运行git branch -a来注意这个分支是如何被删除的。 恢复一个分支:

git checkout -b <branch_name> archive/<branch_name>

紧随其后的是

git push --set-upstream origin <branch_name>

引用: https://gist.github.com/zkiraly/c378a1a43d8be9c9a8f9 https://dev.to/clsource/archiving-git-branches-3k70

其他回答

下面是它的别名:

arc    = "! f() { git tag archive/$1 $1 && git branch -D $1;}; f"

这样加起来:

git config --global alias.arc '! f() { git tag archive/$1 $1 && git branch -D $1;}; f'

请记住,git已经有了archive命令,所以不能使用archive作为别名。

你还可以定义alias来查看“存档”分支的列表:

arcl   = "! f() { git tag | grep '^archive/';}; f"

关于添加别名

我使用以下别名隐藏归档的分支:

[alias]
    br = branch --no-merge master # show only branches not merged into master
    bra = branch                  # show all branches

因此,git br显示正在开发的分支,git bra显示所有分支,包括“存档”的分支。

步骤0。检查以确保工作树是干净的,以免 失去工作:

git status

步骤1。从本地存储库的根签出分支和 然后将其标记为存档:

git checkout <branch_name> && git tag archive/<branch_name>

步骤2。上传标签到远程,并确保在另一个分支之前 继续,例如main:

git push origin --tags && git checkout main

步骤3。从本地和远程存储库中删除分支:

git branch -D <branch_name> && git push origin -d <branch_name>

在哪里您应该替换<branch_name>与分支的名称 要存档,并将原点与远程存储库的名称联系起来 如果不是原点。

评论:

在第1步之前和之后,你可能想要运行git标记来注意 添加标签。 在第三步之前和之后,你可能想要观看 https://github.com/ < github-username > / < github-repository-name > /分支 和/或运行git branch -a来注意这个分支是如何被删除的。 恢复一个分支:

git checkout -b <branch_name> archive/<branch_name>

紧随其后的是

git push --set-upstream origin <branch_name>

引用: https://gist.github.com/zkiraly/c378a1a43d8be9c9a8f9 https://dev.to/clsource/archiving-git-branches-3k70

我有时会这样归档分支:

生成补丁文件,例如format-patch <branchName> < firstthash >^..<lastHash>(使用git日志获取firstthash和lastHash) 将生成的补丁文件移动到文件服务器的目录下。 删除分支,例如git分支-D <branchName>

当你需要再次使用分支时,“应用”补丁;然而,根据目标分支的状态,应用补丁文件(参见git am)可能具有挑战性。从好的方面来看,这种方法的好处是允许分支的提交被垃圾收集,并节省了repo中的空间。

要归档超过n个月或n年的分支,运行这个bash脚本。

#!/bin/bash

# Average days in a month - 30.436875
# Hours in a day 24
# Minutes in an hour 60
# Seconds in a minute 60
months_in_year=12
days_in_month=30.436875
hours_in_day=24
minutes_in_hour=60
seconds_in_minute=60

# Input is x months or x years
# $1 an integer
# $2 a time metric

# Name of the script, follows through simlinks
script_name="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"

if [ $# -le 1 ]; then
    echo "Usage: ./${script_name} [1-9] [month(s)/year(s)]"
    exit 1
fi

time_period=$1
time_metric=$2

if [[ ${time_metric} =~ "month" ]]; then minimum_branch_age_in_seconds=$( echo "scale=4; $time_period * $days_in_month * $hours_in_day * $minutes_in_hour * $seconds_in_minute" | bc); fi
if [[ ${time_metric} =~ "year" ]]; then minimum_branch_age_in_seconds=$( echo "scale=4; $time_period * $months_in_year * $days_in_month * $hours_in_day * $minutes_in_hour * $seconds_in_minute" | bc); fi

echo "minimum_branch_age: $1 $2"
echo "minimum_branch_age_in_seconds: ${minimum_branch_age_in_seconds%.*}"

git for-each-ref refs/remotes | while read commit type ref;do
    current=$(date +%s)
    headcd=$(git log -1 --pretty=%cd --date=format:%s ${commit})
    age_in_seconds=$((current-headcd))
    if [[ ${age_in_seconds} -ge ${minimum_branch_age_in_seconds%.*} ]];then
        branch=$(echo $ref | sed 's=refs/remotes/origin/==g')
        age_in_months=$( echo "scale=4; $age_in_seconds / $days_in_month / $hours_in_day / $minutes_in_hour / $seconds_in_minute" | bc)
        echo "archiving $branch - age in seconds - $age_in_seconds - age in months - $age_in_months "
        git tag archive/${branch} ${branch}
        git push -d origin ${branch}
        git branch -D ${branch}
        echo "Unarchive with: git checkout -b ${branch} archive/${branch}"
    fi
done

谢谢杰里米炖的肉。