git commit --amend

有时候可能Git提交后发现还有文件需要修改,当这个提交没有被推送到远程时,可以用此命令覆盖提交,避免commit记录混乱。

常用参数:

  • --no-edit:不编辑
  • --reset-author:重置提交用户为当前用户
  • --author=:修改Git 的 author和 email 信息。

示例

  1. 想修改提交的消息描述
git commit -amend -m <message>
  1. 想重新提交文件
git add .                         # 将修改的文件添加到暂存区
git commit -amend -m <message>    # 重新提交并修改消息

文档描述

--amend
Replace the tip of the current branch by creating a new commit. The recorded tree is prepared as usual (including the effect of the -i and -o options and explicit pathspec), and the message from the original commit is used as the starting point, instead of an empty message, when no other message is specified from the command line via options such as -m, -F, -c, etc. The new commit has the same parents and author as the current one (the --reset-author option can countermand this).
It is a rough equivalent for:
$ git reset --soft HEAD^
$ ... do something else to come up with the right tree ...
$ git commit -c ORIG_HEAD
but can be used to amend a merge commit.
You should understand the implications of rewriting history if you amend a commit that has already been published. (See the "RECOVERING FROM UPSTREAM REBASE" section in git-rebase[1].)

LnEoi
707 声望17 粉丝