# Question: behavior when reverting a commit from a shallow clone

8 messages from 2026-10-03 to 2026-10-05. Participants: Sphinx, Carlisle T. Hamlin, Patrick Steinhardt, Matt Hunter, Junio C Hamano.
Thread: https://gitlist.dev/t/66454

## Sphinx, 2026-10-03 08:54

Subject: Question: behavior when reverting a commit from a shallow clone
Message-ID: <CALfz8Qx63qNoSbXq7C7u+KwX4=HCL7=uOUahpXd6j7KvW_c_Eg@mail.gmail.com>

```
Hi Git maintainers,

I have been investigating Git's behavior in a particular destructive
scenario and wanted to verify my understanding with the maintainers.

Consider the following repository history:

A -> B

where A contains the repository's files and B is the current HEAD.

The repository is then cloned with:

git clone --depth=1 <repository>

so only B is available locally and its parent A is not present in the
shallow clone.

If an operation is then performed to restore/revert B, I was looking
into the behavior when the resulting working tree/index becomes empty
— effectively causing all tracked files to be removed.

I have gone through the Git documentation and experimented with the
relevant Git commands, including the behavior of shallow repositories,
branch deletion, working-tree changes, resets, restores, and other
destructive operations. Based on my investigation, I have not been
able to find evidence that Git provides a warning or confirmation
specifically when an operation results in all tracked files being
removed or produces an empty tree.

Before drawing any conclusions, I wanted to verify this with the Git developers.

Is the following understanding correct?

An empty tree is a valid Git state, so Git does not generally consider
transitioning from a non-empty tree to an empty tree inherently
erroneous.

Git does not have a general safeguard that warns when an operation
will delete all tracked files.

If there are existing safeguards, warnings, configuration options, or
historical discussions that I may have missed, I would appreciate any
pointers.

The reason I am asking is that I am trying to establish precisely
where Git's safety boundary is in this scenario specifically, whether
Git itself is expected to warn about the resulting empty tree, or
whether detecting an unexpectedly destructive tree change is
considered the responsibility of the tooling performing the operation.

Thanks,
A fellow git user

```

## Carlisle T. Hamlin, 2026-10-03 19:41

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <54fac5f4-e49b-4384-af2c-615d7cc04ff5@gmx.com>
In-Reply-To: <CALfz8Qx63qNoSbXq7C7u+KwX4=HCL7=uOUahpXd6j7KvW_c_Eg@mail.gmail.com>

```
On 10/3/26 1:54 AM, Sphinx wrote:
> Is the following understanding correct?
> 
> An empty tree is a valid Git state, so Git does not generally consider
> transitioning from a non-empty tree to an empty tree inherently
> erroneous.
> 
> Git does not have a general safeguard that warns when an operation
> will delete all tracked files.
> 
> If there are existing safeguards, warnings, configuration options, or
> historical discussions that I may have missed, I would appreciate any
> pointers.
> 
> The reason I am asking is that I am trying to establish precisely
> where Git's safety boundary is in this scenario specifically, whether
> Git itself is expected to warn about the resulting empty tree, or
> whether detecting an unexpectedly destructive tree change is
> considered the responsibility of the tooling performing the operation.
You know, it seems to me that it should be reasonable for Git to assume 
that someone with the wherewithal to set up and operate a git repository 
(or at the very least operate one that someone else set up) knows enough 
to understand what's going to happen if they obliterate the only commit 
in their tree.

There really is only *so* much holding of the hand I think we should be 
expected to perform before it's not only insulting to the project 
developers, but also to the *user*.

My two cents. I know folk use Git for all sorts of stuff. Just, maybe... 
the sort of person who would be surprised catastrophically by this 
behaviour... well... shouldn't.



```

## Patrick Steinhardt, 2026-10-05 06:57

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <asNKYAHSWJkFkhNn@pks.im>
In-Reply-To: <54fac5f4-e49b-4384-af2c-615d7cc04ff5@gmx.com>

