git 如何将多个提交点合并为一个提交点 commit

文章目录

核心命令

git merge branch2 是将分支branch2的提交点合并到本地当前分支。

而在执行这条命令的时候,加一个选项--squash就表示在合并的时候将多个提交点合并为一个提交点。
git merge --squash branch2

先看squash单词的意思:拥挤,使......拥挤。

以下为git merge --help中查看到的--squash选项的功能:

shell 复制代码
--squash, --no-squash
   Produce the working tree and index state as if a real merge happened (except for the merge information),
   but do not actually make a commit, move the HEAD, or record $GIT_DIR/MERGE_HEAD (to cause the next git
   commit command to create a merge commit). This allows you to create a single commit on top of the current
   branch whose effect is the same as merging another branch (or more in case of an octopus).

   With --no-squash perform the merge and commit the result. This option can be used to override --squash.

详细使用

模式总结

shell 复制代码
# 1. 先检出目标分支
git checkout <目标分支名>
# 2. 查看当前的本地分支(确认一下分支是否转换成功)
git branch
# 3. 将需合并分支的代码合并到本地分支
git merge -squash<有多个提交点的、需要合并为一个提交点的分支名>
# 4. 查看本地git文件变动(一定会有变动)
git status
# 5. 将新的本地代码变动提交(为一个新的提交点)
git commit -m "提交信息"git push. 推送
# 6. 推送代码到远程服务器
git push

以上6行命令,核心步骤就是3和5。

示例

目标分支/本地当前分支:master

有多个提交点的、想要合并过来的分支:feature/bug_xxx_fix

shell 复制代码
git checkout master
git branch
git merge -squash feature/bug_xxx_fix
git status
git commit -m "fix bug xxx"
git push
相关推荐
云雀衔光5 小时前
多个 MCP Server 怎么编排:数据库 / Redis / Git / 飞书一把梭
java·数据库·人工智能·redis·git·语言模型·飞书
Java后端的Ai之路6 小时前
Git pull弹出vim编辑器完整排查指南
开发语言·人工智能·git·编辑器·vim
云泽80815 小时前
Git 版本控制系统(下):从 .git 目录结构到冲突解决机制详解
大数据·git·elasticsearch
麻辣布丁1 天前
Git冲突原因与解决方法全解
大数据·git·elasticsearch
一支黑色の铅笔1 天前
VSCode 克隆Git项目
ide·git·vscode
拽着尾巴的鱼儿1 天前
Idea:Cannot Run Git 无法运行git
java·git·intellij-idea
y小川1 天前
【git】移除远端已删除的git分支
git
云边有个稻草人2 天前
SQL Server数据库迁移:金仓KES V9R4C019以深度T‑SQL兼容实现应用极简改造
merge·金仓数据库·数据库国产化·sql server数据库迁移·kes v9r4c019·t‑sql兼容·数据库迁移实践
OsDepK2 天前
项目快速Git至仓库(完整版)
大数据·git·elasticsearch·搜索引擎
妙码生花2 天前
使用git更新ai-go-admin框架
前端·人工智能·git·golang·typescript·php