Git修改/ reword(不添加/更改文件) [英] Git amend/reword (without adding/changing files)

查看:2096
本文介绍了Git修改/ reword(不添加/更改文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 通常我想编辑提交消息,而不必重新选择上次提交的文件集。 git commit file1.c file2.c 

提交消息中的意外错字。

  git commit file1.c file2.c --amend 

这有效,但我喜欢不必从原始提交重新选择文件集,我曾经意外地做过 git commit -a --amend

我知道 git rebase -i HEAD〜1 然后替换 code> pick r (re-word),但这最后只需几步。



有没有一种方法可以在不包含任何新文件的情况下在一个步骤中重新提交上次提交?

解决方案

修改消息而不进行分阶段更改



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

  git commit --amend 

编辑您之前提交的消息
$ b

使用修改消息甚至



但是,如果你做了改变,那么你可以使用 - 仅(或者 -o )标志与组合 - 修改只编辑上一次提交的消息,而不提交阶段性更改:

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

这个选项是由 David Ongaro在他的答案中。



文档



正如 git commit 文档(强调我的):


  -o 
- 仅限

仅从命令行中指定的路径进行提交,忽略到目前为止已经执行的任何内容。如果在命令行中给出任何路径
,这是
git commit 的默认操作模式,在这种情况下,此选项可以省略。 如果
这个选项和 - 修改一起指定,那么没有路径需要指定
,这可以用来修改上一次提交而不用
提交已经进行的更改。


1 Minitech 等。


Often I want to edit a commit message without having to re-select the file-set from the last commit.

git commit file1.c file2.c

Accidental typo in commit message.

git commit file1.c file2.c --amend

This works, but Id like not to have to re-select the file-set from the original commit, I once accidentally did git commit -a --amend and added many changes unintentionally.

I know about git rebase -i HEAD~1 then replace pick with with r (re-word), but this ends up being a few steps.

Is there a way to re-word the last commit in one step without including any new files?

解决方案

Amending message without staged changes

As long as you don't have any changes staged in your staging area, you can simply use

git commit --amend

to edit your previous commit's message1.

Amending message even with staged changes

However, if you do have changes staged, then you can use the --only (or -o) flag in combination with --amend to only edit the message of the previous commit, without committing staged changes:

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

This option was pointed out by David Ongaro in his answer.

Documentation

As stated in the git commit documentation (emphasis mine):

-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.

1As mentioned by Minitech and others.

这篇关于Git修改/ reword(不添加/更改文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