git commit --amend不会提交更改。

5
任何时候我在本地做出更改并执行git commit --amend,然后使用:x保存并退出vim,或者仅仅执行git commit --amend --no-edit都不起作用。没有任何本地更改被提交或暂存。我完全不知道如何可能发生这种情况。其他rebase操作正常使用编辑器。

以下是完整的可工作的重现步骤:

amendwtf|master ⇒ ls
file
amendwtf|master ⇒ git status
On branch master
nothing to commit, working tree clean
amendwtf|master ⇒ echo 'a change appears' > file
amendwtf|master⚡ ⇒ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file

no changes added to commit (use "git add" and/or "git commit -a")
amendwtf|master⚡ ⇒ git commit --amend --no-edit
[master 3b577b8] initial commit
 Date: Thu Mar 14 09:49:08 2019 -0400
 1 file changed, 1 insertion(+)
 create mode 100644 file
amendwtf|master⚡ ⇒ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   file

no changes added to commit (use "git add" and/or "git commit -a")
amendwtf|master⚡ ⇒

Git和Vim版本:

amendwtf|master⚡ ⇒ git --version
git version 2.21.0
amendwtf|master⚡ ⇒ vim --version
VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Nov 29 2017 18:37:46)
Included patches: 1-503, 505-680, 682-1283
2个回答

3
你还没有提交任何内容(使用 git add 文件),而且你没有在提交命令行上输入任何文件名,因此 git commit --amend 只会编辑提交信息、提交日期/时间以及可能在命令行中列出的其他内容(在你的情况下没有)。如果你想提交对文件的更改,请先添加文件或将其指定在提交命令行上。

3

我忘记了一个 -a。我想要的是 git commit -a --amend --no-edit


2
我建议不要使用-a。相反,先运行git add -u——这就是git commit -a所做的,但现在您可以运行git status并确保输出反映了您希望在下一个git commit中发生的情况,无论是使用--amend还是不使用。 (当然,一旦您熟悉了git add -u,并且确定它将执行您想要的操作,那么git commit -a就没问题了。不过我自己仍然使用-u!) - torek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接