2

I have a branch, mybranch, that has six commits and stems from commit C. I would like to create a new branch, also based on commit C, that effectively includes all the commits from mybranch, squashed into one commit. What's the easiest way to do this?

I need to do this due to the mantra of "squash before you git-push". For the new branch, I don't want any mention of mybranch in the history pushed up to the remote server. The reason I want to create a whole new branch used to push is because I'd like to keep all the commits in mybranch for reference.

4

1 回答 1

6

为什么只推送压缩的提交?这听起来很疯狂(而且是错误的)。

但要回答你的问题:

git checkout -b newbranch # checkout new branch
git reset --soft C # move to commit C
git commit -m 'squashed commit'

另一种可能性是用于将rebase -i所有提交压缩/修复为一个:

git checkout -b newbranch
git rebase -i C # now, replace every 'pick' with 'fixup'/'squash' except the first one

同样有趣的是git merge --squash

生成工作树和索引状态,就好像发生了真正的合并一样(合并信息除外),但实际上不进行提交或移动 HEAD,也不记录 $GIT_DIR/MERGE_HEAD 以导致下一个 git commit 命令创建合并犯罪。这允许您在当前分支之上创建一个提交,其效果与合并另一个分支相同(或者在章鱼的情况下更多)。

git checkout -b newbranch C # create newbranch at commit C
git merge --squash mybranch
git commit -m 'squashed commit'
于 2011-08-31T21:44:37.300 回答