Git - 压扁整个分支 - 一行压扁命令 [英] Git - squash entire branch - one line squash command
问题描述
如何在不使用的情况下挤压整个分支> git rebase --interactive 然后更改
挑选
为 squash
所有提交?
我的首选方法是双线程(不包括下面的步骤1和4)。好处是你不需要知道/记录任何提交ID,你可以编写一个简单的别名来执行所有涉及的步骤,并且实际上将整个分支移动到 origin / master上,以便实际合并成主人可以是一个快进,并且不会有任何冲突。
首先,我的假设:
- 您正在使用名为
my-feature-branch
的分支。这个分支已经通过多次提交而从master
发散;
- 您的本地
master
跟踪远程分支origin / master
- 您想要将所有来自
my-feature-branch
的提交压缩为当前的单个提交ontop origin / master 的状态(不是您本地的master
,可能已过时) - 全部你没有任何未更改的变更(它们将在
git reset --hard
期间丢失)
$ b $
$ b我的过程如下:
-
获取,所以
原点/ master
是当前的:
$ git fetch
-
将您的本地分支上的所有提交重置为指向
origin / master
$ git reset - 混合原点/主
$ p
-
将您之前分支状态的所有旧更改合并到索引中
$ git merge --squash HEAD @ {1}
-
提交您的更改 - Git将预先填充您的编辑器,其中包含所有来自压扁提交的提交消息的提交消息
我提到的简单别名是:
alias squash =git fetch;混帐重置 - 混合起源/主; git merge --squash HEAD @ {1}
while I am working on new code I make many small commits to track my changes. My company however prefers each feature to be committed in a single commit. So the solution is to squash my entire branch down to a single commit.
How do I squash an entire branch without using
git rebase --interactive
and then changingpick
tosquash
for all the commits?解决方案My preferred method is a two-liner (excluding steps 1 and 4 below). The benefits are you do not need to know/record any commit IDs, you can write a simple alias to perform all the steps involved, and your actually moving your entire branch onto origin/master so that the actual merge into master can be a fast-forward and there cannot be any conflicts.
First, my assumptions:
- You're working on a branch called
my-feature-branch
. This branch has diverged frommaster
by several commits; this is the checked-out branch. - Your local
master
tracks remote branchorigin/master
- You want to squash all of your commits from
my-feature-branch
into a single commit ontop of the current state of origin/master (not your localmaster
, which may be out of date) - All of your changes are committed, you have no unstaged changes (they will be lost during
git reset --hard
)
My process is as follows:
Fetch, so
origin/master
is current:$ git fetch
Throw away all the commits on your local branch by resetting it to point at
origin/master
$ git reset --mixed origin/master
Merge all of your old changes from the previous state of your branch into the index
$ git merge --squash HEAD@{1}
Commit your changes - Git will pre-populate your editor with a commit message containing all the commit messages from the squashed commits
The simple alias I mentioned would be:
alias squash="git fetch; git reset --mixed origin/master; git merge --squash HEAD@{1}"
这篇关于Git - 压扁整个分支 - 一行压扁命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-