This document outlines strategies for managing and migrating custom modifications made to a third-party Git repository (like PyTorch) that has been included as a subdirectory within a larger project.

The Problem: Unmanaged Local Edits

It’s a common scenario: your main research project is hosted on GitHub. To use a specific library, like PyTorch, you clone it directly into a subdirectory of your project.

my-research-project/
├── .git/
├── analysis/
├── data/
└── pytorch/        <-- A direct clone of the official PyTorch repo
    ├── .git/
    ├── torch/
    └── ...

Over time, you make custom edits to the files inside the pytorch/ directory to suit your project’s specific needs. The problem arises when a new version of PyTorch is released. You want to update to the new version to get the latest features and bug fixes, but you also need to preserve your custom edits.

The core challenges are:

  • Tracking: How do you know exactly what you changed?
  • Updating: How can you merge the official updates without manually re-applying every single one of your edits?
  • Collaboration: How can your teammates get an exact copy of your modified library code?

Simply copying over the new version would overwrite and erase all your work. This guide provides a systematic way to solve this problem.

Step 1: Rescue Your Existing Edits with a Patch File

Before you can adopt a better workflow, you must first save the unmanaged edits you’ve already made. The best way to do this is by creating a patch file—a single text file that contains a complete record of every line you’ve added, deleted, or changed.

  1. Navigate into your modified library folder:

    cd /path/to/your/project/pytorch
    
  2. Generate the patch file: The git diff command compares your current, modified files against the original version you cloned. We’ll save this difference to a file.

    git diff > ~/my_pytorch_edits.patch
    

You now have a file named my_pytorch_edits.patch in your home directory. This file is a complete, portable backup of your modifications.

This is the most robust, safe, and collaborative method. It involves creating a personal copy (a “fork”) of the library’s repository on GitHub. This fork becomes the central, managed source for your team’s custom version.

Why Fork?

  • Safety & Isolation: Your changes are stored in your own repository, with no risk of affecting the official project.
  • Collaboration Hub: Your teammates can clone your fork to get the exact version of the code you are using. It becomes the single source of truth.
  • Backup: Your custom commits are safely backed up on GitHub.

Workflow Steps

  1. Fork and Clone:

    • Go to the official library’s GitHub page (e.g., github.com/pytorch/pytorch) and click the “Fork” button.
    • Clone your fork to a new, clean location (outside your main project folder).
      git clone https://github.com/YOUR_USERNAME/pytorch.git pytorch-fork
      cd pytorch-fork
      
  2. Set Up Remotes and Branch:

    • Add the original repository as an upstream remote to pull official updates from.
      git remote add upstream https://github.com/pytorch/pytorch.git
      
    • Create a dedicated branch for your edits. This keeps your work separate from the official code.
      git checkout -b my-custom-version
      
  3. Apply Your Edits:

    • Apply the patch file you created earlier.
      git apply ~/my_pytorch_edits.patch
      
    • Commit the changes to your branch.
      git add .
      git commit -m "Port custom modifications for project"
      git push -u origin my-custom-version
      
  4. Integrate with Your Main Project:

    • First, delete the old, unmanaged pytorch/ folder from your project.
    • Use Git Submodules to link your fork directly into your main project. From your project’s root directory, run:
      git submodule add -b my-custom-version https://github.com/YOUR_USERNAME/pytorch.git path/to/pytorch
      

    Your project now correctly tracks your custom, version-controlled library.

Migrating to a New Version with the Forking Workflow

When a new official version is released:

  1. Navigate into your local pytorch-fork directory.

  2. Fetch the latest official changes.

    git checkout main
    git fetch upstream
    git pull upstream main
    
  3. Rebase your custom branch on top of the updated code.

    git checkout my-custom-version
    git rebase main
    
  4. Resolve Conflicts: Git will pause if it finds a conflict between an official change and one of your edits. You must manually edit the conflicting files to merge the changes, then continue the rebase with git add . and git rebase --continue. This process forces you to review your edits against the new version.

Approach 2: The Local-Only Workflow

This method avoids creating a fork on GitHub but is riskier and not recommended for teams. Your custom branch will only exist on your local machine.

The Risks

  • No Backup: If your local machine fails, your custom branch and all its history are lost.
  • Difficult Collaboration: Sharing your exact code with teammates is cumbersome and error-prone.

Workflow Steps

  1. Clone the Official Repository:

    git clone https://github.com/pytorch/pytorch.git my-local-pytorch
    cd my-local-pytorch
    
  2. Create a Local Branch: Never make edits on the main branch.

    git checkout -b my-local-edits
    
  3. Apply and Commit: Apply your my_pytorch_edits.patch file and commit the changes to this local branch.

Migrating to a New Version with the Local Workflow

  1. Fetch the latest official changes.

    git checkout main
    git pull origin main
    
  2. Rebase your local branch.

    git checkout my-local-edits
    git rebase main
    
  3. Resolve any conflicts that arise.

Conclusion

FeatureGit Forking WorkflowLocal-Only Workflow
Safety✅ Excellent (Backed up on GitHub)❌ Poor (Exists only on one machine)
Collaboration✅ Excellent (Shared via a central repo)❌ Difficult (Requires manual sharing)
Clarity✅ High (Clear separation of concerns)⚠️ Medium (Can get confusing)
SetupMore initial stepsFewer initial steps

For any project involving more than one person or for any work you cannot afford to lose, the Git Forking Workflow is the superior choice. It establishes a clean, maintainable, and collaborative process for managing custom dependencies.