Git Patch: Transferring commits or changes easily
git patch is a super easy way to move either commits or local changes between repositories or branches. It can be used in two primary ways:
- Transferring commits
- Transferring changes as uncommitted modifications
Transferring commits
Creating patch of a specific commit
git format-patch -1 <commit-hash>
Applying a patch file
Place the patch file in the root directory of the target repo, then apply it
git am yourFileName.patch
Whitespace warnings are not important.
Creating patch for last n commit
git format-patch -n --stdout > yourFileName.patch
stdout option writes all commits into a single patch file.
If stdout is not used, each commit will be saved in a separate patch file.
git format-patch -n
We can apply all patch files in the root folder
git am *.patch
This will take all the commits into the current branch.
Creating patch for a range of commits
git format-patch excludedPreviousCommitHash..includedLaterCommitHash --stdout > yourFileName.patch
The left commit is not included in the patch. The right (included) commit is the latest in the range.
Creating patch for commits between branches
git format-patch develop..feature/test --stdout > yourFileName.patch
Transferring changes
We can move local changes instead of commits.
Creating patch for current local changes (staged ones)
git diff --cached > yourFileName.patch
Creating patch for current all local changes
git diff > yourFileName.patch
To apply changes
git apply yourFileName.patch
When using git diff, no dots (..) are used between branches or commits. and we don’t need stdout.
git diff excludedPreviousCommitHash includedLaterCommitHash > yourFileName.patch