这为压缩多个提交提供了很好的解释:
http://git-scm.com/book/en/Git-Branching-Rebasing
但它不适用于已经推送的提交。如何在本地和远程回购中压缩最近的几次提交?
当我执行gitrebase-I origin/master ~4 master时,将第一个设置为pick,将其他三个设置为squash,然后退出(通过emacs中的c-x-c-c),我得到:
$ git rebase -i origin/master~4 master
# Not currently on any branch.
nothing to commit (working directory clean)
Could not apply 2f40e2c... Revert "issue 4427: bpf device permission change option added"
$ git rebase -i origin/master~4 master
Interactive rebase already started
其中2f40是选择提交。现在,4个提交都没有出现在git日志中。我希望我的编辑器重新启动,以便我可以输入一条提交消息。我做错了什么?
1) git rebase-i头~4
详细说明:它适用于当前分支;HEAD~4意味着压缩最近的四次提交;交互模式(-i)
2) 此时,编辑器打开提交列表,以更改第二次和后续提交,将pick替换为squash,然后保存。
输出:已成功重设基准并更新refs/heads/分支名称。
3) git推送原点refs/heads/branch名称--force
输出:
remote:
remote: To create a merge request for branch-name, visit:
remote: http://xxx/sc/server/merge_requests/new?merge_request%5Bsource_branch%5D=sss
remote:To ip:sc/server.git
+ 84b4b60...5045693 branch-name -> branch-name (forced update)
当您使用Gitlab或Github时,您可能会以这种方式遇到麻烦。您可以使用上述方法之一压缩提交。我最喜欢的是:
git rebase -i HEAD~4
or
git rebase -i origin/master
为您的提交选择squash或fixup。此时,您将检查git状态。信息可能是:
On branch ABC-1916-remote
Your branch and 'origin/ABC-1916' have diverged,
and have 1 and 7 different commits each, respectively.
(use "git pull" to merge the remote branch into yours)
你可能会被诱惑去做。不要那样做,否则你会陷入和以前一样的境地。
相反,通过以下方式推到原点:
git push origin +ABC-1916-remote:ABC-1916
+只允许强制推动一个分支。