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.
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.
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"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.
- 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.
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.
git fetch origin
git checkout feature-destination-branch
git restore --source=origin/feature-source-branch --staged --worktree -- "path/to/subfolder"- 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"- 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.
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