```
On Sat, Oct 03, 2026 at 12:41:47PM -0700, Carlisle T. Hamlin wrote:
> On 10/3/26 1:54 AM, Sphinx wrote:
> > Is the following understanding correct?
> > 
> > An empty tree is a valid Git state, so Git does not generally consider
> > transitioning from a non-empty tree to an empty tree inherently
> > erroneous.
> > 
> > Git does not have a general safeguard that warns when an operation
> > will delete all tracked files.
> > 
> > If there are existing safeguards, warnings, configuration options, or
> > historical discussions that I may have missed, I would appreciate any
> > pointers.
> > 
> > The reason I am asking is that I am trying to establish precisely
> > where Git's safety boundary is in this scenario specifically, whether
> > Git itself is expected to warn about the resulting empty tree, or
> > whether detecting an unexpectedly destructive tree change is
> > considered the responsibility of the tooling performing the operation.
> You know, it seems to me that it should be reasonable for Git to assume that
> someone with the wherewithal to set up and operate a git repository (or at
> the very least operate one that someone else set up) knows enough to
> understand what's going to happen if they obliterate the only commit in
> their tree.
> 
> There really is only *so* much holding of the hand I think we should be
> expected to perform before it's not only insulting to the project
> developers, but also to the *user*.
> 
> My two cents. I know folk use Git for all sorts of stuff. Just, maybe... the
> sort of person who would be surprised catastrophically by this behaviour...
> well... shouldn't.

There really is no need to be this adversarial to a simple question like
from the author. We want to be a welcoming community, and replies like
this are the exact opposite and will drive people away.

Thanks!

Patrick

```

## Patrick Steinhardt, 2026-10-05 06:57

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <asNKZpxiuFhVkVQd@pks.im>
In-Reply-To: <CALfz8Qx63qNoSbXq7C7u+KwX4=HCL7=uOUahpXd6j7KvW_c_Eg@mail.gmail.com>

```
On Sat, Oct 03, 2026 at 02:24:42PM +0530, Sphinx wrote:
> Hi Git maintainers,
> 
> I have been investigating Git's behavior in a particular destructive
> scenario and wanted to verify my understanding with the maintainers.
> 
> Consider the following repository history:
> 
> A -> B
> 
> where A contains the repository's files and B is the current HEAD.
> 
> The repository is then cloned with:
> 
> git clone --depth=1 <repository>
> 
> so only B is available locally and its parent A is not present in the
> shallow clone.
> 
> If an operation is then performed to restore/revert B, I was looking
> into the behavior when the resulting working tree/index becomes empty
> — effectively causing all tracked files to be removed.

Yeah, this can indeed be surprising behaviour. The reason for it is that
in a shallow clone, we rewrite the boundary commit (so in your case B)
so that it doesn't have any parents anymore. It thus looks like just
another root commit that has added all files in a single go. And the
consequence of that is that reverting it will then delete everything.

Now arguably, Git could be improved here. We just recently had a similar
discussion around maybe forbidding to "git commit --amend" such a
shallow commit. Your scenario is a second one where Git should probably
at least warn about what's happening.

Arguably we should even completely refuse editing such a shallow commit
by default. I would guess that in 99% of all the cases where a user does
it it's unintended. And for the 1% where it's actually intended we could
give users a way to override this safeguard.

> I have gone through the Git documentation and experimented with the
> relevant Git commands, including the behavior of shallow repositories,
> branch deletion, working-tree changes, resets, restores, and other
> destructive operations. Based on my investigation, I have not been
> able to find evidence that Git provides a warning or confirmation
> specifically when an operation results in all tracked files being
> removed or produces an empty tree.
> 
> Before drawing any conclusions, I wanted to verify this with the Git developers.
> 
> Is the following understanding correct?
> 
> An empty tree is a valid Git state, so Git does not generally consider
> transitioning from a non-empty tree to an empty tree inherently
> erroneous.

Mostly correct. A small correction though: Git considers the empty
_root_ tree to be a valid state so that you can create a commit that
contains no files at all. We never write an empty sub-tree though, so
you cannot add an empty directory.

> Git does not have a general safeguard that warns when an operation
> will delete all tracked files.

We try hard to not lose a user's data, so especially untracked data is
something we're careful about. But anything that's committed already is
fair game, as it's trivial to restore.

> If there are existing safeguards, warnings, configuration options, or
> historical discussions that I may have missed, I would appreciate any
> pointers.

There are none for the above use case, to the best of my knowledge.

> The reason I am asking is that I am trying to establish precisely
> where Git's safety boundary is in this scenario specifically, whether
> Git itself is expected to warn about the resulting empty tree, or
> whether detecting an unexpectedly destructive tree change is
> considered the responsibility of the tooling performing the operation.

I wouldn't warn about an empty tree in general. But editing a commit
that is a shallow boundary is something that I'd agree Git should warn
about, if not even refuse by default.

Patrick

```

