马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
前两天检视代码时,发现PR里面有两个提交的形貌信息一模一样,于是我提出应该将这两个提交合并成一个,保持提交树的清晰。
1 先储存起来!
而同事这时正在开发别的特性,工作区不是干净的,没法直接执行 git rebase 操作,于是很天然地执行
将正在修改的内容保存到一个栈中,并维持当前工作区干净。
这样就可以执行切换分支、变基等操作,这些操作能执行的条件是当前工作区是干净的。
2 使用 git rebase 合并两个提交
我们先执行 git log 下令,找到要合并的两个提交之前的那个提交:cdedd430
- commit ec0218ff
- feat: 增加国际化
- commit 89f3d02c
- feat: 增加国际化
- commit cdedd430
- refactor: pnpm工程改造
复制代码 然后执行 git rebase 变基下令:
这时会进入一个交互式下令行界面:
- pick 89f3d02 feat: 增加国际化
- pick ec0218f feat: 增加国际化
- # Rebase cdedd43..ec0218f onto cdedd43 (2 commands)
- #
- # 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.
- #
- # 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
- "~/my-project/.git/rebase-merge/git-rebase-todo" 28L, 1232B
复制代码 这时你可以移动光标,但是无法输入字符,因为这是个只读的界面,需要先输入 i 字符进入编辑态,此时界面底部会出现 -- INSERT -- 标识。
- ...
- # Note that empty commits are commented out
- -- INSERT --
复制代码 下面那些以 # 开头的都是解释,只有前面两行比较关键。
- pick 89f3d02 feat: 增加国际化
- pick ec0218f feat: 增加国际化
- # ...
复制代码 每一行都由三部分构成:
- Command:需要执行的下令
- Commit ID:提交 ID
- Commit message:提交的形貌信息
我们需要将 ec0218f 合并到 89f3d02 上,因此需要将第二行的 pick 改成 squash(s) 或 fixup(f),这两个下令的区别在于:
- squash(s) 是将当前的提交合并到上一行的提交上,保存两个提交的形貌信息,可以在下一步中进行提交信息的编辑
- fixup(f) 也是将当前的提交合并到上一行的提交上,但不保存当条件交的形貌信息
由于我们两次提交信息完全一致,没必要保存,选择 fixup(f):
- pick 89f3d02 feat: 增加国际化
- f ec0218f feat: 增加国际化
复制代码 修改好之后,先按 ESC 退出编辑态,然后按 :wq 保存,显示以下信息说明变基成功,两个提交已经合并在一起
- Successfully rebased and updated refs/heads/kagol/test-rebase.
复制代码 执行 git log 看下效果:
- commit 86930d03
- feat: 增加国际化
- commit cdedd430
- refactor: pnpm工程改造
复制代码 可以看到两个提交已经合并在一起,并且生成了一个新的 Commit ID: 86930d03。
3 我代码没了!
1小时之后,同事慌张皇张地跟我说:
我代码没了!
我心田第一反应是:
刚才一顿变基猛如虎,不外变基变出问题来了吧?
作为一个成熟慎重的步伐员,什么大风大浪没见过,于是轻轻地回了句:
少年,莫慌!你先讲下你刚才做了什么?
我没,没做什么…
没做什么代码怎么会自己丢了呢?
我就执行了一下 git stash
pop,然后我之前写了一上午的代码就没…没了…
忽然开始心田有点慌,把同事一上午代码搞没了,我怎么赔给人家??
但心田不能体现出来,不着急,稳住少年!
你先执行下 git stash
list 看下储存栈里还有没有内容:
- $ git stash
- liststash@{0}: WIP on master: f90abfe Initial commit
复制代码 看到之前储存的内容还在我立马不慌了!
不再执行下 git stash
pop,我看下有没有报什么错?
执行完之结果然报了一个错:
- $ git stash
- poperror: Your local changes to the following files would be overwritten by merge: main.tsPlease commit your changes or stash them before you merge.AbortingThe stash entry is kept in case you need it again.
复制代码 大意是:
你本地修改了文件,储存栈里的内容如果弹出会覆盖掉你本地的代码,所以导致操作失败。
然后建议你先提交你本地的修改或者将你本地的修改储存起来。
并且特意提示你你储存的内容给你保存下来了,方便你下次要用的时候可以用。
不得不说,这 Git 真是太知心了,思量得很周到,为 Git 点个赞 |