Git - 压扁整个分支 - 一行压扁命令 [英] Git - squash entire branch - one line squash command

查看:230
本文介绍了Git - 压扁整个分支 - 一行压扁命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在处理新的代码时,我做了很多小的提交来跟踪我的更改。然而,我的公司更喜欢每个功能在一次提交中承诺。因此,解决方案是将整个分支压缩到一个提交。



如何在不使用的情况下挤压整个分支> 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

    我的过程如下:


    1. 获取,所以原点/ master 是当前的:

        $ git fetch 


    2. 将您的本地分支上的所有提交重置为指向 origin / master

        $ git reset  - 混合原点/主
    3. 将您之前分支状态的所有旧更改合并到索引中

        $ git merge --squash HEAD @ {1} 


    4. 提交您的更改 - 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 changing pick to squash 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 from master by several commits; this is the checked-out branch.
    • Your local master tracks remote branch origin/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 local master, 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:

    1. Fetch, so origin/master is current:

      $ git fetch
      

    2. Throw away all the commits on your local branch by resetting it to point at origin/master

      $ git reset --mixed origin/master
      

    3. Merge all of your old changes from the previous state of your branch into the index

      $ git merge --squash HEAD@{1}
      

    4. 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屋!

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