在推动之前将多个提交组合成一个 [英] Combining Multiple Commits Into One Prior To Push

查看:114
本文介绍了在推动之前将多个提交组合成一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题不仅与如何完成这项任务有关,而且与Git的好坏有关。



考虑到我在本地做的工作最多在主分支上,但我创建了一个专题分支,我将称其为topical_xFeature。在处理topical_xFeature并在主分支上来回切换以完成其他工作的过程中,事实证明,我已在topical_xFeature分支上进行了多次提交,但在每次提交之间,我完成了否推。



第一,你会考虑这种不好的做法吗?坚持每个分支每次推送一次提交会不会更明智一点?



第二,我应该如何最好地完成自动导入操作topical_xFeature分支上的多个提交到主分支中进行推送?不必担心它,只是在多个提交被推送的地方进行推送,或者是以某种方式将提交合并到一个提交然后推送,这是否令人讨厌?再次,如何做到这一点?

解决方案

对于第一个问题,不,一次推送多个提交没有任何问题。很多时候,你可能想把你的工作分解成几个小的逻辑提交,但是只有当你觉得整个系列已经准备好时,才会推动它们。或者您可能在断开连接时在本地进行多次提交,并且在您再次连接后再将它们全部推入。没有理由限制自己每次推送一次提交。

我通常会发现,保持每次提交都是一个单一的,逻辑的,一致的更改是一个不错的主意,包括它需要的所有工作(所以,它不会离开您代码处于破碎状态)。如果你有两个提交,但如果你只应用了第一个提交,它们会导致代码被破坏,所以将第二个提交压缩到第一个提交可能是一个好主意。但是如果你有两个提交,每个提交都做出了合理的改变,把它们作为单独提交就可以了。

如果你想一起压缩几个提交,你可以使用 git rebase -i 。如果你在分支 topical_xFeature ,你可以运行 git rebase -i master 。这将打开一个编辑器窗口,其中包含一系列以 pick 为前缀的提交。你可以改变除第一个以外的所有内容到 squash ,这将告诉Git保留所有这些更改,但将它们压缩到第一次提交中。完成之后,请查看 master 并合并到您的功能分支中:

  git checkout topical_xFeature 
git rebase -i master
git checkout master
git merge topical_xFeature

或者,如果您只想将 topical_xFeature 中的所有内容压扁到 master 中,您可以做到以下几点:

  git checkout master 
git merge --squash topical_xFeature
git commit

您选择哪一个取决于您。一般来说,我不担心有多个较小的提交,但有时您不想担心额外的小提交,所以您只需将它们压缩成一个。


This question pertains not only to how to accomplish this task, but to whether doing so is good or bad practice with Git.

Consider that locally I do most work on the master branch, but I have created a topical branch I will call "topical_xFeature". In the process of working on "topical_xFeature" and switching back and forth to do other work on the master branch, it turns out that I have made more than one commit on the "topical_xFeature" branch, but between each commit, I have done no push.

First, would you consider this bad practice? Would it not be wiser to stick with one commit per branch per push? In what cases would it be good to have multiple commits on a branch before a push is made?

Second, how shall I best accomplish bringing the multiple commits on the topical_xFeature branch into the master branch for a push? Is it a nuisance to not worry about it and just do the push where multiple commits get pushed, or is it less annoying to somehow merge the commits into one and then push? Again, how to do this?

解决方案

For your first question, no, there's nothing wrong with pushing multiple commits at once. Many times, you may want to break your work down into a few small, logical commits, but only push them up once you feel like the whole series is ready. Or you might be making several commits locally while disconnected, and you push them all once you're connected again. There's no reason to limit yourself to one commit per push.

I generally find that it's a good idea to keep each commit a single, logical, coherent change, that includes everything it needs to work (so, it does not leave your code in a broken state). If you have a two commits, but they would cause the code to be broken if you only applied the first one, it might be a good idea to squash the second commit into the first. But if you have two commits where each one makes a reasonable change, pushing them as separate commits is fine.

If you do want to squash several commits together, you can use git rebase -i. If you're on branch topical_xFeature, you would run git rebase -i master. This will open an editor window, with a bunch of commits listed prefixed by pick. You can change all but the first to squash, which will tell Git to keep all of those changes, but squash them into the first commit. After you've done that, check out master and merge in your feature branch:

git checkout topical_xFeature
git rebase -i master
git checkout master
git merge topical_xFeature

Alternatively, if you just want to squash everything in topical_xFeature into master, you could just do the following:

git checkout master
git merge --squash topical_xFeature
git commit

Which one you choose is up to you. Generally, I wouldn't worry about having multiple smaller commits, but sometimes you don't want to bother with extra minor commits, so you just squash them into one.

这篇关于在推动之前将多个提交组合成一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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