git
git config
查看配置
git config --list
detail
init.defaultbranch=master # git config --global init.defaultbranch=master
user.email=xxx@xxxx.com
user.name=xxx
name
email
为一个账户配置则无需 --global
git config --global user.name "xxx"
git config --global user.email "xxx"
git squash
git 代码合并,压缩commit
要将 Git 中的 a
分支代码合并到 b
分支,并将所有的 commit 记录合并为一个提交,可以使用 git merge --squash
命令。这种方法会将 a
分支的所有更改压缩成一个单一的 commit。以下是详细步骤:
切换到目标分支
b
:git checkout b
使用
git merge --squash
将a
分支的更改合并到b
分支:git merge --squash a
这条命令会将
a
分支上的所有更改应用到b
分支上,但不会立即创建新的 commit。创建一个新的 commit:
git commit -m "Merged changes from branch 'a' into 'b'"
您可以在提交信息中写上合并的说明。
示例
假设 a
分支有多个 commit,现在我们要将这些 commit 合并到 b
分支上并且只生成一个新的 commit。
切换到
b
分支:git checkout b
将
a
分支的更改合并(压缩)到b
分支:git merge --squash a
输出类似于以下内容:
Squash commit -- not updating HEAD Automatic merge went well; stopped before committing as requested
创建一个新的 commit:
git commit -m "Merged changes from branch 'a' into 'b'"
现在,所有来自
a
分支的更改都被合并到b
分支中,并以一个单一的 commit 提交。
验证合并
您可以使用 git log
查看 b
分支的历史记录,确保合并操作正确执行:
git log
您应该能看到合并的单一 commit,其中包含来自 a
分支的所有更改。
注意事项
- 确保在合并之前同步分支: 在合并之前,确保
b
分支和a
分支都是最新的。可以使用git pull
更新分支。 - 冲突解决: 如果在
git merge --squash
过程中发生冲突,需要手动解决冲突并继续完成合并。
通过这种方式,您可以有效地将一个分支的所有更改压缩成一个单一的 commit 并合并到另一个分支。