首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >git rebase使用场景及注意事项

git rebase使用场景及注意事项

作者头像
十毛
发布2019-10-24 19:32:42
1.9K0
发布2019-10-24 19:32:42
举报

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

使用场景

  • 合并多个commit为一个
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.
#
  • 基分支变更时(一般是master),重新让特性分支基于最新的基分支
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

原理

  • 首先,git 会把 feature1 分支里面的每个 commit 取消掉;
  • 其次,把上面的操作临时保存成 patch 文件,存在 .git/rebase 目录下;
  • 然后,把 feature1 分支更新到最新的 master 分支;
  • 最后,把上面保存的 patch 文件应用到 feature1 分支上;

注意事项

  • 不要通过rebase对任何已经提交到公共仓库中的commit进行修改,因为rebase会修改历史记录
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.10.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用场景
  • 其他常用命令
  • 原理
  • 注意事项
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档