# Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository

6 messages from 2023-07-15 to 2023-07-17. Participants: Yuri, brian m. carlson, Junio C Hamano, Konstantin Khomoutov, Andreas Schwab.
Thread: https://gitlist.dev/t/59989

## Yuri, 2023-07-15 04:26

Subject: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <fe3c68d5-124e-5a87-881a-21ad8e492f76@tsoft.com>
URL: https://gitlist.dev/e/fe3c68d5-124e-5a87-881a-21ad8e492f76%40tsoft.com

```
It stops in some intermediate state, and git still says that it is on 
the main branch, but 'git checkout' deletes files that were added only 
in the main branch,

'git reset --hard HEAD' fixes the main branch, bit now it is impossible 
to switch to the other branch because it says that "some files would be 
overwritten", which shouldn't be the case.


All operations should be atomic.

When the user presses Ctrl-C, the correct action would be to cleanly 
return to the initial branch.


git-2.41.0


Thanks,

Yuri



```

## brian m. carlson, 2023-07-16 00:24

Subject: Re: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <ZLM4sTUjBQt4QMfG@tapette.crustytoothpaste.net>
URL: https://gitlist.dev/e/ZLM4sTUjBQt4QMfG%40tapette.crustytoothpaste.net
In-Reply-To: <fe3c68d5-124e-5a87-881a-21ad8e492f76@tsoft.com>

```
On 2023-07-15 at 04:26:46, Yuri wrote:
> It stops in some intermediate state, and git still says that it is on the
> main branch, but 'git checkout' deletes files that were added only in the
> main branch,
> 
> 'git reset --hard HEAD' fixes the main branch, bit now it is impossible to
> switch to the other branch because it says that "some files would be
> overwritten", which shouldn't be the case.
> 
> 
> All operations should be atomic.

This is impossible, since POSIX doesn't provide the functionality for us
to perform operations atomically.  There are various reasons, including
permissions and files differing in case on a case-insensitive system,
why an operation might not succeed part way through.

> When the user presses Ctrl-C, the correct action would be to cleanly return
> to the initial branch.

I would disagree here.  When the user has hit Ctrl-C, they want to
interrupt the operation.  That's literally why a SIGINT (interrupt)
signal is sent.  A checkout can take a long time, and the user will not
want Git to perform an operation which will take even longer than the
original one (because the original checkout was aborted).

Even if we did that, the user could just hit Ctrl-C again and really
interrupt the process, and then they'd be stuck again.

If you don't want to interrupt the operation, then don't hit Ctrl-C.
-- 
brian m. carlson (he/him or they/them)
Toronto, Ontario, CA

```

## Junio C Hamano, 2023-07-16 01:15

Subject: Re: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <xmqqedl8lkqu.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqedl8lkqu.fsf%40gitster.g
In-Reply-To: <ZLM4sTUjBQt4QMfG@tapette.crustytoothpaste.net>

```
"brian m. carlson" <sandals@crustytoothpaste.net> writes:

>> When the user presses Ctrl-C, the correct action would be to cleanly return
>> to the initial branch.
>
> I would disagree here.  When the user has hit Ctrl-C, they want to
> interrupt the operation.  That's literally why a SIGINT (interrupt)
> signal is sent.  A checkout can take a long time, and the user will not
> want Git to perform an operation which will take even longer than the
> original one (because the original checkout was aborted).
>
> Even if we did that, the user could just hit Ctrl-C again and really
> interrupt the process, and then they'd be stuck again.
>
> If you don't want to interrupt the operation, then don't hit Ctrl-C.

I agree with all of the above, but stopping with "don't" is not very
helpful---people do do things that they are told not to anyway, and
it makes a whole lot of difference if they know how to recover from
the fallout of their actions.  It would help to teach "reset --hard"
or something that lets the user to return to a known state.  It may
not necessarily be the state the user would want to go, but it is
still better to be in a known stable state and be able to complain
"I lost my stashed changes" or "I lost a few commits" than to be in
a state where the user is totally lost and do not know what to do
next.

Of course, that kind of coaching is not something we should do in
our error or advise messages, but in an early part of the tutorial
or somewhere, perhaps?

Thanks.

```

## Yuri, 2023-07-17 00:16

Subject: Re: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <9e5b5818-6107-134f-b4ce-3a6028417838@tsoft.com>
URL: https://gitlist.dev/e/9e5b5818-6107-134f-b4ce-3a6028417838%40tsoft.com
In-Reply-To: <ZLM4sTUjBQt4QMfG@tapette.crustytoothpaste.net>

```
On 7/15/23 17:24, brian m. carlson wrote:
> If you don't want to interrupt the operation, then don't hit Ctrl-C.


A more comprehensive way to handle this is to offer the user a choice:

Ctrl-C was pressed during a long operation. Please choose:

(1) press Ctrl-C again to stop immediately while likely leaving the 
repository in inconsistent state

(2) press C to continue

(3) press R to roll back the current operation


And if the user would press Ctrl-C again during the rollback - he would 
be presented with choices:

Ctrl-C was pressed during the roll back of a long operation. Please choose:

(1) press Ctrl-C again to stop immediately while likely leaving the 
repository in inconsistent state

(2) press C to continue the rollback


This would be a lot better than to just stop immediately and leave the 
repository damaged.



Yuri





```

## Konstantin Khomoutov, 2023-07-17 09:18

Subject: Re: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <20230717091849.z7bvqbygbpg4sluk@carbon>
URL: https://gitlist.dev/e/20230717091849.z7bvqbygbpg4sluk%40carbon
In-Reply-To: <ZLM4sTUjBQt4QMfG@tapette.crustytoothpaste.net>

```
"brian m. carlson" <sandals@crustytoothpaste.net> writes:

> I would disagree here.  When the user has hit Ctrl-C, they want to
> interrupt the operation.  That's literally why a SIGINT (interrupt)
> signal is sent.

Just a fun remark: that "INT" in SIGINT stands for "INTeractive attention",
and IIUC, relabeling it as a request for interruption in the public
consciousness is likely the result of that signal having been left in its
default disposition in most of the software which was in use then, which
naturally made the signal work as a termination signal ;-)


```

## Andreas Schwab, 2023-07-17 16:35

Subject: Re: Pressing Ctrl-C during 'git checkout <branch-name>' messes up the repository
Message-ID: <87bkgalcn8.fsf@igel.home>
URL: https://gitlist.dev/e/87bkgalcn8.fsf%40igel.home
In-Reply-To: <20230717091849.z7bvqbygbpg4sluk@carbon>

```
On Jul 17 2023, Konstantin Khomoutov wrote:

> Just a fun remark: that "INT" in SIGINT stands for "INTeractive attention",

Do you have a source for that?  The oldest manpage for signal(2)
available at man.freebsd.org describes it as "interrupt".

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

```
