一、git commit --amend
本地修改文件,并提交:
git add .
git commit -m "message"
此时:
如果发现 message 漏掉了一些信息,可以使用:
git commit --amend -m "message ++++++++++ other message"
如果发现有额外的文件漏掉,需要补充在上次的提交里,可以使用:
git add .
git commit --amend --no-edit
如果需要同时修改message,并提交文件,可以使用
git add .
git commit --amend -m "补充遗漏文件 message.file "
通过git commit --amend可以避免生成无用的 commit-id,简化git log的记录,便于追踪。
二、git rebase -i HEAD~n
如果有多次提交,此时想合并 commit-id,可以使用:git rebase -i HEAD~n
例子:
git log:看到有下面三次提交
"test3"
"test2"
"tes t1"
git rebase -i HEAD~3
生成:
pick 9e3c5b3a tes t1 pick ac139d94 test2 pick 2d0f7f64 test3 # Rebase 73a555fb..2d0f7f64 onto 73a555fb (3 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 # . 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
合并 commit-id 最常用的是 squash 和 fixup, 前者包含 commit message,后者不包含。
修改vim文件为:
pick 9e3c5b3a tes t1
s ac139d94 test2
f 2d0f7f64 test3
wq保存退出
上述表示:test3合并到test2,且不保留log message;test2合并到tes t1,保留commit
生成:
# This is a combination of 3 commits.
# This is the 1st commit message:
tes t1
# This is the commit message #2:
test2
# The commit message #3 will be skipped:
# test3
# Please enter the commit message for your changes. Lines starting
此时,可以修改 tes t1 和 test2 这两次commit的log。删掉test2,tes t1修改为test5678。保存退出。
git log:
只有一条 test5678 log message,而tes t1,test2,test3 这三次修改的内容也被合并了。