前馈科技

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 5577|回复: 0

git squash和git commit --amend的用法

[复制链接]

97

主题

97

帖子

539

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
539
发表于 2022-1-24 23:25:33 | 显示全部楼层 |阅读模式
一 git squash的用法
1 用途
考虑如下场合,我们在本地仓库的新建分支上没修改一次就向线上clone仓库的对应分支上commit一次,经过多次commit后我们准备将线上clone仓库的对应分支合并到远程master分支上,整个merge中包含多个commit,处理起来非常不方便,此时我们就需要用到git squash功能将多个commit合并(git squash)成一个commit再merge.
2 操作过程
a: 执行git rebase --interactive HEAD~5   命令,执行后,会进入以下编辑界面
  1. pick xxx: commit1 add : add xxx
  2. pick xxx: commit2 fix: update xxxx
  3. pick xxx: commit3 fix: delete xxx
  4. pick xxx: commit4 modify : update xxxx
复制代码
b: 移动光标,编辑以上4次commit,例如将除第一行的pick外,其余的pick都改成 squash
  1. pick xxx: commit1 add : add xxx
  2. squash xxx: commit2 fix: update xxxx
  3. squash xxx: commit3 fix: delete xxx
  4. squash xxx: commit4 modify : update xxxx
复制代码
c: 保存编辑,注意:此时提示你命名并选择保存到哪儿,这时千万不能命名保存,否则不起作用,原因未知,怎么解决也未知!正确的操作为:直接回车
d: 将合并后的commit同步到线上clone仓库的对应分支上: git push -f origin fft
e: 再次执行将线上clone仓库的对应分支合并到远程master分支上,此时的merge显示就只有一个commit了其他几个commit以注释的形式存在于合并后的commit标题上.

二 git commit --amend的用法
1 用途
继续前面的git squash过程,由于最后的merge显示只有一个commit,其标题是commit1 add : add xxx 整个标题可能太简单,无法提示该merge的内容,这时你需要通过git commit --amend改变commit的名称或增加commit的注释.

2 操作过程
a: 执行命令 git commit --amend
b: 在命令行编辑模式下修改commit标题和注释
修改前:
  1. commit1 add : add xxx
  2. commit2 fix: update xxxx
  3. commit3 fix: delete xxx
  4. commit4 modify : update xxxx
复制代码
修改后
  1. xxxxxxxxxxxxx commit
  2. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  3. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  4. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
复制代码
c: 将合并后的commit同步到线上clone仓库的对应分支上: git push -f origin fftd: 再次执行将线上clone仓库的对应分支合并到远程master分支上,此时的merge显示的一个commit的标题就和说明就复杂多了.





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|计算机控制

GMT+8, 2024-4-20 02:06 , Processed in 0.049036 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表