将分支移动到历史上的另一个点
我们有以下历史记录
start master public
| | |
v v v
o---o-- ... --o---o---o
不幸的是,我们对包含一些敏感数据的 master
分支进行了一些提交。我们在一个名为 public 的单独分支中对此进行了修改。现在我们想要“切断”public
分支,以便在 public
中获得完整且干净的“状态”,但又不包含仍然通过 包含的受损历史部分大师。换句话说,我们想要以下新历史记录:
start master
| |
v v
o---o-- ... --o---o
\
o <- public
现在检查 public
将导致与原始情况相同的工作树,但没有合理的历史记录详细信息。然后,我们封存旧的 master
分支:将其重命名为 unsafe
并从新的 public
中精心设计一个新的 master
分支> 分支机构。通过这种方式,我们可以将旧历史记录保存在 unsafe
中,并且能够将 public
分支推向公众,而无需担心:
start unsafe
| |
v v
o---o-- ... --o---o
\
o---o-- ... --o <-- public
\ /
o-- .. --o-- ... --o <-- master
实现此目的的正确 git 命令是什么?
PS:当然,我们可以签出 start
,创建一个新分支并在那里提交 public
分支的整个工作树。但一定有一种不同的、更奇特的、git 的方式!
We have the following history
start master public
| | |
v v v
o---o-- ... --o---o---o
Unfortunately we made some commits into the master
branch containing some sensitive data. We amended this in a separate branch called public. Now we want to "cut-off" the public
branch in order get a complete and clean "state" in public
but without the compromising history parts still contained via master
. In other words we want the following new history:
start master
| |
v v
o---o-- ... --o---o
\
o <- public
Now checking out public
shall lead to the same working tree as in the original situation but without the sensible history details. Afterwards we mothball the old master
branch: Rename it to unsafe
and elaborate a new master
branch out of the new public
branch. This way we conserve the old history in unsafe
and are able to push the public
branch into the public without any worries:
start unsafe
| |
v v
o---o-- ... --o---o
\
o---o-- ... --o <-- public
\ /
o-- .. --o-- ... --o <-- master
What are the right git commands to achieve this?
PS: Of course we could checkout start
, make a new branch and commit there the entire working tree of the public
branch. But there must be a different, fancier, git way!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将所有公共提交合并为一个提交(即,将多个提交合并为一个提交,或完全删除单个提交等)的正确方法是使用 git rebase -i 。所以你要做的是(假设第一次提交有一个标签
start
,如你的图表所示):出现一个编辑器窗口,你可以在其中编辑所有补丁的顺序
只需对所有补丁使用
squash
即可。保存该文件并关闭编辑器后,git
将根据请求重新组合您的补丁历史记录。当然,master分支根本不会改变它的历史。要将当前的 master 重命名为 unsafe 并开始公开进行所有进一步的开发,我会这样做:
如果您不创建
unsafe
分支,硬重置将丢失 master 中的所有提交,并且您不会能够(轻松)访问它们了。因此,请确保您确实创建了该分支。另一种方法是将
master
重命名为unsafe
,然后创建一个新的 master 分支:两者都应该产生完全相同的分支结构。 (当然,第二个没有失去历史的危险......)
The proper way to merge all commits in public into one commit (i.e. merging several commits to one, or removing individual commits entirely, etc.) is to use
git rebase -i
. So what you would do is (assuming the first commit has a tagstart
as your graph indicates):An editor window comes up where you can edit the order of all the patches
simply use
squash
for all your patches. After you saved that file and closed the editor,git
will recombine your patch history as requested. Of course, the master branch will not change its history at all.To rename your current master as unsafe and start all further development on public, I would do:
If you don't create the
unsafe
branch, the hard reset will loose all commits in master and you won't be able to (easily) access them any more. So make sure you really created that branch.An alternative would be to rename
master
tounsafe
and then create a new master branch:Both should result in the exact same branch structure. (Granted, the second does not have that danger of loosing the history...)