git rebase使用场景及注意事项

2019-10-23  本文已影响0人  十毛tenmao

git rebase是一个非常有魅力的命令,使用得当会让git代码管理更加清晰,但是如果对机理不熟悉则特别容易给团队造成巨大麻烦,请谨慎使用。本文就简单介绍一下使用方法、原理和注意事项

使用场景

git rebase -i [startpoint] [endpoint]

# startpoint不包含,endpoint包含
# endpoint可以没有,则默认为当前分支的HEAD

# 这时候,会自动进入 vi 编辑模式
pick cacc52da add: qrcode
pick f072ef48 update: indexeddb hack
pick 4e84901a feat: add indexedDB floder
pick 8f33126c feat: add test2.js

# Rebase 5f2452b2..8f33126c onto 5f2452b2 (4 commands)
#
# 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.
#
git checkout master
# commit: add b
# commit: add a

git checkout -b feature1
# ... feature1有一些commit
# commit: add c

git checkout master
# commit: add d
# .. master有一些commit,这个时候就是feature1的基分支master发生了改变

git checkout feature1

# rebase操作
git rebase master
# rebase后的commit记录,会把master记录放在feature1的记录前面
# commit: add c
# commit: add d
# commit: add b
# commit: add a

# merge操作
git merge master
# merge后的commit记录,会把master记录放在feature1后面或交叉,并会生成一条merge的commit
# commit: merge
# commit: add d
# commit: add c
# commit: add b
# commit: add a

其他常用命令

# 继续之前的rebase: 如果需要解决冲突等原因跳出rebase过程后,还想继续刚才的rebase
git rebase --continue

# 退出rebase
git rebase --abort

# 继续编辑rebase动作
git rebase --edit-todo

原理

注意事项

参考

上一篇 下一篇

猜你喜欢

热点阅读