git rebase使用(部分)
重新排序commit
现在历时中有这样的三个commit,他们是
commit 9f75177750050e6d9339b820c66128bcb407f8c2
Author: FIRE_TRAY <fire_tray@foxmail.com>
Date: Sun Oct 18 22:48:41 2015 +0800
feature: add C.txt
commit 6a48afc037172a4c20019f42d27e5faa853207ae
Author: FIRE_TRAY <fire_tray@foxmail.com>
Date: Sun Oct 18 22:48:15 2015 +0800
feature: add B.txt
commit 2bf31e1235b6dcd1948123df226ac18a690bcb95
Author: FIRE_TRAY <fire_tray@foxmail.com>
Date: Sun Oct 18 22:47:55 2015 +0800
feature: add A.txt
现在希望把commit “feature: add B.txt”放在 “add C.txt“之后。那么就需要rebase了。
首先输入git rebase -i
$ git rebase -i 2bf31e1235b6dcd1948123df226ac18a690bcb95
pick 6a48afc feature: add B.txt
pick 9f75177 feature: add C.txt
# Rebase 2bf31e1..9f75177 onto 2bf31e1 (2 command(s))
#
# 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
按“i”进入编辑模式,将B,C所在行调换,让Git先使用C的commit在应用B的commit,保存并退出,再次log一下发现commit顺序调换了。
localhost:learngit liufangliang$ git log --pretty=oneline
544197534071695a2b7376236e31974ea8b461ba feature: add B.txt
08b57309156b00024cafad7621740ed74f2129d6 feature: add C.txt
2bf31e1235b6dcd1948123df226ac18a690bcb95 feature: add A.txt
合并commit
根据上次时Git的提示,我们可以使用s
或者squash
来压缩commit,还是进行上次的操作,不过把需要合并的commit前的pick
改为s
或者squash
。主意压缩的commit会被压缩到前一个commit中,所以需要有前驱commit才行。以最开始状态为例,如果想把C压缩到B中去,那么需要执行:
$ git rebase -i 2bf31e1235b6dcd1948123df226ac18a690bcb95
并在编辑模式下将C部分的pick
改为s
。保存退出,再log发现C不见了,C的commit已经和B的合并,Git repo中的文件依旧为ABC三个,添加C的操作并没有失效
更改commit注释
更改最新提交的commit注释
直接输入以下指令即可
$ git commit --amend
更改历史commit的注释
由于上述命令仅用于更改最新的提交,如果需要改动历史commit的注释,需要利用rebase对commit重排序,把需要改动的commit顶置然后修改即可。