# exit code from git reset

3 messages from 2012-12-09 to 2012-12-09. Participants: Martin von Zweigbergk, Junio C Hamano.
Thread: https://gitlist.dev/t/32304

## Martin von Zweigbergk, 2012-12-09 22:23

Subject: exit code from git reset
Message-ID: <CANiSa6i0LXE18Pyb5norRTm7PM+TMo3JvxDjoS5JOWt_qjHLHw@mail.gmail.com>
URL: https://gitlist.dev/e/CANiSa6i0LXE18Pyb5norRTm7PM%2BTMo3JvxDjoS5JOWt_qjHLHw%40mail.gmail.com

```
Hi,

"git reset" currently returns 0 (if successful) while "git reset
$pathspec" returns 0 iff the index matches HEAD after resetting (on
all paths, not just those matching $pathspec). The exit code doesn't
seem to be documented. Is there a reason they should behave
differently? If not, what would be the better behavior? I don't see
when the "git reset $pathspec" behavior" would be useful and it seems
safer for any existing scripts out there not to change the status code
for "git reset" (w/o paths). Scripts that run "git reset $pathspec"
probably already know to ignore (or use?) the exit code. Also, having
any non-zero exit code signal failure might actually be useful.

Changing "git reset $pathspec" to return 0 on success, regardless of
diff between HEAD and index, breaks 10 test cases (in
t2013-checkout-submodule.sh and t7102-reset.sh). These seem to do
"test_must_fail git reset $pathspec", but I have not been able to find
any motivation for expecting the failure.

It seems like this behavior has been there at least since the tests
were added in 359048d (Add tests for documented features of "git
reset"., 2007-09-11), just before reset became built-in.

Would a patch to change the exit code from "git reset $pathspec" be appreciated?

Martin

```

## Junio C Hamano, 2012-12-09 23:04

Subject: Re: exit code from git reset
Message-ID: <7vzk1mddzo.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzk1mddzo.fsf%40alter.siamese.dyndns.org
In-Reply-To: <CANiSa6i0LXE18Pyb5norRTm7PM+TMo3JvxDjoS5JOWt_qjHLHw@mail.gmail.com>

```
Martin von Zweigbergk <martinvonz@gmail.com> writes:

> "git reset" currently returns 0 (if successful) while "git reset
> $pathspec" returns 0 iff the index matches HEAD after resetting (on
> all paths, not just those matching $pathspec).

So in short, you observed that either of them reports with its exit
code if the resulting index (not just any subpart, but always the
entire thing) matches the HEAD, e.g. "do we have change that will be
listed on 'will be committed' section in git status output?"

Sounds like one sane and consistent semantics to me.  I am not
saying that there cannot be other behaviours that are internally
consistent (e.g. the error code could have matched the number of
paths that are different between the index and the HED, or the error
code could have been zero for successful reset, non-zero for some
failure), but I am saying that the current behaviour gives _one_
sane and consistent meanings regardless of how you ran the command.

> The exit code doesn't seem to be documented.

Please make it so.

> Changing "git reset $pathspec" to return 0 on success, regardless of
> diff between HEAD and index, breaks 10 test cases (in
> t2013-checkout-submodule.sh and t7102-reset.sh). These seem to do
> "test_must_fail git reset $pathspec", but I have not been able to find
> any motivation for expecting the failure.

See above.

```

## Martin von Zweigbergk, 2012-12-09 23:12

Subject: Re: exit code from git reset
Message-ID: <CANiSa6gPGGDWxpWGsjX0dgBA_SB7-gRKPb5o7EfK0h+DOZauYw@mail.gmail.com>
URL: https://gitlist.dev/e/CANiSa6gPGGDWxpWGsjX0dgBA_SB7-gRKPb5o7EfK0h%2BDOZauYw%40mail.gmail.com
In-Reply-To: <7vzk1mddzo.fsf@alter.siamese.dyndns.org>

```
On Sun, Dec 9, 2012 at 3:04 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Martin von Zweigbergk <martinvonz@gmail.com> writes:
>
>> "git reset" currently returns 0 (if successful) while "git reset
>> $pathspec" returns 0 iff the index matches HEAD after resetting (on
>> all paths, not just those matching $pathspec).
>
> So in short, you observed that either of them reports with its exit
> code if the resulting index (not just any subpart, but always the
> entire thing) matches the HEAD, e.g. "do we have change that will be
> listed on 'will be committed' section in git status output?"
>
> Sounds like one sane and consistent semantics to me.

Heh, true, the behavior according to my description does make sense,
it's just that my description was wrong; sorry :-(.

What "git reset $pathspec" returns is not "whether HEAD differs from
index" (as I wrote), but "whether worktree differs from index". So
"git reset" and "git reset ." will return different exit codes if
there are changes in the worktree as compared to HEAD before the
invocation. I hope that's clearer.

```
