Skip to content

Commit f5a8400

Browse files
committed
Merge branch 'jk/maint-commit-amend-only-no-paths' into maint
"git commit --amend --only --" was meant to allow "Clever" people to rewrite the commit message without making any change even when they have already changes for the next commit added to their index, but it never worked as advertised since it was introduced in 1.3.0 era. * jk/maint-commit-amend-only-no-paths: commit: fix "--amend --only" with no pathspec
2 parents 1cd2913 + ea2d4ed commit f5a8400

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

builtin/commit.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
184184
int i;
185185
char *m;
186186

187+
if (!pattern)
188+
return 0;
189+
187190
for (i = 0; pattern[i]; i++)
188191
;
189192
m = xcalloc(1, i);
@@ -345,7 +348,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,
345348
* and create commit from the_index.
346349
* We still need to refresh the index here.
347350
*/
348-
if (!pathspec || !*pathspec) {
351+
if (!only && (!pathspec || !*pathspec)) {
349352
fd = hold_locked_index(&index_lock, 1);
350353
refresh_cache_or_die(refresh_flags);
351354
if (active_cache_changed) {

t/t7501-commit.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ test_expect_success 'amend commit' '
108108
EDITOR=./editor git commit --amend
109109
'
110110

111+
test_expect_success 'amend --only ignores staged contents' '
112+
cp file file.expect &&
113+
echo changed >file &&
114+
git add file &&
115+
git commit --no-edit --amend --only &&
116+
git cat-file blob HEAD:file >file.actual &&
117+
test_cmp file.expect file.actual &&
118+
git diff --exit-code
119+
'
120+
111121
test_expect_success 'set up editor' '
112122
cat >editor <<-\EOF &&
113123
#!/bin/sh

0 commit comments

Comments
 (0)