|
| 1 | +# Sharing Changes as Patch Files |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +> Generate and share patch files for committed or uncommitted changes. |
| 6 | +
|
| 7 | +How to create patch files from your changes for sharing via email, SCP, Slack, or other means. Covers both committed (with full commit metadata) and uncommitted changes. |
| 8 | + |
| 9 | + |
| 10 | +#### Tags |
| 11 | +`patch`, `diff`, `sharing`, `email`, `collaboration` |
| 12 | + |
| 13 | +#### Author |
| 14 | +mike-rambil |
| 15 | + |
| 16 | +#### Last Updated |
| 17 | +2024-06-10 |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +### Subcommands |
| 22 | +#### Create Patch from Last Commit(s) |
| 23 | + |
| 24 | +#### Command |
| 25 | +```sh |
| 26 | +git format-patch HEAD~1 |
| 27 | +``` |
| 28 | + |
| 29 | +#### Examples |
| 30 | +- **Create a .patch file for the last commit.** |
| 31 | + |
| 32 | + |
| 33 | +```sh |
| 34 | +git format-patch HEAD~1 |
| 35 | +``` |
| 36 | +- **Create a single patch file for all commits on top of main.** |
| 37 | + |
| 38 | + |
| 39 | +```sh |
| 40 | +git format-patch origin/main..HEAD --stdout > my-changes.patch |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +#### Steps |
| 45 | +1. Run '`git format-patch HEAD~1' to create a patch for the last commit`. |
| 46 | +2. Use '`git format-patch origin/main..HEAD --stdout > my-changes.patch' to create a single patch file for multiple commits`. |
| 47 | + |
| 48 | + |
| 49 | +#### Warnings |
| 50 | +- ⚠️ Patch files created this way include commit messages, authorship, and timestamps. |
| 51 | +- ⚠️ Use 'git am' to apply these patches on another system. |
| 52 | + |
| 53 | + |
| 54 | +#### Links |
| 55 | +- [Official Docs](https://git-scm.com/docs/git-format-patch) |
| 56 | + |
| 57 | + |
| 58 | +#### Tags |
| 59 | +`patch`, `format-patch`, `committed` |
| 60 | + |
| 61 | +#### Author |
| 62 | +mike-rambil |
| 63 | + |
| 64 | +#### Last Updated |
| 65 | +2024-06-10 |
| 66 | + |
| 67 | +#### Apply Patch with Commit Metadata |
| 68 | + |
| 69 | +#### Command |
| 70 | +```sh |
| 71 | +git am my-changes.patch |
| 72 | +``` |
| 73 | + |
| 74 | +#### Examples |
| 75 | +- **Apply a patch file and preserve commit info.** |
| 76 | + |
| 77 | + |
| 78 | +```sh |
| 79 | +git am my-changes.patch |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +#### Steps |
| 84 | +1. Run '`git am my-changes.patch' to apply the patch and preserve commit messages, authorship, and timestamps`. |
| 85 | + |
| 86 | + |
| 87 | +#### Tags |
| 88 | +`patch`, `am`, `apply` |
| 89 | + |
| 90 | +#### Author |
| 91 | +mike-rambil |
| 92 | + |
| 93 | +#### Last Updated |
| 94 | +2024-06-10 |
| 95 | + |
| 96 | +#### Create Patch from Uncommitted Changes |
| 97 | + |
| 98 | +#### Command |
| 99 | +```sh |
| 100 | +git diff > changes.diff |
| 101 | +``` |
| 102 | + |
| 103 | +#### Examples |
| 104 | +- **Create a diff file of uncommitted changes.** |
| 105 | + |
| 106 | + |
| 107 | +```sh |
| 108 | +git diff > changes.diff |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +#### Steps |
| 113 | +1. Run '`git diff > changes.diff' to save uncommitted changes to a file`. |
| 114 | + |
| 115 | + |
| 116 | +#### Warnings |
| 117 | +- ⚠️ This does NOT preserve commit metadata or history—just raw changes. |
| 118 | + |
| 119 | + |
| 120 | +#### Links |
| 121 | +- [Official Docs](https://git-scm.com/docs/git-diff) |
| 122 | + |
| 123 | + |
| 124 | +#### Tags |
| 125 | +`diff`, `uncommitted`, `snapshot` |
| 126 | + |
| 127 | +#### Author |
| 128 | +mike-rambil |
| 129 | + |
| 130 | +#### Last Updated |
| 131 | +2024-06-10 |
| 132 | + |
| 133 | +#### Apply Diff File |
| 134 | + |
| 135 | +#### Command |
| 136 | +```sh |
| 137 | +git apply changes.diff |
| 138 | +``` |
| 139 | + |
| 140 | +#### Examples |
| 141 | +- **Apply a diff file of uncommitted changes.** |
| 142 | + |
| 143 | + |
| 144 | +```sh |
| 145 | +git apply changes.diff |
| 146 | +``` |
| 147 | + |
| 148 | + |
| 149 | +#### Steps |
| 150 | +1. Run '`git apply changes.diff' to apply the changes from a diff file`. |
| 151 | + |
| 152 | + |
| 153 | +#### Tags |
| 154 | +`diff`, `apply`, `uncommitted` |
| 155 | + |
| 156 | +#### Author |
| 157 | +mike-rambil |
| 158 | + |
| 159 | +#### Last Updated |
| 160 | +2024-06-10 |
| 161 | + |
| 162 | +#### Patch vs Diff: Quick Reference |
| 163 | + |
| 164 | +#### Tags |
| 165 | +`patch`, `diff`, `reference` |
| 166 | + |
| 167 | +#### Author |
| 168 | +mike-rambil |
| 169 | + |
| 170 | +#### Last Updated |
| 171 | +2024-06-10 |
| 172 | + |
0 commit comments