Git modify / modify (without adding / changing files)

Often times I want to edit a commit message without reselecting a set of files from the last commit.

git commit file1.c file2.c

      

Accidental typo in the commit message.

git commit file1.c file2.c --amend

      

This works, but Id not seem to have to re-fetch a set of files from the original commit, I accidentally did one day git commit -a --amend

and added a lot of changes unintentionally.

I know about git rebase -i HEAD~1

, then change pick

to r

(word repeated), but that ends with a few steps.

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

+3


source to share


3 answers


Modifying a message without staging changes

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

git commit --amend

      

to edit your previous commit message 1 .

Change message even with phased changes

However, if you have changes, you can use the flag --only

(or -o

) in combination with --amend

just to edit the message of the previous commit, without making the staged changes:



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

      

This option was noted 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 given on the command line, disregarding any content that's been done so far. This is the default mode of git commit if any paths are specified on the command line, in which case this option can be omitted. If this option is specified with --amend, then no paths are needed which can be used to modify the last commit without those already supplied.

1As mentioned by Minitech et al.

+4


source


Just use git commit --amend

without any other arguments. It will open an editor and you can change the commit message.



+2


source


You can do git commit --amend -m <new commit message>

. Of course, a precaution is to ensure that there are no incremental changes mentioned in other answers.

0


source







All Articles