Git提交合并提交及注释

简介: Git提交合并提交及注释

本地开发时,可以随时去提交写好的代码,但这样会导致提交历史比较多,推送到远端或者发起Pull Request显得比较杂乱,这时就可以使用rebase命令将几次提交或者全部提交合并成一次提交。


rebase可提升工作效率,也可能会给其他人带来麻烦。


作用:可以对某一段线性提交历史进行编辑、删除、复制、粘贴。


目标:合理使用rebase命令可以使提交历史干净、简洁。


使用范围:适用于本地提交的commit。不要使用rebase对已经提交到公共仓库中的commit进行修改。


第一步:执行git log或git status查看代码的提交状况。


git status可以看到本地有2次commit:


(base) appledeMacBook-Pro-2:beta-parent apple$ git status

On branch master

Your branch is ahead of 'origin/master' by 2 commits.

 (use "git push" to publish your local commits)

1

2

3

4

git log可以查看每次提交的注释信息:


(base) appledeMacBook-Pro-2:beta-parent apple$ git log
commit 23bad176cc6dfc95ab63b90c365a636143a865c2 (HEAD -> master)
Author: secbro <xxx@126.com>
Date:   Wed Sep 8 15:10:04 2021 +0800
    优化代码-业务逻辑判断
commit f29b55dcf832fab9ef94df06dc4b17d1dc4e273f
Author: secbro <xxx@126.com>
Date:   Wed Sep 8 15:08:02 2021 +0800
    优化代码-移除无用代码

查看到有2次提交,现将将这2次提交的注释进行合并。

第二步:修改提交注释命令

输入git rebase -i HEAD~N(N=需要合并的最后几次提交)。

git rebase -i HEAD~2
• 1

输出如下:

pick f29b55dc 优化代码-移除无用代码
pick 23bad176 优化代码-业务逻辑判断
# Rebase 227b1465..23bad176 onto 227b1465 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's

第三步:根据Commands注释修改成预期效果

将第二次提交注释命令改为squash:

pick f29b55dc 优化代码-移除无用代码
squash 23bad176 优化代码-业务逻辑判断
• 1
• 2

第四步:保存退出

输入:wq保存退出,出现如下提交信息:

# This is a combination of 2 commits.
# This is the 1st commit message:
优化代码-移除无用代码
# This is the commit message #2:
优化代码-业务逻辑判断

第五步:修改实际提交信息


其中#开头的为注释,可以不用管。将上面两行注释修改为一行:


# This is a combination of 2 commits.

# This is the 1st commit message:


优化代码-移除无用代码和添加TODO


1

2

3

4

5

第六步:再次保存退出


再次输入:wq就会看到合并完毕的提示信息:


(base) appledeMacBook-Pro-2:beta-parent apple$ git rebase -i HEAD~2

[detached HEAD e509fc87] 优化代码-移除无用代码和添加TODO

Date: Wed Sep 8 15:08:02 2021 +0800

2 files changed, 19 insertions(+), 31 deletions(-)

Successfully rebased and updated refs/heads/master.

1

2

3

4

5

再次执行git log或git status查看代码的提交状况会发现已经变为1次提交了。说明Rebase合并成功,2次提交合并成一次提交;


第七步:提交代码


此时可通过git push提交代码了。



目录
相关文章
|
2天前
|
开发工具 git
Git操作远程仓库及解决合并冲突
Git操作远程仓库及解决合并冲突
96 0
|
7月前
|
开发工具 git
IDEA中Git面板操作介绍 变基、合并、提取、拉取、签出
在IDEA的Git面板中,仓库会分为本地仓库和远程仓库,代码仓库里面放的是各个分支。
575 2
|
2天前
|
存储 开发工具 git
Pycharm git-创建本地仓库\创建分支\合并分支\回溯版本\加入git后文件颜色代表的含义
Pycharm git-创建本地仓库\创建分支\合并分支\回溯版本\加入git后文件颜色代表的含义
75 0
|
2天前
|
数据可视化 Go 开发工具
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
cggit 简化 Git 提交、合并、分支偏移小神器,提升开发、修BUG效率!
43 0
|
2天前
|
开发工具 git
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
git 拉取代码仓库代码报错(合并错误 refusing to merge unrelated histories)
26 0
|
2天前
|
数据可视化 开发工具 git
|
6月前
|
开发工具 git Windows
Git分支新建与合并案例实操(结合IDEA讲解)
Git分支新建与合并案例实操(结合IDEA讲解)
172 0
|
2天前
|
Shell 开发工具 git
【问题篇】git创建分支后idea切换分支找不到以及合并问题
【问题篇】git创建分支后idea切换分支找不到以及合并问题
38 0
|
2天前
|
存储 开发工具 git
Git 教程:解密 .gitignore 文件、合并分支、解决冲突、及 Git 帮助
如果你忘记了命令或命令的选项,你可以使用 Git 帮助。 在命令行中,有几种不同的使用帮助命令的方式: git command -help - 查看特定命令的所有可用选项 git help --all - 查看所有可能的命令 让我们看看不同的命令。
215 3

相关实验场景

更多