Loading

记录git rebase用法

git 是基于文件系统的版本管理工具,文档和详细介绍可以查看git

一.git commit --amend

如果你对文件做了修改需要和上一次的修改合并为一个change

git add .
git commit --amend

二.git rebase

  在 Git 中整合来自不同分支的修改主要有两种方法:merge 以及 rebase。 在本节中我们将学习什么是“变基”,怎样使用“变基”,并将展示该操作的惊艳之处,以及指出在何种情况下你应避免使用它 --  参考https://git-scm.com/book/zh/v2/Git-%E5%88%86%E6%94%AF-%E5%8F%98%E5%9F%BA

git rebase -i HEAD~n

列出最近的几次change(从old -> new),并且左边包含可以进行的操作

  1 pick 7a802ef test4
  2 pick 0eaa6ba for #noTicket 1
  3 pick 999e310 for #noTicket
  4 pick 83bb011 for #ceshi
  5
  6 # Rebase c3ff658..83bb011 onto c3ff658 (4 command(s))
  7 #
  8 # Commands:
  9 # p, pick = use commit
 10 # r, reword = use commit, but edit the commit message
 11 # e, edit = use commit, but stop for amending
 12 # s, squash = use commit, but meld into previous commit
 13 # f, fixup = like "squash", but discard this commit's log message
 14 # x, exec = run command (the rest of the line) using shell
 15 #
 16 # These lines can be re-ordered; they are executed from top to bottom.
 17 #
 18 # If you remove a line here THAT COMMIT WILL BE LOST.
 19 #
 20 # However, if you remove everything, the rebase will be aborted.
 21 #
 22 # Note that empty commits are commented out

在commands里面包括了几个命令,简单介绍下

r, reword = use commit, but edit the commit message
简单的理解就是修改此commit的commit message
  1 pick 7a802ef test4
  2 r 0eaa6ba for #noTicket 1
  3 pick 999e310 for #noTicket
  4 pick 83bb011 for #ceshi

保存后进入编辑commit message模式

  1 for #noTicket 1
  2
  3 测试
  4
  5 Change-Id: Ia0542f3b9292df45213c567948ff6f797858426f

保存退出后对应change的message就修改成功了

edit : use commit, but stop for amending 选取此commit,并且保存退出后提交的修改将amend此commit

  1 pick 7a802ef test4
  2 e 73c9c8f for #noTicket 1
  3 pick 5cc2966 for #noTicket
  4 pick 437ba25 for #ceshi

保存退出后:

 gerrit-workshop git:(master) git rebase -i HEAD~4
Stopped at 73c9c8f1b281b8636ae990f58d1fba6260efb589... for #noTicket 1
You can amend the commit now, with

    git commit --amend

Once you are satisfied with your changes, run

    git rebase --continue

 之后做的修改再次amend后会基于这个commit

git add . 将所有修改提交到暂存区

git commit --amend 将暂存区的修改提交到本地仓库修改你刚才选中的change

git rebase --continue 回到最初的状态

第三个 squash use commit, but meld into previous commit ,将这个change合并到上一次change中,并且会保留这个change的message,相当于将两个commit合并为一个

第四个 fixup 和squash一样,但是不会保留change的message

 

posted @ 2017-06-15 13:17  戴杭林  阅读(1469)  评论(0编辑  收藏  举报