Skip to content

Commit 319d835

Browse files
apkgitster
authored andcommitted
commit: make --only --allow-empty work without paths
--only is implied when paths are present, and required them unless --amend. But with --allow-empty it should be allowed as well - it is the only way to create an empty commit in the presence of staged changes. Signed-off-by: Andreas Krey <a.krey@gmx.de> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 0202c41 commit 319d835

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

Documentation/git-commit.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,8 @@ FROM UPSTREAM REBASE" section in linkgit:git-rebase[1].)
263263
If this option is specified together with `--amend`, then
264264
no paths need to be specified, which can be used to amend
265265
the last commit without committing changes that have
266-
already been staged.
266+
already been staged. If used together with `--allow-empty`
267+
paths are also not required, and an empty commit will be created.
267268

268269
-u[<mode>]::
269270
--untracked-files[=<mode>]::

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ static int parse_and_validate_options(int argc, const char *argv[],
12011201

12021202
if (also + only + all + interactive > 1)
12031203
die(_("Only one of --include/--only/--all/--interactive/--patch can be used."));
1204-
if (argc == 0 && (also || (only && !amend)))
1204+
if (argc == 0 && (also || (only && !amend && !allow_empty)))
12051205
die(_("No paths with --include/--only does not make sense."));
12061206
if (argc == 0 && only && amend)
12071207
only_include_assumed = _("Clever... amending the last one with dirty index.");

t/t7501-commit.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ test_expect_success 'amend --only ignores staged contents' '
155155
git diff --exit-code
156156
'
157157

158+
test_expect_success 'allow-empty --only ignores staged contents' '
159+
echo changed-again >file &&
160+
git add file &&
161+
git commit --allow-empty --only -m "empty" &&
162+
git cat-file blob HEAD:file >file.actual &&
163+
test_cmp file.expect file.actual &&
164+
git diff --exit-code
165+
'
166+
158167
test_expect_success 'set up editor' '
159168
cat >editor <<-\EOF &&
160169
#!/bin/sh

0 commit comments

Comments
 (0)