<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Pankaj Kumar - Read the Docs</title>
    <subtitle>Code explorations, engineering thoughts, and life in between.</subtitle>
    <link rel="self" type="application/atom+xml" href="https://pankaj.xyz/rtd/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://pankaj.xyz/rtd/"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-07-26T00:00:00+00:00</updated>
    <id>https://pankaj.xyz/rtd/atom.xml</id>
    <entry xml:lang="en">
        <title>Syncing a Repo with Its Upstream Template, Without Losing Content</title>
        <published>2026-07-26T00:00:00+00:00</published>
        <updated>2026-07-26T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://pankaj.xyz/rtd/syncing-a-repo-with-its-upstream-template/"/>
        <id>https://pankaj.xyz/rtd/syncing-a-repo-with-its-upstream-template/</id>
        
        <content type="html" xml:base="https://pankaj.xyz/rtd/syncing-a-repo-with-its-upstream-template/">&lt;h2 id=&quot;background&quot;&gt;Background&lt;a class=&quot;zola-anchor&quot; href=&quot;#background&quot; aria-label=&quot;Anchor link for: background&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you build a project on top of an open-source template or boilerplate repo, you eventually hit a fork-management problem: how do you keep pulling in upstream improvements without your own content, config, or custom code getting clobbered every time you merge?&lt;/p&gt;
&lt;p&gt;Here’s the setup I landed on. It uses a second Git remote pointed at the upstream repo, plus a custom merge driver that tells Git to always keep “our” version of specific files and folders. Once it’s wired up, pulling updates becomes a two-command routine.&lt;/p&gt;
&lt;aside class=&quot;admonition note&quot; role=&quot;note&quot;&gt;
&lt;p class=&quot;admonition-header&quot;&gt;
&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot;
  stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; class=&quot;svg-icon info &quot; aria-hidden=&quot;true&quot;&gt;&lt;circle cx=&quot;12&quot; cy=&quot;12&quot; r=&quot;10&quot; /&gt;
  &lt;path d=&quot;M12 16v-4&quot; /&gt;
  &lt;path d=&quot;M12 8h.01&quot; /&gt;&lt;/svg&gt;
 Note&lt;/p&gt;
