注意:git rebase -i HEAD~3 与git log 出来的顺序是相反的,rebase出来的最下面是最新的,log是第一条是最新的。
git log 查看日志,复制需要更改的前一个commintId 使用
git rebase -i commitId
输入i进入编辑模式然后将pick需要改为reword(简写位r)或者其他的
可以看到,三个 commit 都列了出来,前面的 pick 就是指定怎么处理这个 commit。
- pick 是原封不动使用这个 commit
- reword 是使用这个 commit,但是修改 commit message
- edit 是使用这个 commit,但是修改这个 commit 的内容,然后重新 amend。
- squash 是合并这个 commit 到之前的 commit
后面的命令就不看了,很明显,这里我们要用的是 edit 命令。
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
然后wq保存后如果有冲突就使用idea解决冲突,然后可以使用git commit --amend 修改提交信息,如果其他没有改动也可以使用git commit --allow-empty 提交空的,或者不操作直接进行下一步操作,git rebase --continue。继续操作有冲突就解决然后下一步直到最后一步,然后最后强制推送到远程仓库,将本地覆盖远程仓库 git push --force
一、修改最近一次提交的commit信息
1.首先通过 git log 查看commit信息
2.使用指令:git commit --amend 进入命令模式(修改号commit信息保存后退出编辑模式)
- git push --force 到远程仓库(注意:必须加上 --force 如果只使用 git push 会报错)
二、修改最近两个或者两次上的commit信息
1.首先通过 git log 查看commit信息
2.使用指令:git rebase -i HEAD~2(把对应的 pick 改成 e 或者 edit 后保存)
3.使用 git commit --amend 去修改具体的commit信息
4.用 git rebase --continue 完成
5.git push --force 到远程仓库(注意:必须加上 --force 如果只使用 git push 会报错)
6.去远程仓库查看提交记录