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
相关推荐
小龙6 小时前
【Git 报错解决】本地无有效提交无法推送(`src refspec main does not match any`)
git·github·报错
小扶苏6 小时前
删除git全局账号信息并设置成新的账号密码命令
git
Greg_Zhong9 小时前
Git创建任务分支进行开发,最后合并主分支master【纯git命令执行过程】阐述
git
眯眼因为很困啦10 小时前
GitHub Fork 协作完整流程
前端·git·前端工程化
AlexDeng12 小时前
Git 中模糊搜索分支名称并创建本地跟踪分支
git
jxm_csdn16 小时前
递归工程工厂:Claude Code + Git Worktrees + Tilix/Tmux 的“AI分身”编码团队
人工智能·git
码咔吧咔16 小时前
Git 中 pull.rebase = true 的作用与设置方法详解
git
郑州光合科技余经理19 小时前
开发实战:海外版同城o2o生活服务平台核心模块设计
开发语言·git·python·架构·uni-app·生活·智慧城市
我是海飞19 小时前
Git 本地仓库推送到远程空仓库指南
大数据·git·elasticsearch
记得开心一点嘛19 小时前
如何去使用git进行协同开发?
git