git合并commit记录

234 阅读2分钟

第一步

git rebase -i HEAD~ + 数字(要合并的数量)

如(合并数量为3个时):

git rebase -i HEAD~3

第二步

注意:
如果是在vscode里面自动打开文件,则直接在vscode里面进行修改;
如果是在git里面则会进入编辑区,按i进行编辑即可,以下是需要编辑的内容。

保留第一行的pick,其余行改为squash

// 原来的代码
git a6a378d feat: 1
git 59b790d feat: 2
git af5c9a2 feat: 3

// 修改后的代码
git a6a378d feat: 1
squash 59b790d feat: 2
squash af5c9a2 feat: 3

第三步

注意:
若在vscode中则保存好修改的文件后退出,退出后会自动在vscode里面打开另外一个文件;
若在git中则保存退出编辑模式(按esc后按:键再在最下方输入wq后回车退出),退出后会进入编辑信息页面。

把不需要的commit注释掉

//原来的代码
# This is a combination of 2 commits.
# This is the 1st commit message:

feat: 1

# This is the commit message #2:

feat: 2

# This is the commit message #3:

feat: 3

//修改后的代码
# This is a combination of 2 commits.
# This is the 1st commit message:

#feat: 1

# This is the commit message #2:

#feat: 2

# This is the commit message #3:

feat: 3

第四步(关键一步)

强制push上去:

git push -f origin + 分支名

git remote看一下是否为origin,这里举例用的是origin,不是的话就替换一下。

$ git remote
origin

如(分支名为feat-edit时):

git push -f origin feat-edit