gitforkbranchgit-submodulescherry-pick

How can I track a subset of files from a remote repository?


I'm trying to solve the following situation: I'd like to include a (not owned, public) project into mine, resizing a little bit the original file tree by removing redundant and/or not-needed files, and only leaving the bare minimum, BUT also retaining the possibility of tracking modifications to the original files.

I've tried making my own copy of said repository, adding the original as remote, but that only works up until I start deleting files from my own copy, at which point trying to fetch the remote changes fails as I'm missing files.

Is that normal? Did I mess something up in the process, and is there a more elegant way to accomplish this?


Solution

  • WARNING: the answer below only works in very limited cases where the removed files are never modified in the upstream repository

    Because of the files missing from your copy of the remote repository, a git pull will fail with a "divergent branches" error as soon as any later commits exist on the remote. However, in your case, a git rebase should do exactly what you want.

    In simple terms, a rebase will just reapply your commits onto a selected commit of the original repository (typically origin/main). You will end up with a copy of the current origin/main minus the files you chose to remove. Check the git-rebase documentation for details.

    Here's an example:

    # Clone a repository and remove some files from my local copy
    git clone https://github.com/some_repo
    cd some_repo
    git rm file_a file_b
    git commit -m "remove unneeded files"
    git rm file_c
    git commit -m "remove file_c"
    
    # At a later time, bring in new commits from the
    # remote repository and rebase my commits (removals)
    # atop the updated content
    git fetch
    git rebase origin/main