git rebase合併多次commi
阿新 • • 發佈:2019-01-02
將多次commit合併,只保留一次提交歷史。
1.首先使用git log檢視一下提交歷史
-
[[email protected]:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git log
-
commit
5e187c7dbe84af67ad19823a54f3cc3e3f6d6940
-
Author: yangcs2009 <[email protected]>
-
Date: Thu Jul
30
20:
48:
15
2015 +
0800
-
-
add
center
style indent
-
-
commit
6d577eb344080d7e3593733ac8dcb622de22b492
-
Author: yangcs2009 <[email protected]>
-
Rebasing (
4
/
4)
-
Date: Thu Jul
30
20:
30:
20
2015 +
0800
-
-
add center
style
-
-
commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab
-
Author: yangcs2009 <[email protected]>
-
Date: Thu Jul
30
20:
16:
35
2015 +
0800
-
-
add center
style
-
-
commit
111ab9cc26101f7c6972de3dccfef2836a95efe0
-
Author: yangcs2009 <[email protected]>
-
Date: Thu Jul
30
18:
57:
46
2015 +
0800
-
-
update templates
這樣在git中看到的是4次提交,有點冗餘,需要做的是將4次commit合併為一次
2. git 壓縮 git rebase -i HEAD~4
該命令執行後,會彈出一個編輯視窗,4次提交的commit倒序排列,最上面的是最早的提交,最下面的是最近一次提交。
-
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
-
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
修改第2-4行的第一個單詞pick為squash,當然看一下里面的註釋就理解含義了。
然後儲存退出,git會壓縮提交歷史,如果有衝突,需要修改,修改的時候要注意,保留最新的歷史,不然我們的修改就丟棄了。修改以後要記得敲下面的命令:
-
git
add .
-
git rebase --
continue
如果你想放棄這次壓縮的話,執行以下命令:
git rebase --abort
如果沒有衝突,或者衝突已經解決,則會出現如下的編輯視窗:
-
# 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.
3.同步到遠端git倉庫
不過此時遠端的資訊仍未改變,下面操作會把修改同步到遠端git倉庫
-
[[email protected]:zh_cn(bugfix/ycs-MOS
-1503-notify-template-table-center)]$ git push -f
-
Enter passphrase
for key
'/home/demo/.ssh/id_rsa':
-
Counting objects:
1, done.
-
Writing objects:
100% (
1/
1),
223 bytes |
0 bytes/s, done.
-
Total
1 (delta
0), reused
0 (delta
0)
-
remote:
-
remote: View pull request
for bugfix/ycs-MOS
-1503-notify-template-table-center => release/
1.1
.3:
-
remote: http://git.sankuai.com/projects/SA/repos/cloud/pull-requests/
1042
-
remote:
-
To ssh://[email protected]/sa/cloud.git
-
+
5e187c7..
.8d26431 bugfix/ycs-MOS
-1503-notify-template-table-center -> bugfix/ycs-MOS
-1503-notify-template-table-center (forced update)