压缩git中的提交意味着什么? [英] What does it mean to squash commits in git?

查看:124
本文介绍了压缩git中的提交意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Squashing在git中的含义是什么.如何在Github中压缩提交?

What does Squashing commits in git mean. How do I squash commits in Github?

我是Git的新手,我要求分配给Coala-analyzer中的新手错误.我修复了该错误,现在我被要求压缩提交内容.我该怎么做?

I'm new to Git and I requested to be assigned to a newcomer bug in coala-analyzer. I fixed the bug, and now I was asked to squash my commits. How do I do it?

推荐答案

您可以将Git视为工作目录快照的高级数据库.

You can think of Git as an advanced database of snapshots of your working directory(ies).

Git的一个非常不错的功能是可以重写提交历史记录.
这样做的主要原因是,很多这样的历史记录仅与生成它的开发人员相关,因此,在将其提交到共享存储库之前,必须对其进行简化或使其更加美观.

One very nice feature of Git is the ability to rewrite the history of commits.
The principal reason for doing this is that a lot of such history is relevant only for the developer who generated it, so it must be simplified, or made more nice, before submitting it to a shared repository.

压缩提交从惯用的观点来看,意味着将所述提交中引入的更改移到其父级中,以便最终得到一个提交而不是两个(或更多)提交. br> 如果您多次重复此过程,则可以将 n 提交减少为一个.

Squashing a commit means, from an idiomatic point of view, to move the changes introduced in said commit into its parent so that you end up with one commit instead of two (or more).
If you repeat this process multiple times, you can reduce n commit to a single one.

从视觉上看,如果您从标记为 Start 的提交开始工作,那么您就需要

Visually, if you started your work at the commit tagged Start, you want this

您可能会注意到,新的提交具有略深的蓝色阴影.这是故意的.

You may notice that the new commit has a slightly darker shade of blue. This is intentional.

在Git中,压榨是通过称为 Interactive Rebase 的特殊形式的 Rebase 实现的.
为简化将一组提交重新建立到分支 B 中的时间,您可以应用这些提交所引起的所有更改,这些更改从 B 开始而不是其原始祖先.

In Git squashing is achieved with a Rebase, of a special form called Interactive Rebase.
Simplifying when you rebase a set of commits into a branch B, you apply all the changes introduced by those commits as they were done, starting from B instead of their original ancestor.

视觉线索

再次注意不同的蓝色阴影.

Note again the different shades of blue.

一个交互式的变基,让您选择如何对提交进行变基. 如果运行此命令:

An interactive rebase let you choose how commits should be rebased. If you run this command:

 git rebase -i branch

您最终将得到一个列出将被重新提交的提交的文件

You would end up with a file that lists the commits that will be rebased

 pick ae3...
 pick ef6...
 pick 1e0...
 pick 341...

我没有命名提交,但是这四个是从 Start Head

I didn't name the commits, but these four ones are intended to be the commits from Start to Head

此列表的优点是它是可编辑的.
您可以忽略提交,也可以压缩它们.
您要做的就是将第一个单词更改为 squash .

The nice thing about this list is that it is editable.
You can omit commits, or you can squash them.
All you have to do is to change the first word to squash.

 pick ae3...
 squash ef6...
 squash 1e0...
 squash 341...

如果关闭编辑器却没有发现合并冲突,那么您将获得以下历史记录:

If you close the editor and no merge conflicts are found, you end up with this history:

在您的情况下,您不想基于另一个分支,而希望基于一个先前的提交.
为了转换历史记录,如第一个示例所示,您必须运行类似

In your case, you don't want to rebase into another branch, but rather into a previous commit.
In order to transform the history as shown in the very first example, you have to run something like

git rebase -i HEAD~4

对于除第一次提交以外的所有提交,将命令"更改为 squash ,然后关闭编辑器.

change the "commands" to squash for all the commits apart from the first one, and then close your editor.

有关更改历史记录的提示

在Git中,永远不会编辑提交.可以将它们修剪,设置为不可访问,克隆但不能更改.
重新确定基准时,实际上是在创建新的提交.
旧的不再可以通过任何引用访问,因此历史记录中没有显示,但它们仍然存在!

In Git, commits are never edited. They can be pruned, made not reachable, cloned but not changed.
When you rebase, you are actually creating new commits.
The old ones are not longer reachable by any refs, so are not shown in the history but they are still there!

这是您实际获得的变基奖励:

This is what you actually get for a rebase:

如果您已经将它们推到某个位置,则重写历史记录实际上会创建一个分支!

If you have already pushed them somewhere, rewriting the history will actually make a branch!

这篇关于压缩git中的提交意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