我在我的git存储库中有一些旧分支,不再处于活跃开发状态。我想将这些分支存档,以便在运行git branch -l -r
时默认情况下不显示它们。我不想删除它们,因为我想保留历史记录。有什么方法可以做到这一点吗?
我知道可以创建一个refs/heads之外的引用,例如refs/archive/old_branch
。这样做会有什么后果吗?
我在我的git存储库中有一些旧分支,不再处于活跃开发状态。我想将这些分支存档,以便在运行git branch -l -r
时默认情况下不显示它们。我不想删除它们,因为我想保留历史记录。有什么方法可以做到这一点吗?
我知道可以创建一个refs/heads之外的引用,例如refs/archive/old_branch
。这样做会有什么后果吗?
我认为正确的做法是给分支打上标签。如果你在打完标签后删除了分支,那么实际上你已经保留了这个分支,但它不会在分支列表中出现。
如果需要返回到该分支,只需检出标签即可。它将从标签中有效地恢复该分支。
要存档和删除分支:
git tag archive/<branchname> <branchname>
git branch -d <branchname>
稍后恢复该分支的方法:
git checkout -b <branchname> archive/<branchname>
分支的历史记录将被保留,与您打标签时一模一样。branch -D
而不是branch -d
,因为它很可能没有完全合并。 - Arkadiy Kukarkingit tag [-a -m "某些描述"] archive/<你喜欢的任意名称> <分支名或提交ID>
。我在一个分支上工作时这样做,但后来决定采取另一种方法。我继续在同一个分支上工作,但进行了重大更改。我所做的工作现在以一个良好的名称和描述存储在那个归档标签中。 - eigilgit push --tags
(推送新创建的归档标签到服务器)。 git push origin :<branchname>
(从服务器中删除 <branchname> 分支)。 - jerrymouseJeremy的回答在原则上是正确的,但在我看来他指定的命令不太对。
以下是如何将一个分支存档到一个标签而无需检出该分支(因此,也无需先检出另一个分支,然后才能删除该分支):
> git tag archive/<branchname> <branchname>
> git branch -D <branchname>
以下是如何恢复一个分支的步骤:
> git checkout -b <branchname> archive/<branchname>
git push origin archive/<branchname>
推送标签。 - Nearoogit push origin --delete <branchname>
。来源:https://git-scm.com/book/en/v2/Git-Internals-The-Refspec - jslatane是的,你可以使用git update-ref
来创建带有一些非标准前缀的ref。例如:
git update-ref refs/archive/old-topic topic && git branch -D topic
git branch topic refs/archive/old-topic
带有非标准前缀(这里是refs/archive
)的ref不会出现在通常的git branch
、git log
或git tag
中。尽管如此,你仍然可以使用git for-each-ref
来列出它们。
我正在使用以下别名:
[alias]
add-archive = "!git update-ref refs/archive/$(date '+%Y%m%d-%s')"
list-archive = for-each-ref --sort=-authordate --format='%(refname) %(objectname:short) %(contents:subject)' refs/archive/
rem = !git add-archive
lsrem = !git list-archive
此外,您可能想要配置远程仓库,例如push = +refs/archive/*:refs/archive/*
来自动推送存档分支(或者只在推送时指定,例如git push origin refs/archive/*:refs/archive/*
)。
另一种方法是在删除分支之前将SHA1记录在某处,但它有局限性。没有任何引用的提交将在3个月后被GC回收(或者没有reflog的情况下会在几周内),更不用说手动的git gc --prune
。由引用指向的提交是安全的。
编辑:发现了一个相同想法的perl实现,作者是@ap:git-attic
编辑^2:发现一个博客文章,Git的创始人也使用了同样的技巧。他将其命名为git hold
。
git grep-archive
,git fetch-archive
,git push-archive
?)听起来是个好主意。我会更新答案并附上一些简介。rem
代表记住我,但无论如何您都可以将其重命名为自己喜欢的名称。这只是一个快捷方式 :) - snipsnipsnipgit push git://yourthing.com/myproject-archive-branches.git yourbranch
git branch -d yourbranch
git-bundle
代替单独的代码库。 - Jakub Narębskigit-bundle
? - Devin Rhodegit bundle
是一种创建文件的方式,该文件包含项目历史的选定部分,可用于获取或克隆。据我所知,它最初是为了“sneakernet”传输而设计的。 - Jakub Narębski这是它的别名:
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
用作别名名称。arcl = "! f() { git tag | grep '^archive/';}; f"
!git tag archive/$1 $1 && git branch -D
。 - Lack我正在使用以下别名来隐藏存档分支:
[alias]
br = branch --no-merge master # show only branches not merged into master
bra = branch # show all branches
git br
来显示活跃开发的分支,而使用git bra
则会显示所有分支,包括已经"归档"的分支。git merge --no-ff
合并任务分支;即使只有一个提交,您也需要该合并提交和历史记录气泡。git blame
查找相关提交,然后希望日志消息能够说明问题。如果我需要深入挖掘,我会找出工作是在分支上完成的,并阅读整个分支(以及问题跟踪器中的评论)。git blame
指向提交XYZ。我打开Git历史记录浏览器(gitk、GitX、git log --decorate --graph
等),找到提交XYZ并查看...AA - BB - CC - DD - EE - FF - GG - II ...
\ /
QQ - UU - XYZ - JJ - MM
git log
并在合并提交中搜索分支名称。即使在非常大的存储库中,速度也足够快。format-patch <branchName> <firstHash>^..<lastHash>
(使用git log <branchName>
获取firstHash和lastHash)。git branch -D <branchName>
git am
)可能具有挑战性。好处是,这种方法使得分支的提交可以进行垃圾回收,并节省存储库中的空间。firstHash
的父提交)的。 - Guildenstern步骤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>
为要存档的分支名称,在origin
不是默认远程仓库名的情况下,需要替换为远程仓库的名称。
注释:
在步骤1之前和之后,您可能希望运行git tag
命令以注意到添加的标签。
在步骤3之前和之后,您可能希望观察https://github.com/<github-username>/<github-repository-name>/branches
或运行git branch -a
命令以注意到删除分支的情况。
要恢复分支:
git checkout -b <branch_name> archive/<branch_name>
git push --set-upstream origin <branch_name>
References:
https://gist.github.com/zkiraly/c378a1a43d8be9c9a8f9
https://dev.to/clsource/archiving-git-branches-3k70
git tag archive/<branch_name> remotes/origin/<branch_name>
或类似的命令标记一个当前不在的分支。然后,唯一需要的其他命令是 git push origin --tags
和 git push origin -d <branch_name>
(避免潜在昂贵的检出操作)。 - Plato
git checkout [rev] file
命令轻松恢复这些资源。http://www.kernel.org/pub/software/scm/git/docs/git-rm.html - Dana the Sane