## Matt Hunter, 2026-10-05 10:41

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <DLWUB05MOV7T.2XA7NG57870ZD@lfurio.us>
In-Reply-To: <asNKZpxiuFhVkVQd@pks.im>

```
On Mon Oct 5, 2026 at 2:57 AM EDT, Patrick Steinhardt wrote:
> On Sat, Oct 03, 2026 at 02:24:42PM +0530, Sphinx wrote:
>> 
>> If an operation is then performed to restore/revert B, I was looking
>> into the behavior when the resulting working tree/index becomes empty
>> — effectively causing all tracked files to be removed.
>
> Yeah, this can indeed be surprising behaviour. The reason for it is that
> in a shallow clone, we rewrite the boundary commit (so in your case B)
> so that it doesn't have any parents anymore. It thus looks like just
> another root commit that has added all files in a single go. And the
> consequence of that is that reverting it will then delete everything.

Separate question from the sidelines:  As a non shallow clone user, this
makes me wonder if/how these boundary commits might be munged to
preserve original commit ids in the clone?  eg: so a fast-forward
pull still works for future content

```

## Junio C Hamano, 2026-10-05 16:38

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <xmqq7bjwkspu.fsf@gitster.g>
In-Reply-To: <DLWUB05MOV7T.2XA7NG57870ZD@lfurio.us>

```
"Matt Hunter" <m@lfurio.us> writes:

> On Mon Oct 5, 2026 at 2:57 AM EDT, Patrick Steinhardt wrote:
>> On Sat, Oct 03, 2026 at 02:24:42PM +0530, Sphinx wrote:
>>> 
>>> If an operation is then performed to restore/revert B, I was looking
>>> into the behavior when the resulting working tree/index becomes empty
>>> — effectively causing all tracked files to be removed.
>>
>> Yeah, this can indeed be surprising behaviour. The reason for it is that
>> in a shallow clone, we rewrite the boundary commit (so in your case B)
>> so that it doesn't have any parents anymore. It thus looks like just
>> another root commit that has added all files in a single go. And the
>> consequence of that is that reverting it will then delete everything.
>
> Separate question from the sidelines:  As a non shallow clone user, this
> makes me wonder if/how these boundary commits might be munged to
> preserve original commit ids in the clone?  eg: so a fast-forward
> pull still works for future content

Something similar to "graft" (and now "replace") is done under the
hood, to stop history traversal machinery seeing the true parents
of these boundary commits.  As the commit object itself (specifically
its "parent " lines in the header part) is not modified in any way,
this does not affect object names.


```

## Sphinx, 2026-10-05 17:35

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <CALfz8QxzXJ_0EC=a2AEO-_Qy+MGGnjHbE_8dBky23BLN-WnGPA@mail.gmail.com>
In-Reply-To: <CALfz8QzymKxvzYGWLwdtzERDm1apa8ebZEVyLne18hpTaB=D0g@mail.gmail.com>

