Git中修改commit内容的方式

331 阅读2分钟

1.可以先 git log查看一下日志情况

git log

2.修改最新一次提交时

git commit --amend

之后会看到下面这些信息

1646824278(1).png

然后按E键即可看见下面信息

1646824412(1).png

之后按i键进行修改,修改完毕之后按ESc即可保存,然后按:wq退出编辑 这个时候可以git log再看一下commit描述,你会发现应该修改了

3.修改某次提交时

修改最近三条commit信息

git rebase -i HEAD~3          此处的3至需要修改commit的条数(倒数3条,也就是离最新commit最近的3条commit)

之后你会看到下面这些信息

  1 pick 2275781 should find method from parent
  2 pick 223fc80 unit test case
  3 pick 9ac1179 update test case
  4
  5 # Rebase 79db0bd..9ac1179 onto 79db0bd (3 commands)
  6 #
  7 # Commands:
  8 # p, pick = use commit
  9 # r, reword = use commit, but edit the commit message
 10 # e, edit = use commit, but stop for amending
 11 # s, squash = use commit, but meld into previous commit
 12 # f, fixup = like "squash", but discard this commit's log message
 13 # x, exec = run command (the rest of the line) using shell
 14 # d, drop = remove commit
 15 #
 16 # These lines can be re-ordered; they are executed from top to bottom.
 17 #
 18 # If you remove a line here THAT COMMIT WILL BE LOST.
 19 #
 20 # However, if you remove everything, the rebase will be aborted.
 21 #
 22 # Note that empty commits are commented out

然后你可以按i键进行编辑修改,然后把1、2、3行中的任意一个pick换成edit,再按ESC即可保存,然后按:wq退出编辑

这个时候你可以用git指令来打开刚才你换成edit的那个commit信息了

git commit --amend   打开commit信息编辑视图,之后你会看见下面途中的情况

1646824278(1).png

这个时候按E键进入编辑,会看到下面这种信息 1646824412(1).png 之后按 i 键进行修改,修改完毕之后按ESc即可保存,然后按:wq退出编辑;如果修改完成后,执行git rebase --continue,之后会看到下面的情况

client_java git:(fix_aop_no_class_defined) git rebase -i HEAD~3
Stopped at 2275781...  should find method from parent
You can amend the commit now, with

  git commit --amend

Once you are satisfied with your changes, run

  git rebase --continue
➜  client_java git:(2275781)

这个时候可以使用git log查看一下是否commit信息改变

最后push 到远程仓库,-f强制推送,这种方式可能会将远程分支覆盖,存在一定危险

git push origin 你的远程分支名 -f