gpt4 book ai didi

Git 修改/改写(不添加/更改文件)

转载 作者:太空狗 更新时间:2023-10-29 13:30:46 26 4
gpt4 key购买 nike

我经常想编辑提交消息,而不必从上次提交中重新选择文件集。

git commit file1.c file2.c

提交消息中的意外拼写错误。

git commit file1.c file2.c --amend

这行得通,但我不想从原始提交中重新选择文件集,我曾经不小心做了 git commit -a --amend 并无意中添加了许多更改。

我知道 git rebase -i HEAD~1 然后用 r 替换 pick (重写),但这最终只需几步。

有没有办法在不包含任何新文件的情况下一步重写最后一次提交?

最佳答案

在没有分阶段更改的情况下修改消息

只要您的暂存区域中没有任何更改,您可以简单地使用

git commit --amend

编辑您之前提交的消息1

甚至分阶段更改修改消息

但是,如果您确实已暂存更改,则可以将--only(或-o)标志与--amend 仅编辑上一次提交的消息,而不提交阶段性更改:

git commit --amend --only
git commit --amend -o # Shorter

David Ongaro in his answer 指出了这个选项。

文档

git commit documentation(强调我的)所述:

-o
--only

Make a commit only from the paths specified on the command line, disregarding any contents that have been staged so far. This is the default mode of operation of git commit if any paths are given on the command line, in which case this option can be omitted. If this option is specified together with --amend, then no paths need to be specified, which can be used to amend the last commit without committing changes that have already been staged.

1正如 Minitech 和其他人所提到的。

关于Git 修改/改写(不添加/更改文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25375679/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com