Git
Patch
Diff
Version Control
Repository

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.

Browse interview questions

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:

bash
1# In source repository
2git diff main..feature-branch > feature.patch
3
4# In target repository
5git apply --check feature.patch
6git apply feature.patch

Metadata-preserving workflow:

bash
1# In source repository
2git format-patch -1 <commit-sha> --stdout > change.patch
3
4# In target repository
5git am --3way < change.patch

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:

bash
1git status
2git rev-parse --abbrev-ref HEAD
3git apply --stat change.patch
4git apply --check change.patch

--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.

bash
# Strip one leading path component from patch entries
git apply -p1 change.patch

For complex remapping, edit the patch headers before applying, or apply manually with git apply --reject and resolve .rej files.

bash
git apply --reject --whitespace=fix change.patch

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.

bash
1git add -A
2git diff --cached
3# run project tests here
4git commit -m "Apply upstream feature patch"

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.

bash
git remote add source /path/to/source/repo
git fetch source
git cherry-pick <commit-sha>

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.

bash
1# Export last 3 commits as numbered patch files
2git format-patch -3 -o /tmp/patches
3
4# In target repository
5git am --3way /tmp/patches/*.patch

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-patch and git am when commit metadata should be preserved.
  • Use git diff and git apply for quick content-only transfer.
  • Always run git apply --check before 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions