How can I squash a range of git commits together given starting and ending SHA's

后端 未结 4 1908
一向
一向 2021-02-07 03:19

I have a branch with about 20 commits.

The first SHA on the branch is bc3c488...
The last SHA on the branch is 2c2be6...

How can I

4条回答
  •  渐次进展
    2021-02-07 03:59

    Interactive rebasing can help here.

    Let's say your branch is based off of your upstream's master branch. (and let's say your upstream is defined by the "upstream" remote)

    Do this:

    git rebase -i upstream/master
    

    "upstream/master" can be replaced by any SHA if you need to be more exact.

        git rebase -i bc3c488
    

    You will be placed in an editor defined by your $EDITOR environment variable. Change "pick" to "squash" (or "s" for short) for every line except the very top one. This is squashing all those commits into one.

    As with any merge or rebase against another line of work there is the possibility of a code conflict. If this occurs, do a "git status" to see which files are conflicted, edit those files (the conflict will be delimited with <<< and >>> symbols) and do a "git rebase --continue"

    Rebasing replays commit's one at a time, keep this in mind if you find yourself fixing the same conflict over and over again (there are tools to help with this as well).

    You will then be presented with an opportunity to edit the commit message for your new squashed commit.

    Push to your remote branch with -f (this re-writes history which you want, but be careful with this one).

    This is a pretty good tutorial on interactive rebasing: https://www.atlassian.com/git/tutorials/rewriting-history/git-rebase-i

提交回复
热议问题