git rebase 命令介绍
2023-09-11 14:19:00 时间
git rebase 命令介绍
本文源自极客时间 《go 语言项目开发实战 孔令飞》
git rebase 的使用场景:
git rebase 的最大作用是它可以重写历史。
我们通常会通过 git rebase -i
这个交互界面会首先列出给定
git rebase 支持的变更操作如下:
在上面的 7 个命令中,squash 和 fixup 可以用来合并 commit。例如用 squash 来合并,我们只需要把要合并的 commit 前面的动词,改成 squash(或者 s)即可。你可以看看下面的示例:
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
s 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
rebase 后,第 2 行和第 3 行的 commit 都会合并到第 1 行的 commit。这个时候,我们提交的信息会同时包含这三个 commit 的提交信息:
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2ndCommit Message:
Fix PostChecker::Post#urls
# This is the 3rdCommit Message:
Hey kids, stop all the highlighting
如果我们将第 3 行的 squash 命令改成 fixup 命令:
pick 07c5abd Introduce OpenPGP and teach basic usage
s de9b1eb Fix PostChecker::Post#urls
f 3e7ee36 Hey kids, stop all the highlighting
pick fa20af3 git interactive rebase, squash, amend
rebase 后,还是会生成两个 commit,第 2 行和第 3 行的 commit,都合并到第 1 行的 commit。但是,新的提交信息里面,第 3 行 commit 的提交信息会被注释掉:
# This is a combination of 3 commits.
# The first commit's message is:
Introduce OpenPGP and teach basic usage
# This is the 2ndCommit Message:
Fix PostChecker::Post#urls
# This is the 3rdCommit Message:
# Hey kids, stop all the highlighting
除此之外,我们在使用 git rebase 进行操作的时候,还需要注意以下几点:
- 删除某个 commit 行,则该 commit 会丢失掉。
- 删除所有的 commit 行,则 rebase 会被终止掉。
- 可以对 commits 进行排序,git 会从上到下进行合并。
相关文章
- [Git] git revert ( revert commit 和 revert merge)
- git分支原理命令图文解析
- git提交到远程仓库失败
- Git 代码更新:git fetch 和 git pull 的区别
- [git]Git常用命令
- git remote 命令的用法
- [Git] git Ignore a File that has Already been Committed and Pushed
- [Git] Use and Compare the Different git Reset Options: --hard, --soft, and --mixed
- [Practical Git] Configure global settings with git config
- [Practical Git] Switching between current branch and last checkout branch
- [Git] Rebase basic
- Git远程04:git fetch & git push & git pull
- [Practical Git] Remove unnecessary git tracking with .gitignore files
- [Practical Git] Configure global settings with git config
- [Practical Git] Format commit history with git log arguments
- git创建与合并分支
- use web IDE to commit change to git
- 使用git命令行将某个指定文件回退到前几个版本
- Git经常使用命令
- GitHub上传文件夹及Git命令
- Git 管理工具 SourceTree 的使用(上手简单,不熟悉git命令的开发者必用)