```
Thanks, Patrick. That makes it much clearer.

I was initially thinking of the empty tree as the potentially unsafe
part, but from your explanation, it's clear that the empty tree itself
isn't really the problem it's the shallow boundary that makes this
behavior surprising.

The absence of a safeguard specifically for editing a shallow boundary
commit, and the possibility of warning or refusing such operations by
default, is exactly what I was trying to understand.

Thanks again for taking the time to explain it.


On Mon, Oct 5, 2026 at 10:44 PM Sphinx <sphinx9692@gmail.com> wrote:
>
> Thanks, Patrick. That makes it much clearer.
>
> I was initially thinking of the empty tree as the potentially unsafe part, but from your explanation, it's clear that the empty tree itself isn't really the problem it's the shallow boundary that makes this behavior surprising.
>
> The absence of a safeguard specifically for editing a shallow boundary commit, and the possibility of warning or refusing such operations by default, is exactly what I was trying to understand.
>
> Thanks again for taking the time to explain it.
>
>
> On Mon, 5 Oct, 2026, 10:08 pm Junio C Hamano, <gitster@pobox.com> wrote:
>>
>> "Matt Hunter" <m@lfurio.us> writes:
>>
>> > On Mon Oct 5, 2026 at 2:57 AM EDT, Patrick Steinhardt wrote:
>> >> On Sat, Oct 03, 2026 at 02:24:42PM +0530, Sphinx wrote:
>> >>>
>> >>> If an operation is then performed to restore/revert B, I was looking
>> >>> into the behavior when the resulting working tree/index becomes empty
>> >>> — effectively causing all tracked files to be removed.
>> >>
>> >> Yeah, this can indeed be surprising behaviour. The reason for it is that
>> >> in a shallow clone, we rewrite the boundary commit (so in your case B)
>> >> so that it doesn't have any parents anymore. It thus looks like just
>> >> another root commit that has added all files in a single go. And the
>> >> consequence of that is that reverting it will then delete everything.
>> >
>> > Separate question from the sidelines:  As a non shallow clone user, this
>> > makes me wonder if/how these boundary commits might be munged to
>> > preserve original commit ids in the clone?  eg: so a fast-forward
>> > pull still works for future content
>>
>> Something similar to "graft" (and now "replace") is done under the
>> hood, to stop history traversal machinery seeing the true parents
>> of these boundary commits.  As the commit object itself (specifically
>> its "parent " lines in the header part) is not modified in any way,
>> this does not affect object names.


```

## Carlisle T. Hamlin, 2026-10-05 18:10

Subject: Re: Question: behavior when reverting a commit from a shallow clone
Message-ID: <593528e2-bac3-4d9d-8374-be30e97b0028@gmx.com>
In-Reply-To: <asNKYAHSWJkFkhNn@pks.im>

```
On 10/4/26 11:57 PM, Patrick Steinhardt wrote:
>> You know, it seems to me that it should be reasonable for Git to assume that
>> someone with the wherewithal to set up and operate a git repository (or at
>> the very least operate one that someone else set up) knows enough to
>> understand what's going to happen if they obliterate the only commit in
>> their tree.
>>
>> There really is only *so* much holding of the hand I think we should be
>> expected to perform before it's not only insulting to the project
>> developers, but also to the *user*.
>>
>> My two cents. I know folk use Git for all sorts of stuff. Just, maybe... the
>> sort of person who would be surprised catastrophically by this behaviour...
>> well... shouldn't.
> > There really is no need to be this adversarial to a simple question like
> from the author. We want to be a welcoming community, and replies like
> this are the exact opposite and will drive people away.

My apologies; I didn't realize I was coming across as adversarial.

Also, I stand corrected - other replies to this question indicate that we *are, indeed* prepared to perform this level of hand holding. I fundamentally misunderstood the project's stance in this regard.



```
