使用git rebase合并屡次commit

1.首先使用git log查看一下提交历史: 这样在git中看到的是4次提交,有点冗余,须要作的是将4次commit合并为一次git

[demo@ubuntu1204:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git log  
commit 5e187c7dbe84af67ad19823a54f3cc3e3f6d6940  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 20:48:15 2015 +0800  
  
    add center style indent  
  
commit 6d577eb344080d7e3593733ac8dcb622de22b492  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Rebasing (4/4)  
Date:   Thu Jul 30 20:30:20 2015 +0800  
  
    add center style  
  
commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 20:16:35 2015 +0800  
  
    add center style  
  
commit 111ab9cc26101f7c6972de3dccfef2836a95efe0  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 18:57:46 2015 +0800  
  
    update templates
  1. git 压缩 git rebase -i HEAD~4

该命令执行后,会弹出一个编辑窗口,4次提交的commit倒序排列,最上面的是最先的提交,最下面的是最近一次提交。shell

pick 5e187c7dbe8    add center style indent  
pick 6d577eb3440    add center style  
pick f9b9508a3ab    add center style  
pick 111ab9cc261    update templates  
# Rebase 150a643..2fad1ae onto 150a643  
#  
# Commands:  
#  p, pick = use commit  
#  r, reword = use commit, but edit the commit message  
#  e, edit = use commit, but stop for amending  
#  s, squash = use commit, but meld into previous commit  
#  f, fixup = like "squash", but discard this commit's log message  
#  x, exec = run command (the rest of the line) using shell  
#  
# These lines can be re-ordered; they are executed from top to bottom.  
#  
# If you remove a line here THAT COMMIT WILL BE LOST.  
#  
# However, if you remove everything, the rebase will be aborted.  
#  
# Note that empty commits are commented out

修改第2-4行的第一个单词pick为squash,固然看一下里面的注释就理解含义了。 而后保存退出,Git会压缩提交历史,若是有冲突,须要修改,修改的时候要注意,保留最新的历史,否则咱们的修改就丢弃了ubuntu

pick 5e187c7dbe8    add center style indent  
squash 6d577eb3440  add center style  
squash f9b9508a3ab  add center style  
squash 111ab9cc261  update templates  
# Rebase 150a643..2fad1ae onto 150a643  
#  
# Commands:  
#  p, pick = use commit  
#  r, reword = use commit, but edit the commit message  
#  e, edit = use commit, but stop for amending  
#  s, squash = use commit, but meld into previous commit  
#  f, fixup = like "squash", but discard this commit's log message  
#  x, exec = run command (the rest of the line) using shell  
#  
# These lines can be re-ordered; they are executed from top to bottom.  
#  
# If you remove a line here THAT COMMIT WILL BE LOST.  
#  
# However, if you remove everything, the rebase will be aborted.  
#  
# Note that empty commits are commented out

修改之后要记得敲下面的命令(若是没有冲突会自动略过这一步进入下一步):讲以前的commit message 删除只保留一个便可this

git add .  
git rebase --continue

若是没有冲突,或者冲突已经解决,则会出现以下的编辑窗口:3d

# This is a combination of 4 commits.  
# The first commit’s message is:  
add center style indent  
  
# The 2nd commit’s message is:  
add center style  
  
# The 3rd commit’s message is:  
add center style  
  
# The 4th commit’s message is:  
update templates  
  
# Please enter the commit message for your changes. Lines starting  
# with ‘#’ will be ignored, and an empty message aborts the commit.

保存以后push到仓库rest

git push --force

若是你想放弃此次压缩的话,执行如下命令:code

git rebase --abort
相关文章
相关标签/搜索