Git Diff & Patch Files: Sharing Changes Without Remotes
In this tutorial, you'll learn about Git Diff & Patch Files: Sharing Changes Without Remotes. We cover key concepts, practical examples, and best practices.
A Git diff shows the difference between file versions, and a patch file is a diff saved to a file that can be shared and applied without network access.
In this tutorial, you'll learn how to use Git diff and patch files to share changes when you don't have direct repository access. Patch files capture changes as text files that anyone can review and apply — no network, no remotes, no merge conflicts from parallel tooling. By the end, you'll create, apply, and manage patches across branches and repositories.
flowchart LR A[Make changes] --> B[git diff > changes.patch] B --> C[Share patch file] C --> D[Recipient: git apply changes.patch] D --> E[Changes applied] B --> F[Review: view patch content] F --> G[Check changes before applying]
Viewing Changes with Git Diff
# Show unstaged changes
git diff
# Show staged changes (ready to commit)
git diff --staged
# Compare working directory to a specific commit
git diff HEAD
# Compare two commits
git diff a1b2c3d..e5f6g7h
# Compare two branches
git diff main..feature
Expected output:
diff --git a/app.py b/app.py
index a1b2c3d..e5f6g7h 100644
--- a/app.py
+++ b/app.py
@@ -1,3 +1,4 @@
def hello():
- print("Hello")
+ name = "World"
+ print(f"Hello {name}")
Creating Patch Files
Save a diff as a patch file:
# Save unstaged changes
git diff > my-changes.patch
# Save staged changes
git diff --staged > staged-changes.patch
# Save changes between commits
git diff a1b2c3d..e5f6g7h > feature-diff.patch
# Save entire commit as patch
git format-patch HEAD~1 # Creates 0001-commit-message.patch
Expected output from format-patch:
0001-Add-payment-feature.patch
Applying Patches
# Apply a patch to working directory
git apply my-changes.patch
# Apply a patch and add to staging area
git apply --index my-changes.patch
# Check if a patch applies cleanly (dry run)
git apply --check my-changes.patch
Apply patches created with format-patch (includes commit metadata):
git am 0001-Add-payment-feature.patch
Expected output:
Applying: Add payment feature
Viewing Patch Content
A patch file is human-readable:
cat my-changes.patch
It shows: which file changed, the old/new versions, and the exact lines changed with context.
Creating Patches for Multiple Commits
Generate patches for the last N commits:
git format-patch -3 # Last 3 commits
This creates one .patch file per commit.
Advanced Diff Options
Control the amount of context shown around changes:
# Show 10 lines of context instead of default 3
git diff -U10
# Ignore whitespace changes
git diff -w
# Show changes in word-level granularity
git diff --word-diff
# Show only file names (no content)
git diff --name-only
# Show only file names and status
git diff --name-status
Expected output of --name-status:
M app.py
A new_file.py
D old_file.py
Applying Patches with git am
The git am command applies patches created by format-patch and preserves the original commit metadata:
# Generate patches for the last 2 commits
git format-patch -2
# Apply all patches in the current directory
git am *.patch
# If a patch fails, resolve and continue
# Edit the conflicting file
git add resolved_file.py
git am --continue
# Or skip this patch
git am --skip
# Or abort the entire am operation
git am --abort
Common Errors
| Error | Cause | Fix |
|---|---|---|
patch does not apply |
File mismatch or line differences | Check file paths and context |
fatal: unrecognized input |
Corrupt patch format | Generate patch from Git, not manually |
error: patch failed: file.txt:1 |
Lines already match | The change is already applied |
git am: Patch is empty |
Commit already applied | Skip with git am --skip |
Cannot apply to uncommitted changes |
Working tree not clean | Commit or stash first |
diff --git line missing |
Wrong patch origin | Generate with git diff not diff |
fatal: No such file |
File path in patch doesn't match | Check your working directory |
git apply --check says good but apply fails |
Line endings differ | Use --ignore-whitespace |
Practice Questions
Challenge
Create a repository with three files. Make changes to all three but only stage two. Create a patch of unstaged changes. Create a patch of staged changes. Create format-patch patches for the last two commits. Apply the unstaged patch to a fresh clone of the repository. Verify all patches applied correctly.
Real-World Task
A contractor sent you a patch file with a bugfix, but you don't have network access (offline environment). Save the patch to a USB drive. On the offline machine, check the patch applies cleanly with git apply --check. Apply it with git apply --index. Run the tests. Create a new commit with the applied fix. This offline patch workflow is used at DodaTech for air-gapped environments where security tools like Durga Antivirus Pro are developed on isolated networks.
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro