Stop git from relating related history on fetch

I’m working on creating a template system with git, but git is being too clever for my use case. After creating a shallow clone of the template, the new repository is made with only the latest commit, great. Then I remove the template remote. However, now after some time, I want to compare the template with the new repository. Using fetch to retrieve the history of the template, so they can be compared, the history of the new repository is being ‘unshallowed’, which is undesirable.

I’ve boiled it down to simple scenario for reproducibility:

$ pwd
/home/test/
$ ls
template/
$ git clone file://home/test/template --depth 1 repo
[...]
$ ls
repo/
template/
$ git -C repo log main
commit ac787e88da22e91b38cea934299f01f40b7da764 (grafted, HEAD -> main, origin/main, origin/HEAD)
Author: Qubitz <15070724+qubitz@users.noreply.github.com>
Date:   Sun Jun 1 16:27:29 2025 -0700

    transient technology
$ git remote remove origin
$ git fetch ../template --shallow-since="2025-06-01 16:00:00 -0700"
[...]
$ git -C repo log main
commit ac787e88da22e91b38cea934299f01f40b7da764 (HEAD -> main)
Author: Qubitz <15070724+qubitz@users.noreply.github.com>
Date:   Sun Jun 1 16:27:29 2025 -0700

    transient technology

===== Result of git unshallowing history ===
|  commit 744f4fee64b174c5e52d15d59ad996df5c84a85c
|  Author: Qubitz <15070724+qubitz@users.noreply.github.com>
|  Date:   Sun Jun 1 16:27:29 2025 -0700
|
|      initial commit
======

There seems to be some lingering link between the two histories that needs to severed. How does this link work and how can I stop git from unswallowing my history?

Git is “unshallowing” your clone because the commit(s) in your new repo still share history with the original template — and git fetch sees that and tries to complete the history graph.

This happens because even though you removed the remote, Git still sees the existing commit’s hash (ac787e...) as part of the same commit graph when fetching from the template — Git optimizes by reconnecting the histories.


How to stop Git from unshallowing the history:

To fully sever the link between the new repo and the template history, you need to rewrite the commit(s) in the new repo so Git doesn’t see them as shared.

Use git commit --amend or rebase to give them new commit IDs:

cd repo
git commit --amend --no-edit

Or, for multiple commits:

git rebase --root

This rewrites the commits with new hashes, breaking the shared history link.

Then, when you do:

bash

CopyEdit

git fetch ../template --shallow-since=...

Git will treat the histories as unrelated, and won’t unshallow your new repo.


Summary:

  • Git unshallows because your repo still shares commit hashes with the template.
  • To prevent that, rewrite the commits in the new repo (e.g. --amend or rebase --root).
  • After rewriting, Git will treat them as separate histories and won’t merge them.