# [BUG] Amending a shallow clone -> orphan branch

5 messages from 2021-08-06 to 2021-08-06. Participants: Alexandr Miloslavskiy, Bagas Sanjaya.
Thread: https://gitlist.dev/t/56226

## Alexandr Miloslavskiy, 2021-08-06 00:04

Subject: [BUG] Amending a shallow clone -> orphan branch
Message-ID: <64760969-1ead-2bfb-6f98-1161d385c0ca@syntevo.com>
URL: https://gitlist.dev/e/64760969-1ead-2bfb-6f98-1161d385c0ca%40syntevo.com

```
Consider the following steps :

git clone --depth 1 --single-branch --branch master 
https://github.com/git/git
cd git
 >1.txt
git add 1.txt
git commit --amend

This results in an orphaned branch, where a single commit contains 
entire tree.

I understand that this is a bug, because certainly git knew shallow 
commit's parents and could reuse that when amending?

```

## Bagas Sanjaya, 2021-08-06 12:21

Subject: Re: [BUG] Amending a shallow clone -> orphan branch
Message-ID: <b3083a15-f2da-0eae-b07b-8a641c613906@gmail.com>
URL: https://gitlist.dev/e/b3083a15-f2da-0eae-b07b-8a641c613906%40gmail.com
In-Reply-To: <64760969-1ead-2bfb-6f98-1161d385c0ca@syntevo.com>

```
Hi Alexandr, welcome to Git mailing list!

On 06/08/21 07.04, Alexandr Miloslavskiy wrote:
> Consider the following steps :
> 
> git clone --depth 1 --single-branch --branch master 
> https://github.com/git/git
> cd git
>  >1.txt
> git add 1.txt
> git commit --amend
> 
> This results in an orphaned branch, where a single commit contains 
> entire tree.
> 
> I understand that this is a bug, because certainly git knew shallow 
> commit's parents and could reuse that when amending?

I tested that, and below is `git status` after the test (using Git 2.32.0):

> On branch master
> Your branch and 'origin/master' have diverged,
> and have 1 and 1 different commits each, respectively.
>   (use "git pull" to merge the remote branch into yours)
> 
> nothing to commit, working tree clean

So you're amending merge commit by adding dummy file, so that your 
`master` branch becomes divergent against origin. That dummy file isn't 
contained in any commits that are included with that merge commit.

-- 
An old man doll... just what I always wanted! - Clara

```

## Alexandr Miloslavskiy, 2021-08-06 12:24

Subject: Re: [BUG] Amending a shallow clone -> orphan branch
Message-ID: <4d242c96-77e4-e59d-a7a3-c108164ba819@syntevo.com>
URL: https://gitlist.dev/e/4d242c96-77e4-e59d-a7a3-c108164ba819%40syntevo.com
In-Reply-To: <b3083a15-f2da-0eae-b07b-8a641c613906@gmail.com>

```
Bagas,

Thanks for testing.

 > I tested that, and below is `git status` after the test (using Git 
2.32.0):
 >
 >> On branch master
 >> Your branch and 'origin/master' have diverged,
 >> and have 1 and 1 different commits each, respectively.
 >>   (use "git pull" to merge the remote branch into yours)
 >>
 >> nothing to commit, working tree clean
 >
 > So you're amending merge commit by adding dummy file, so that your
 > `master` branch becomes divergent against origin. That dummy file isn't
 > contained in any commits that are included with that merge commit.

What you probably didn't notice is that the new commit has no parents, 
that is, it formed a new orphaned branch. The expected behavior is 
amending the top commit and keeping it connected to the previous branch.

```

## Alexandr Miloslavskiy, 2021-08-06 13:16

Subject: Re: [BUG] Amending a shallow clone -> orphan branch
Message-ID: <20d768d5-2fe2-5540-b7e7-2174db7c80cf@syntevo.com>
URL: https://gitlist.dev/e/20d768d5-2fe2-5540-b7e7-2174db7c80cf%40syntevo.com
In-Reply-To: <b3083a15-f2da-0eae-b07b-8a641c613906@gmail.com>

```
A bit more details:

$ git clone --depth 1 --single-branch --branch master 
https://github.com/git/git
$ cd git

Here, a shallow clone is made with just 1 commit.

$ git show -s --pretty=%P HEAD

Outputs nothing; that's already a small bug.
Surely the commit has parents!

$ git cat-file -p HEAD | grep parent

OK, that's better, finally the true parents are shown.

$ >1.txt
$ git add 1.txt
$ git commit --amend

Here, the top (and only in our clone) commit is amended.

$ git cat-file -p HEAD | grep parent

Nothing! This is the bug I'm reporting: amending the commit orphaned it.

To give a bit more background: I have a heavy-weight repo which I was 
testing on different machines. Since it's heavy, I decided to shallow 
clone just 1 commit. Upon testing on some machine, I found a small bug 
and amended a fix. Then I force-pushed. I expected the branch to be amended.

Instead, what I got was an orphaned branch, disconnected from all repo's 
branches, containing all files at once! Clearly not the best thing.

```

## Alexandr Miloslavskiy, 2021-08-06 12:27

Subject: Re: [BUG] Amending a shallow clone -> orphan branch
Message-ID: <fbec1ce8-a1ec-b504-72a7-2f96b646127b@syntevo.com>
URL: https://gitlist.dev/e/fbec1ce8-a1ec-b504-72a7-2f96b646127b%40syntevo.com
In-Reply-To: <b3083a15-f2da-0eae-b07b-8a641c613906@gmail.com>

```
On 06.08.2021 15:21, Bagas Sanjaya wrote:
> So you're amending merge commit by adding dummy file, so that your 
> `master` branch becomes divergent against origin. That dummy file isn't 
> contained in any commits that are included with that merge commit.

Note, the problem isn't about merge commits, it happens for regular 
commits as well. Maybe I should have picked a different repo for 
example. Still, the problem with losing all parents is the same 
regardless of merge/regular commit.

```
