git修改历史提交的记录
修改最后一次提交
- 这可能是较为频繁的操作之一了
git commit --amend # 此时会弹出一个文件供你修改,弄就是了
- 这种方法类似于一个小的变基,如果已经推送了远程,则要小心使用这方法;
- 另外,此方法也可以用于,但你此次提交的意图是这样的:
feat(add): 更新了A文件,同时添加B文件
- 但提交后发现,B文件根本没提交,此时则可以通过以下操作,提交 B.filename
git add B.filename
git commit --amend
修改多个历史记录
- 这可能是一个比较复杂的操作,Git 需要通过变基来更改这一系列的提交
- 通过这个方法,不仅可以修改提交的评论信息,更可修改文件,添加文件等操作
- 改变最近三条历史记录
git rebase -i HEAD~3 # 实际上蹦出了4条提交记录
- 此时会蹦出一个文件,like this:
pick 4598a8e history commmit log..........
pick dd1692e history commmit log..........
pick ad15818 history commmit log..........
# Rebase 30d7d06..ad15818 onto 30d7d06 (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
# 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
- 一般来讲,使用
p/r/e
即可满足需要,使用r
,则可使用此条commit msg
- 使用
e
,则得使用--amend
与rebase continue
来手动执行顺序
注意
- 如变基所言,它会将分支结构图干掉,使得最终结果看起来就像是串行工作提交的历史记录一样,切记。