Git tips: 合并 commit 保持分支干净整洁
来源:互联网 发布:网络教育能考公务员吗 编辑:程序博客网 时间:2024/05/19 19:13
原文:https://www.lovelucy.info/git-tips-combine-commits-keep-your-branch-clean.html?utm_source=tuicool&utm_medium=referral
本文的读者需要已经了解 基本的 Git 操作和开发流程 。
在我们开发完分支后,一般分支上会有很多 commit —— 少不了诸如 “fix typo”, “sth wrong in the previous commit” 之类的 commit。在合并到主干的时候,往往这类 commit 显得臃肿多余。为了方便别人做 code review,我们希望合并一些不必要的 commit 使我们的分支显得干净一目了然,也方便管理。有 3 种方式可以做到。
一、git rebase
$ git rebase -i origin/master
-i
参数表示互动 interactive,这时 git 会使用你设定的编辑器,让你对 git 历史记录做详细修改。
下面以 Atlassian 的例子来说明
# 开始开发一个新 feature$ git checkout -b new-feature master# 改了一些代码$ git commit -a -m "Start developing a feature"# 刚刚的修改有点问题,再改一下$ git commit -a -m "Fix something from the previous commit" # 紧急修复,直接在 master 分支上改点东西$ git checkout master# 改了一些代码$ git commit -a -m "Fix security hole" # 开始交互式地 rebase 了$ git checkout new-feature$ git rebase -i master
这时 git 会打开编辑器,你会看到 new-feature 分支上的 2 个最新 commit,以及一些指引提示
pick 32618c4 Start developing a featurepick 62eed47 Fix something from the previous commit # 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## 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.## Note that empty commits are commented out
每个 commit 前有一个操作命令,默认是 pick
,表示该行被选中,需要进行 rebase 操作。下面一堆注释的指引中还有几个指令,我们常用到的是以下 2 个
- squash:将这一行的 commit 与上一个 commit 进行合并
- fixup:与 squash 相同,只是不会保留这行 commit 的提交 message 信息
比如上面的例子,我们在编辑器中修改指令为
pick 32618c4 Start developing a featuresquash 62eed47 Fix something from the previous commit
这样一改,保存执行后,new-feature 分支就只剩下 1 个 commit 了,这个合并后的 commit 提交的信息包含之前 2 个 commit 的信息
Start developing a featureFix something from the previous commit
消除了一个多余的不那么重要的 commit,达到了我们的目的。最后,只需 fast-forward merge 到 master
$ git checkout master$ git merge new-feature
在别人看来,你就是一个天才开发者,没有出差错地一次实现了 new-feature
,项目的 commit 历史记录显得干净而有意义。
如果我们在编辑器中不用 squash 指令,而使用 fixup 指令,这样改
pick 32618c4 Start developing a featurefixup 62eed47 Fix something from the previous commit
则结果一样,还是只剩 1 个 commit,但是合并后的提交消息,就只有之前第一个 commit 的消息,第二个的 commit 消息被注释掉了
Start developing a feature
这样也达到了我们的目的,消除了曾经发生过 2 个 commit 的痕迹。在别人看来,这个新功能的分支是按计划一次性开发好的,中途并未发生 “错别字” 之类的意外,简直完美。
二、–fixup & –autosquash
# 自动标记这一次的 commit 为上一个 commit 的 fix$ git commit --fixup <commit># 自动组织合并两个 commit$ git rebase -i --autosquash
这种方式和上面的第一种原理是一样的,只是形式上不是在编辑器中调整历史记录,而是直接执行命令,结果等同。
三、撤销过去的 commit 重建一个新的
$ git reset HEAD~2$ git add .$ git commit -am "This is the new feature"$ git push --force
这种方式就比较暴力了,嗯。
说点题外话
修改上一次 commit 提交的 message
有时我们提交代码,message 写的太匆忙有错字(不是代码里有错字,而是 commit message 写错),就可以用下面的命令来修正
$ git commit --amend
不过只能修正上一次的 commit。如果很多个 commit 之前就有 message 写错,就得用上我们之前说的 git rebase
了
$ git rebase -i HEAD~4
和之前一样,会打开编辑器,显示最近的 4 次提交
pick 07c5abd Introduce OpenPGP and teach basic usagepick de9b1eb Fix PostChecker::Post#urlspick 3e7ee36 Hey kids, stop all the highlightingpick fa20af3 git interactive rebase, squash, amend
将 pick 指令改为 reword 指令,就可以修改这一行的 commit message
pick 07c5abd Introduce OpenPGP and teach basic usagepick de9b1eb Fix PostChecker::Post#urlsreword 3e7ee36 Hey kids, stop all the highlightingpick fa20af3 git interactive rebase, squash, amend
– EOF –
参考文档
- 阮一峰:Git 使用规范流程
- Atlassian Git tutorials: Rewriting history
- Git tips: 合并 commit 保持分支干净整洁
- git合并分支上指定的commit
- git 合并某个提交commit到指定的分支上
- 使用git的cherry-pick命令把其他分支的某个commit合并到当前分支
- git 合并旧分支至新的分支,并合并多次commit
- git commit合并
- git合并commit方法
- GIT合并特定commit
- 【GIT】git 分支 合并
- git如何合并某次提交命令到分支--git cherry-pick <commit id>
- 用git合并分支时,如何保持某些文件不被合并
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- git 合并分支出现 Please enter a commit message to explain why this merge is necessary.
- git 创建分支 合并分支
- Git: 合并几个commit为一个commit
- git分支的合并
- git 分支合并
- git分支的合并
- Linux运维笔记-课后作业-日志的内容同步和时间同步
- CAS原理深度分析及其结合Lock,Atomic分析
- Sum of two integers EIJudge
- 多线程之四大线程池的使用介绍
- js中的类型判断
- Git tips: 合并 commit 保持分支干净整洁
- MAC版本MYSQL安装完忘记密码
- HDU 3001 三进制状压DP
- Linux常用命令(29)之默认权限管理
- Linux初级学习之系统日志 第十章学习笔记
- Linux 学习 第九单元
- 一个文件下载的工具类
- 短信
- Android 常识