我的git存储库中有一些旧的分支,它们不再处于活跃的开发状态。我想存档这些分支,这样它们在运行git branch -l -r时默认不会显示。我不想删除它们,因为我想保留历史。我该怎么做呢?
我知道可以在refs/heads之外创建一个ref。例如:refs/archive/old_branch。这样做有什么后果吗?
我的git存储库中有一些旧的分支,它们不再处于活跃的开发状态。我想存档这些分支,这样它们在运行git branch -l -r时默认不会显示。我不想删除它们,因为我想保留历史。我该怎么做呢?
我知道可以在refs/heads之外创建一个ref。例如:refs/archive/old_branch。这样做有什么后果吗?
当前回答
下面是它的别名:
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"
关于添加别名
其他回答
下面是它的别名:
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"
关于添加别名
把史蒂夫的回答扩展到遥控器上的变化,我做到了
git tag archive/<branchname> <branchname>
git branch -D <branchname>
git branch -d -r origin/<branchname>
git push --tags
git push origin :<branchname>
要从远程进行恢复,请参见此问题。
杰里米的回答原则上是正确的,但恕我直言,他指定的命令不太正确。
下面是如何在不签出分支的情况下将一个分支归档到一个标签中(因此,在删除该分支之前,不必签出到另一个分支):
> git tag archive/<branchname> <branchname>
> git branch -D <branchname>
下面是如何恢复一个分支:
> git checkout -b <branchname> archive/<branchname>
在Git中,分支只是一个指向某个提交的指针。因此,删除一个分支只会删除一个指针,即使分支从未合并,也不会删除相关的代码。因此,“归档”本地分支就像记住指针名称一样简单(提交哈希)。你可以在。/git/refs/heads目录下找到这些信息。或者,您可以简单地将分支到提交的映射复制到如下所示的文本文件中。
git branch -v > /path/to/backup/branches.txt
删除分支之后,可以使用以下命令在本地恢复它。
git branch <branch_name> <commit_hash>
您可以使用为您存档分支的脚本
archbranch
它为您创建一个带有前缀archive/的标记,然后删除分支。在使用它之前,请确保您了解它的功能=)
使用- $/your/location/of/script/archbranch [branchname] [defaultbranch]
如果希望运行脚本而不向其写入位置,则将其添加到路径中
那你可以顺便来看看
$ archbranch [branchname] [defaultbranch]
[defaultbranch]是归档完成后它将转到的分支。有一些问题与颜色编码,但除此之外,它的工作。我已经在项目中使用它很长时间了。