删除/隐藏 git 分支而不删除提交历史

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25169440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:17:02  来源:igfitidea点击:

Remove/hide git branches without deleting commit histories

gitgit-branchgit-branch-sculpting

提问by synaptik

Situation:

情况:

I have a main repo with a main dev branch and lots of "experiment" branches sprouting off from it (e.g., exp1and exp2). The purpose of these experiment branches is to serve as placeholders for experiments that generate numerical results. I record the branch name (and commit ID) of the experiment branches so I can return to the commits to see precisely the code and history behind the results.

我有一个主存储库,其中有一个主开发分支和许多从中萌芽的“实验”分支(例如,exp1exp2)。这些实验分支的目的是作为产生数值结果的实验​​的占位符。我记录了实验分支的分支名称(和提交 ID),以便我可以返回到提交以准确查看结果背后的代码和历史记录。

But, now, there are so many experiment branches that it's getting hard to see the main tree. So, I'm rethinking my strategy for keeping placeholders to the code behind each set of results (i.e., each experiment). Obviously I could just save the working dir at each branch, but it would be nice to keep the commit history also.

但是,现在,有这么多的实验分支,以至于很难看到主树。因此,我正在重新考虑将占位符保留在每组结果(即每个实验)背后的代码中的策略。显然我可以只保存每个分支的工作目录,但最好也保留提交历史。

Possible solution:

可能的解决方案:

One way to deal with this is to move the experiment branches into their own independent repos, each being rooted at the child node of the appropriate node in the commit history of the dev branch. Here's an illustration of what I mean:

解决这个问题的一种方法是将实验分支移动到它们自己独立的存储库中,每个存储库都以 dev 分支的提交历史记录中相应节点的子节点为根。这是我的意思的说明:

enter image description here

在此处输入图片说明

Click herefor larger version of image (on imgur.com).

单击此处获取更大版本的图像(在 imgur.com 上)。

So, for example, for branch exp1, I would like to export commits A->B->Cto a separate repo rooted at commit A. Then, I can just record the hash of commit P1so that I know where the exp1branch descended from.

因此,例如,对于 branch exp1,我想将提交导出A->B->C到以 commit为根的单独存储库A。然后,我可以只记录提交的哈希值,P1以便我知道exp1分支的来源。

Question:

题:

How can I do that?

我怎样才能做到这一点?

Better question:

更好的问题:

On the other hand, I strongly suspect there is a much better strategy for doing what I want to do---namely, unclutter the tree for visual inspection but keep placeholders to prior branches so I can return to them if needed. So, can anyone recommend a strategy for this?

另一方面,我强烈怀疑有一个更好的策略来做我想做的事情——即,整理树以进行视觉检查,但将占位符保留在先前的分支上,以便我可以在需要时返回它们。那么,有人可以为此推荐一个策略吗?

采纳答案by torek

Here's one alternative: use non-branch references to save the branch-tips before deleting the branch names.

这是一种替代方法:在删除分支名称之前,使用非分支引用来保存分支提示。

Since these are non-branch references, they won't show up in git branchoutput, nor in stuff shown by git log --branchesand gitk --branches, for instance. However, they will show up in --alllistings, and will retain repository objects.

由于这些都是非分支引用,就不会在显示git branch输出,也没有东西被示git log --branchesgitk --branches,例如。但是,它们将显示在--all列表中,并将保留存储库对象。

To create or update a non-branch reference, use git update-ref. Choose a name-space within refs/that you think will not collide with some future use (current uses are refs/heads/for branches, refs/tags/for tags, refs/remotes/for remote branches, refs/notes/for notes, and refs/stashfor the stash).

要创建或更新非分支引用,请使用git update-ref. 选择一个refs/你认为不会与未来使用冲突的命名空间(当前用途是refs/heads/用于分支、refs/tags/标签、refs/remotes/远程分支、refs/notes/笔记和refs/stash存储)。