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
相关推荐
cui_hao_nan2 小时前
Git常用命令1
运维·git
xxwl5853 小时前
Git常用命令的学习
git·学习
攻城狮-申3 小时前
git本地分支对齐远程分支
前端·git
不怕犯错,就怕不做13 小时前
git prune 自动删除本地记录中那些远程已经不存在的分支引用
linux·服务器·git
ELI_He99918 小时前
如何还原已经推送的提交
git
️学习的小王19 小时前
Git项目提交忽略文件怎么做?以Python项目为例,详解.gitignore
git·python·elasticsearch
愚蠢得人20 小时前
Git Worktree实用指南:同时开发两个分支,不再反复切换
git
lskaixin1 天前
Git命令
git
_道隐_1 天前
git 如何把一个已经commit到本地分支的,被错误staged的文件,单独拿出来revert呢?
git
lingran__1 天前
Git 完全指南(一):本地仓库基础操作
linux·git·开发工具·devops·版本控制