0%

GIT Rebase 学习 //TODO

动机

一般git rebase在过去项目中很少用到,最近阅读了如下blog, Git Rebase ,很赞同作者说的“提交历史质量与代码质量同等重要” ,因此在囫囵吞枣式地阅读了一下后,有了如下笔记。

Rebase的适用情形

  • Edit/rework a commit in the history
  • 修订提交历史
  • Add a commit into the history of a change set (or branch)
  • 增加一个改变集合或分支的提交到历史
  • Remove a commit from the history of a change set (or branch)
  • 删除一个改变集合或分支的提交到历史
  • Split a commit into several commits
  • 拆分一个提交到若干各提交
  • Combine a series of commits into one commit
  • 合并一系列提交为一个单独提交
  • Re-order commits
  • 重新排序提交

用于合并本地若干次提交

将本地近三次提交合并,可以通过rebase -i 进入交互式编辑

1
git rebase -i HEAD~3

之后,在文本编辑界面中,将看到最近几次提交的记录,类似

1
2
3
pick 09i3foe comments
pick jeo32jo comments
pick je323jo comments

此时,可以通过改变pick命令来完成多次提交的合并。相关指令说明:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue Rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to reword the commit message.

显然,可以通过将第一个保留为pick,将其他的改为squash或者fixup来完成多条提交合并。

Welcome to my other publishing channels