修改提交备注
修改最近一次提交的备注
如果 commit
后还未 push
发现提交的信息表述错误或者有偏差,这时候想把备注信息改掉:
git commit --amend
进入编辑页面,一般是使用的默认的 vim 编辑器- 编辑完成了保存
git push
修改历史提交备注
git commit --amend
选项提供了最后一次 commit
的反悔的机会。
如果要修改历史备注信息,可以通过rebase -i
,但是这种方式有个很严重的缺点:push
的时候只能强制push才能生效,如果是协作开发,强制push很可能会影响到其他人,很多时候是被禁止的。
具体步骤如下:
-
git rebase -i HEAD~n
orgit rebase -i Revision Number
-
执行之后会进入编辑页面,内容大致如下
pick f312c035 commmit_message_3 pick 81729e86 commmit_message_2 pick 52195054 commmit_message_1 # # 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 # 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 # . message (or the oneline, if no original merge commit was # . specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # # Note that empty commits are commented out
-
把指定提交前面的
pick
改为edit
,保存 退出 (vim 的操作方式) -
保存之后会提示一下信息
You can amend the commit now, with git commit --amend Once you are satisfied with your changes, run git rebase --continue
-
git commit --amend
修改备注信息,git rebase --continue
继续下一个,也可以通过git rebase --abort
取消这次修改 -
git rebase --continue
过程中很可能出现冲突,解决完之后继续git rebase --continue
直到出现 No rebase in progress? -
rebase
结束之后,版本历史实际上落后于origin
的,这时候就需要强制push 覆盖origin
的版本(强制push 是一个危险操作,需要慎用)
merge 时 修改备注
在合并分支的时候,如果被合并分支上的提交备注很随意,并没有实际的参考意义,可以 通过 --squash
把合并过来的内容,压缩成一次提交。
--squash
选项的含义是:本地文件内容与不使用该选项的合并结果相同,但是不提交、不移动HEAD,因此需要一条额外的commit命令。其效果相当于将被合并分支上的多个commit合并成一个,放在当前分支上,原来的commit历史则没有拿过来。
判断是否使用
--squash
选项最根本的标准是,待合并分支上的历史是否有意义。—— 懒惰的程序员 git merge –squash介绍