Git rebase的妙用
Git rebase的妙用
确实是一个很强大的工具,记录其使用方法,以供后用。
合并两个分支
已知分支master
和分支master.feature
,目前两个分支都各有提交,有重叠也有差异,需要将master
上差异的commit
拿出来提交到master.feature
上,有:
-
先checkout到master
git checkout master
-
然后rebase到新的分支,这里添加参数
-i
表示逐个commitgit rebase -i master.feature
这里可能会有冲突,便逐个解决冲突,解决完成后,此时的
master
分支便已经将自身有差异的改动,rebase
到master.feature
分支了,但分支仍然为master
-
然后checkout到
master.fearture
git checkout master.feature
-
将
master
分支合并到master.feature
分支,最后便得到最终的master.feature
分支,此时可以选择push
到服务器端:git merge master
以上指令可以合并为:
删除/改动某些提交
已知有一个分支,改动有a、b、c、d、e、f,这些字母均代表commit id,但是中间的d有问题,需要删除。此时可以用rebase
直接:
git rebase -i d^
根据其提示:
# Rebase 76b1d1a..0f35d41 onto 76b1d1a (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
# d, drop = remove commit
#
# 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
字段。
例如这里要删除,那么将pick
修改为d
即可,然后保存、退出,即可删除对应的commit,此时commit便只有a、b、c、e、f了。