&lt;p&gt;I’m using a website/theme as the running example below since that’s the situation I was in, but nothing here is specific to websites. This works for any repo that needs to keep tracking an external template, boilerplate, or starter-kit repo - a backend service scaffolded from a starter template, a config repo based on a shared base, and so on. Swap in whatever names make sense for your project; &lt;code&gt;template&lt;/code&gt; is just the remote name I chose, not a requirement.&lt;/p&gt;
&lt;/aside&gt;
&lt;h2 id=&quot;why-not-just-fork-the-upstream-repo&quot;&gt;Why Not Just Fork the Upstream Repo?&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-not-just-fork-the-upstream-repo&quot; aria-label=&quot;Anchor link for: why-not-just-fork-the-upstream-repo&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The usual advice for this kind of setup is: fork the upstream repo, then build your project inside the fork. That way, your repo &lt;em&gt;is&lt;/em&gt; a fork, and pulling upstream changes is just a routine “sync fork” operation.&lt;/p&gt;
&lt;p&gt;That advice assumes you’re starting fresh. I wasn’t. My repo was already live, with its own commit history going back years, long before I decided to move it onto an open-source template published on GitHub. There was no clean way to retroactively turn an existing, live repo into a fork of a repo that didn’t exist yet when mine was created - and I wasn’t willing to throw away my history just to get a tidier Git relationship.&lt;/p&gt;
&lt;p&gt;So instead of forking, I linked the histories after the fact: added the upstream repo as a second remote on my existing repo, did the initial merge, and set up &lt;code&gt;.gitattributes&lt;/code&gt; with a custom merge driver so my content wouldn’t get overwritten by the upstream repo’s own files going forward. If you’re in the same boat - an existing repo that you want to start tracking an external template from, without recreating it as a fork - this is the pattern that worked for me.&lt;/p&gt;
&lt;h2 id=&quot;initial-setup-one-time&quot;&gt;Initial Setup (One-Time)&lt;a class=&quot;zola-anchor&quot; href=&quot;#initial-setup-one-time&quot; aria-label=&quot;Anchor link for: initial-setup-one-time&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This part only needs to happen once, on the repo itself, and it’s what makes everything else possible.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add a &lt;code&gt;.gitattributes&lt;/code&gt; file&lt;/strong&gt; at the root of your repo that marks the paths you want protected with the &lt;code&gt;merge=ours&lt;/code&gt; strategy - for example:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;content/** merge=ours&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;static/images/** merge=ours&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config.toml merge=ours&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Adjust the paths to match your own project’s layout.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Register the custom merge driver locally&lt;/strong&gt; so Git knows how to apply the rule:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; config&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; merge.ours.driver&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0550AE, #79C0FF);&quot;&gt; true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Commit and push &lt;code&gt;.gitattributes&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; .gitattributes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; commit&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0550AE, #79C0FF);&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0550AE, #79C0FF);&quot;&gt;m&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt;Add merge=ours rules to protect personal content during template sync&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; push&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Once &lt;code&gt;.gitattributes&lt;/code&gt; is committed and pushed, it’s a permanent part of your repo’s history - it travels with every future clone. You won’t need to do this step again, even on a fresh machine. What &lt;em&gt;doesn’t&lt;/em&gt; travel automatically is the remote pointing to the upstream repo, and the local merge driver registration - that’s the part covered next.&lt;/p&gt;
&lt;h2 id=&quot;setting-this-up-on-a-new-machine-or-fresh-clone&quot;&gt;Setting This Up on a New Machine (or Fresh Clone)&lt;a class=&quot;zola-anchor&quot; href=&quot;#setting-this-up-on-a-new-machine-or-fresh-clone&quot; aria-label=&quot;Anchor link for: setting-this-up-on-a-new-machine-or-fresh-clone&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Git remotes and local Git config don’t travel with a repo - only tracked files like &lt;code&gt;.gitattributes&lt;/code&gt; do. So on a new clone, you need to re-run two commands:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Add the upstream repo as a remote:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; remote&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; add&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; template&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; https://github.com/your-username/your-upstream-repo.git&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Name it whatever makes sense to you - &lt;code&gt;template&lt;/code&gt;, &lt;code&gt;upstream&lt;/code&gt;, &lt;code&gt;theme&lt;/code&gt;, etc. Just stay consistent with whatever name you use in the commands below.)&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Register the custom merge driver:&lt;/strong&gt;
This tells Git how to honor the &lt;code&gt;merge=ours&lt;/code&gt; rule defined in &lt;code&gt;.gitattributes&lt;/code&gt;, so your own content is protected whenever you merge in upstream changes.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; config&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; merge.ours.driver&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0550AE, #79C0FF);&quot;&gt; true&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&quot;ongoing-sync-pulling-updates-from-upstream&quot;&gt;Ongoing Sync: Pulling Updates from Upstream&lt;a class=&quot;zola-anchor&quot; href=&quot;#ongoing-sync-pulling-updates-from-upstream&quot; aria-label=&quot;Anchor link for: ongoing-sync-pulling-updates-from-upstream&quot;&gt;#&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Once the remote and merge driver are set up, syncing is just two steps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Fetch the latest upstream changes:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; fetch&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; template&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Merge the updates:&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color-scheme: light dark; color: light-dark(#1F2328, #E6EDF3); background-color: light-dark(#FFFFFF, #0D1117);&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: light-dark(#953800, #FFA657);&quot;&gt;git&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; merge&lt;/span&gt;&lt;span style=&quot;color: light-dark(#0A3069, #A5D6FF);&quot;&gt; template/master&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;(Use whatever the upstream repo’s default branch is called - &lt;code&gt;main&lt;/code&gt;, &lt;code&gt;master&lt;/code&gt;, etc.)&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The &lt;code&gt;.gitattributes&lt;/code&gt; rules do the heavy lifting here - they automatically prevent your own folders and files (in my case, things like &lt;code&gt;content/&lt;/code&gt;, &lt;code&gt;static/images/&lt;/code&gt;, and &lt;code&gt;config.toml&lt;/code&gt;) from being overwritten during the merge. If you want to exclude (or stop excluding) other files or folders from future syncs, just edit &lt;code&gt;.gitattributes&lt;/code&gt; to match your own project’s layout.&lt;/p&gt;
&lt;p&gt;That’s it - no manual cherry-picking, no diffing upstream changes by hand. The merge driver keeps your content safe by default, and you opt files in or out as your setup evolves.&lt;/p&gt;
&lt;p&gt;Flush!&lt;/p&gt;
</content>
        
    </entry>
</feed>
