squash Commits
时间: 2023-08-12 09:07:54 浏览: 359
Squash Commits 是一种将多个 Git 提交合并为一个的方法。使用 Squash Commits 可以减少 Git 提交历史中的杂乱无章,使 Git 历史更加清晰。下面是 Squash Commits 的具体步骤:
1. 使用 git log 命令查看需要合并的提交历史记录,并记录下需要合并的 commit ID。
2. 执行 git rebase -i <commit ID> 命令,其中 <commit ID> 是需要合并的 commit 的 ID。
3. 在弹出的编辑器中,将需要合并的提交的命令前面的 pick 改为 squash。例如,如果需要合并最近的三个提交,那么编辑器中的内容如下:
```
pick e499d89 Delete CNAME
squash 0c39034 Better README
squash f7fde4a Change the commit message but push the same commit.
```
将第二个和第三个提交的 pick 改为 squash,即:
```
pick e499d89 Delete CNAME
squash 0c39034 Better README
squash f7fde4a Change the commit message but push the same commit.
```
4. 保存并退出编辑器,Git 会将需要合并的提交合并为一个提交。如果有冲突需要解决,Git 会提示你进行解决。
5. 如果需要,在合并提交后,可以使用 git log 命令查看 Git 提交历史记录,确认提交是否已经合并。
6. 使用 git push origin <branch name> --force-with-lease 命令将合并后的提交推送到远程仓库。注意:由于使用了 --force-with-lease 参数,该操作会覆盖远程仓库的提交记录,因此需要谨慎操作。
以上就是 Squash Commits 的具体步骤。
阅读全文
相关推荐












