Create patch or diff file from git repository and apply it to another different git repository
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Moving changes from one Git repository to another is common when projects diverge or when upstream and downstream codebases evolve independently. The safest approach is to create a patch from well-scoped commits, then apply it with tooling that preserves history and conflict visibility. Choosing between git diff, git format-patch, and git am depends on how much metadata you need.
Choose the Right Patch Format
Two common formats:
- raw diff from
git diff, good for quick file-level transfer - email-style patch from
git format-patch, includes commit metadata
Raw diff workflow:
Metadata-preserving workflow:
If authorship and commit message matter, prefer format-patch plus git am.
Prepare Repositories for Clean Application
Before applying patches, align directory structure as much as possible. Large path differences increase conflict risk.
Helpful checks:
--check validates applicability without writing files. Use it every time.
Handle Different Directory Layouts
When file paths differ between repos, use strip levels or path remapping.
For complex remapping, edit the patch headers before applying, or apply manually with git apply --reject and resolve .rej files.
This keeps partially applicable hunks while showing exactly what failed.
Validate and Commit in Target Repository
After patch application, run tests and inspect staged changes before committing.
If you used git am, commit metadata may already be created. Review with git log -1 --stat.
When to Use Cherry-Pick Instead
If both repositories share commit ancestry, git cherry-pick is often easier and safer than patch files.
Patch files are best when repositories have no direct Git relationship.
Working with Multi-Commit Patch Series
When a feature spans multiple commits, export a patch series instead of one large diff. This preserves logical history and makes review easier in the target repository.
If one patch fails, resolve conflicts, run git add on resolved files, and continue with git am --continue. This keeps each commit boundary intact.
For auditability, record source commit hashes in the target repository commit message or release notes so teams can trace provenance later.
Common Pitfalls
A common pitfall is generating patches from uncommitted working tree changes without documenting context. Prefer clean commits so patches are reproducible.
Another issue is applying patches on top of unrelated local modifications. Start from a clean branch to isolate conflicts.
Whitespace-only differences can also block application depending on project settings. Use whitespace flags carefully and review resulting changes.
Finally, do not skip test runs after patch application. A patch that applies cleanly may still break behavior due to dependency or API differences.
Summary
- Use
git format-patchandgit amwhen commit metadata should be preserved. - Use
git diffandgit applyfor quick content-only transfer. - Always run
git apply --checkbefore writing changes. - Handle path mismatches with strip levels or reject-based conflict resolution.
- Validate and test in the target repository before final commit.
Related reading
- Create spring repository without entity
- Creating a new empty branch for a new project
- Creating folders inside a GitHub repository without using Git
- Cygwin/Git error cygheap base mismatch detected
- DDD and Asynchronous Repositories
- Deep merge dictionaries of dictionaries in Python
- Default behavior of "git push" without a branch specified
- Default behavior of git push without a branch specified
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.