Skip to content

Instantly share code, notes, and snippets.

@rambalachandran
Created April 14, 2026 01:14
Show Gist options
  • Select an option

  • Save rambalachandran/456333dc735bc8ad21034f196cb20daa to your computer and use it in GitHub Desktop.

Select an option

Save rambalachandran/456333dc735bc8ad21034f196cb20daa to your computer and use it in GitHub Desktop.
Git Copy SubFolder Changes Across Branches

Task: copy only the changes for specific subfolders from a source branch into a destination branch.

Example branches:

  • source branch: feature-source-branch
  • destination branch: feature-destination-branch

There are two valid approaches depending on whether you want to preserve commit history or only bring over the final state of the folder.

1) Preserve commit history (more precise, more work)

Use this when you want provenance preserved, including:

  • original commit authors
  • commit messages
  • commit-by-commit review trail

This is the better option when the history itself matters, such as for auditing, debugging, or understanding how the folder evolved over time.

The tradeoff is that it can be harder to isolate only the folder changes you want. A commit may touch the target subfolder and unrelated files, so cherry-picking may require conflict resolution or manual cleanup.

Find commits that touched the target folder

Add --reverse if you want to apply them oldest to newest.

git log --oneline origin/feature-destination-branch..origin/feature-source-branch -- "path/to/subfolder"

Example with oldest first:

git log --oneline --reverse origin/feature-destination-branch..origin/feature-source-branch -- "path/to/subfolder"

Apply the relevant commits

Start from the destination branch and make sure refs are current:

git checkout feature-destination-branch
git fetch origin
git cherry-pick <commit-id>

Repeat for each commit you want to bring over.

Notes

  • Review each commit before cherry-picking if the commit may include unrelated changes.
  • If a commit partially affects the target folder and partially affects other areas, you may need to resolve conflicts or selectively stage changes before continuing.
  • This approach is best when the commits are already well-scoped.

2) Copy the folder snapshot (simpler, no history)

Use this when you only care about the final contents of the folder as it exists at the tip of the source branch.

This is usually the fastest and safest option when:

  • you only want the resulting folder state
  • commit history is noisy or mixed with unrelated changes
  • you do not need to preserve the original author/message trail

The tradeoff is that you lose commit provenance. The destination branch will show the change as part of whatever new commit you make there.

Restore the folder from the source branch into the destination branch

git fetch origin
git checkout feature-destination-branch
git restore --source=origin/feature-source-branch --staged --worktree -- "path/to/subfolder"

What this does

  • updates the working tree copy of the folder
  • stages the restored changes
  • makes the folder in your current branch match the source branch version

After that, inspect the diff and commit it normally:

git diff --staged
git commit -m "Copy subfolder changes from feature-source-branch"

Notes

  • This is ideal when you want “just the folder contents” and not the intermediate history.
  • You can repeat the command for multiple subfolders if needed.
  • It is a good approach when the source branch contains lots of unrelated commits.

Rule of thumb

Choose preserve commit history if:

  • you care about traceability
  • the commits are clean and folder-scoped
  • you want the original evolution of the changes

Choose copy folder snapshot if:

  • you only need the final state
  • the source history is messy
  • you want the least complicated path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment