git 代理 git_Git壁球解释

git 代理 git

什么是Git Squash? (What is Git Squash?)

One of the things that developers hear quite often regarding their pull requests is something like “That looks good to me, please squash and merge”. The fun part is that there is no such command like git squash (unless you create an alias to it).

开发人员经常听到有关其拉取请求的一件事,例如“对我来说看起来不错,请压缩并合并”。 有趣的是,没有像git squash这样的命令(除非您为其创建别名 )。

To squash pull request means commonly to compact all the commits in this request into one (rarely to other number) to make it more concise, readable and not to pollute main branch’s history. To achieve this, a developer needs to use interactive mode of Git Rebase command.

为了squash拉请求的装置通常在此请求所有提交压缩成一个(很少到其他号码),使之更简洁,可读性和不污染主要分支的历史。 为此,开发人员需要使用Git Rebase命令的交互模式

Quite often when you develop some new feature you end up with several intermittent commits in your history - you develop incrementally after all. That might be just some typos or steps to final solution. Most of the time there is no use in having all these commits in the final public version of your code, so it’s more beneficial to have all of them compacted into one, single and final version.

开发新功能时,经常会在历史记录中多次断断续续地提交-毕竟,您是逐步进行开发的。 这可能只是最终解决方案的一些错别字或步骤。 在大多数情况下,在代码的最终公共版本中没有所有这些提交是无用的,因此将它们全部压缩为一个,单个和最终版本会更加有益。

So let’s assume you have following commit log in the branch you’d like to merge as part of pull request:

因此,假设您要合并的分支中包含以下提交日志,作为合并请求的一部分:

$ git log --pretty=oneline --abbrev-commit
30374054 Add Jupyter Notebook stub to Data Science Tools
8490f5fc Minor formatting and Punctuation changes
3233cb21 Prototype for Notebook page

Clearly we would prefer to have only one commit here, since there is no benefit in knowing what we started on writing and which typos we fixed there later. Only the final result is important.

显然,我们宁愿只在这里提交一次,因为知道我们开始写什么以及后来在这里改正哪些错字并没有好处。 只有最终结果很重要。

So what we do is start an interactive rebase session from the current HEAD (commit 30374054) to commit 3233cb21, with the intention to combine the 3 latest commits into one:

因此,我们要做的是从当前HEAD (提交30374054 )开始一个交互式的rebase会话,以提交3233cb21 ,目的是将3个最新的提交合并为一个:

$ git rebase -i HEAD~3

That will open an editor with something like the following:

这将打开一个如下所示的编辑器:

pick 3233cb21 Prototype for Notebook page
pick 8490f5fc Minor formatting and Punctuation changes
pick 30374054 Add Jupyter Notebook to Data Science Tools
# Rebase
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

As always, Git gives us a very nice help message where you can see the squash option we are looking for.

与往常一样,Git为我们提供了非常好的帮助消息,您可以在其中看到我们正在寻找的squash选项。

Currently the instructions for interactive rebase say to pick every specified commit and preserve the corresponding commit message. That is - don’t change anything. But we want to have only one commit in the end.

当前,交互式变基的说明说要pick每个指定的提交保留相应的提交消息。 那是-不要改变任何东西。 但我们希望最后只提交一次。

So you can simply edit the text in you editor replacing pick with squash (or just s) next yo every commit we want to get rid of and save/exit the editor. That might look like this:

因此,您可以简单地在编辑器中编辑文本,然后在每次要删除的保存和保存/退出编辑器的过程中,用squash (或s )替换pick 。 可能看起来像这样:

s 3233cb21 Prototype for Notebook page
s 8490f5fc Minor formatting and Punctuation changes
pick 30374054 Add Jupyter Notebook to Data Science Tools

When you close your editor after saving this change it will be reopened right away and will suggest that you choose and reword those commit messages. Something like this:

保存更改后关闭编辑器后,它将立即重新打开,并建议您选择并重新输入那些提交消息。 像这样:

# This is a combination of 3 commits.
# The first commit's message is:
Prototype for Notebook page

# This is the 2nd commit message:

Minor formatting and Punctuation changes

# This is the 3rd commit message:

Add Jupyter Notebook to Data Science Tools

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

At this point you can delete all the messages you don’t want to be included in the final commit version. You can also reword them or just write commit message from scratch.

此时,您可以删除所有不希望包含在最终提交版本中的消息。 您也可以改写它们的字词或仅从头开始编写提交消息。

Just remember that the new version will include all the lines that don't start with the # character. Once again, save and exit your editor.

只需记住,新版本将包含所有以#字符开头的行。 再次保存并退出编辑器。

Your terminal now should show a success message including Successfully rebased and updated <branch name> and the git log should show a nice and compacted history with only one commit. All intermediary commits are gone and we are ready to merge!

现在,您的终端应显示一条成功消息,其中包括“ Successfully rebased and updated <branch name> ,并且git日志应仅一次提交即可显示良好且紧凑的历史记录。 所有中间提交都已消失,我们可以合并了!

关于本地和远程提交历史记录不匹配的警告 (Warning about local and remote commit history mismatch)

This operation is slightly dangerous if you have your branch already published in a remote repository - you are modifying commit history after all. So it’s best to do the squash operation on a local branch before you do push.

如果您的分支已经发布在远程存储库中,则此操作会有些危险-毕竟您正在修改提交历史记录。 因此,最好在执行push之前在本地分支上执行壁球操作。

Sometimes, it will be already pushed - how would you create a pull request after all? In this case you’ll have to force the changes on the remote branch after doing the squashing, since your local history and branch history in the remote repository are different:

有时,它已经被推送-毕竟您将如何创建一个请求请求? 在这种情况下,您必须在执行挤压后在远程分支上强制进行更改,因为您的本地历史记录和远程存储库中的分支历史记录是不同的:

$ git push origin +my-branch-name

Do your best to make sure you are the only one using this remote branch at this point, or you’ll make the other developer's life harder when they have a history mismatch. But since squashing is usually done as the final operation on a branch before getting rid of it, it’s usually not so big of a concern.

尽力确保您是此时唯一使用此远程分支的人,否则当他们之间的历史记录不匹配时,您将使其他开发人员的生活更加艰难。 但是由于壁球通常是在摆脱分支之前作为分支的最终操作完成的,因此通常不必担心。

翻译自: https://www.freecodecamp.org/news/git-squash-explained/

git 代理 git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值