threads / bug / 56226

[BUG] Amending a shallow clone -> orphan branch

Subject: [BUG] Amending a shallow clone -> orphan branch

## tl;dr

5 messages between Aug 6, 2021 and Aug 6, 2021.

replies: 4people: 2as markdown or json

Alexandr Miloslavskiy· Aug 6, 2021, 00:04 UTC · lore
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· Aug 6, 2021, 12:21 UTC · re: Alexandr Miloslavskiy · lore

Re: [BUG] Amending a shallow clone -> orphan branch

Hi Alexandr, welcome to Git mailing list!
On 06/08/21 07.04, Alexandr Miloslavskiy wrote:
Show 14 quoted lines
> 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):
Show 6 quoted lines
> 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· Aug 6, 2021, 12:24 UTC · re: Bagas Sanjaya · lore

Re: [BUG] Amending a shallow clone -> orphan branch

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· Aug 6, 2021, 13:16 UTC · re: Bagas Sanjaya · lore

Re: [BUG] Amending a shallow clone -> orphan branch

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· Aug 6, 2021, 12:27 UTC · re: Bagas Sanjaya · lore

Re: [BUG] Amending a shallow clone -> orphan branch

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.

← back to recent threads