Skip to content

Commit 49b9362

Browse files
peffgitster
authored andcommitted
git-reset: refuse to do hard reset in a bare repository
It makes no sense since there is no working tree. A soft reset should be fine, though. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 02e5ba4 commit 49b9362

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

builtin-reset.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
244244
if (reset_type == NONE)
245245
reset_type = MIXED; /* by default */
246246

247+
if (reset_type == HARD && is_bare_repository())
248+
die("hard reset makes no sense in a bare repository");
249+
247250
/* Soft reset does not touch the index file nor the working tree
248251
* at all, but requires them in a good order. Other resets reset
249252
* the index file to the tree object we are switching to. */

t/t7103-reset-bare.sh

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/sh
2+
3+
test_description='git-reset in a bare repository'
4+
. ./test-lib.sh
5+
6+
test_expect_success 'setup non-bare' '
7+
echo one >file &&
8+
git add file &&
9+
git commit -m one &&
10+
echo two >file &&
11+
git commit -a -m two
12+
'
13+
14+
test_expect_success 'setup bare' '
15+
git clone --bare . bare.git &&
16+
cd bare.git
17+
'
18+
19+
test_expect_success 'hard reset is not allowed' '
20+
! git reset --hard HEAD^
21+
'
22+
23+
test_expect_success 'soft reset is allowed' '
24+
git reset --soft HEAD^ &&
25+
test "`git show --pretty=format:%s | head -n 1`" = "one"
26+
'
27+
28+
test_done

0 commit comments

Comments
 (0)