The
git rebase
command allows you to easily change a series of commits, modifying the history of your repository. You can reorder, edit, or squash commits together.
通过 git rebase
能完成:
- Edit previous commit messages
- Combine multiple commits into one
- Delete or revert commits that are no longer necessary
git rebase
是一个变基的过程,有些命令可以直接完成,有些需要额外的操作,git commit -amend
,git rebase --continue
Rebasing commits against a branch
git rebase --interactive OTHER-BRANCH-NAME
Rebasing commits against a point in time
git rebase --interactive commit-id
rebase可选的操作
command | desc |
---|---|
pick | pick commits,此时可以 delete 、reorder commits |
reword | 和 pick 很像,但是会停止 rebase,给你修改 commit message 的机会, 任何当前 commit 做的代码修改都不受影响 |
edit | 如果选择 edit commit,此时有机会 amend commit,也就是说,可以增加或改变当前 commit ,甚至在继续进行 rebase 时,添加更多commit或者删除当前 commit 的错误代码 |
squash | 把多个 commit 合并成一个,会把当前命令之上的一行 commit 合并到当前 squash 的 commit 上 |
fixup | 和 squash 很像,把当前行合并到上一行 |
exec | This lets you run arbitrary shell commands against a commit. |
An example of using git rebase
使用 rebase 时,会使用配置的编辑器打开一个文件描述选择要 rebase 的 commits 范围的详细信息
1 | pick 1fc6c95 Patch A |
- 文件列出了7个 commits,说明从 rebase 的 starting point 到 当前分支状态直接有 7 次提交
- The commits you chose to rebase are sorted in the order of the oldest changes (at the top) to the newest changes (at the bottom).
- Each line lists a command (by default, pick), the commit SHA, and the commit message.整个 rebase 过程都是围绕这些命令完成的
- After the commits, Git tells you the range of commits we’re working with (41a72e6..7b36971).
- Finally, Git gives some help by telling you the commands that are available to you when rebasing commits.
Demo
1 | mkdir demo |
目标
- squash squash 第五个 bd089b9 commit 到第一个 commit
- pick 把第七个 commit 移到第二个 commit 之前
- fixup 合并第四个 commit 到 第二个 commit
- edit 把第三个 commit 分成更小的 commit
- reword 修改 bd089b9 commit message
actions
要完成上面五个目的,只需合理的修改 rebase 命令
1 | git rebase -i HEAD~7 |
定义 rebase 行为
1 | pick 0777897 1. Patch A |
This file is Git’s way of saying, “Hey, here’s what I’m about to do with this squash.” It lists the first commit’s message (“Patch A”), and the second commit’s message (“something to add to patch A”). If you’re happy with these commit messages, you can save the file, and close the editor. Otherwise, you can edit commit message. When the editor is closed, the rebase continues.
Git processes the two pick commands, It also processes the fixup command, since it doesn’t require any interaction,fixup merges the changes from 02ba6e9 into the commit before it, 2d01a11. Both changes will have the same commit message: “Patch B”.
Git gets to the edit opration, stop, prints the following message to the terminal
1
2
3
4
5
6
7
8
9
10
11You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continue
此时可以对代码做任意的修改,添加 commit,然后 git commit --amend 修改当前 commit 的提交信息,git rebase --continue继续进行rebase
git commit --amend 可以修改最新提交信息Git then gets to the reword command, 打开编辑器让你修改 commit message
推送 rebase 到 远程
1 | Don't override changes |
注意:demo 有文件冲突跑不通,要做一些修改。