threads / discuss / 59989

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

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

## tl;dr

6 messages between Jul 15, 2023 and Jul 17, 2023.

replies: 5people: 5as markdown or json

Yuri· Jul 15, 2023, 04:26 UTC · lore

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· Jul 16, 2023, 00:24 UTC · re: Yuri · lore

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

On 2023-07-15 at 04:26:46, Yuri wrote:
Show 10 quoted lines
> 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· Jul 16, 2023, 01:15 UTC · re: brian m. carlson · lore

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

"brian m. carlson" <sandals@crustytoothpaste.net> writes:
Show 13 quoted lines
>> 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· Jul 17, 2023, 00:16 UTC · re: brian m. carlson · lore

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

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· Jul 17, 2023, 09:18 UTC · re: brian m. carlson · lore

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

"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· Jul 17, 2023, 16:35 UTC · re: Konstantin Khomoutov · lore

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

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."

← back to recent threads