← Back to Git Course | Chapter 6: Advanced Commands | Lesson 7 of 7

Git patch Command

एक patch file एक sheet पर लिखी exact edits की एक list जैसी है जिसे एक friend अपनी copy में same changes करने के लिए follow कर सकता है।
Syntax
bash
git diff > patch_file.patch
git format-patch commit_hash
git apply patch_file.patch

Diff से Patches बनाना

git diff > changes.patch current uncommitted differences को एक plain-text patch file की तरह capture करता है, जिसे कोई भी git apply से same codebase की अपनी copy पर apply कर सकता है — repository access या एक pull request की ज़रूरत के बिना एक fix share करने का एक lightweight तरीका।

उदाहरण: Creating Patches with Diff

bash
git diff > changes.patch

Format-Patch से Patches बनाना

git format-patch <commit> एक raw diff से आगे जाता है: यह प्रति commit एक .patch file produce करता है, हर एक author, date, और commit message सहित एक पूरे email-style message की तरह formatted — specifically इतनी information preserve करने के लिए designed कि receiving end original commit को उसकी file changes ही नहीं, exactly recreate कर सके।

उदाहरण: Creating Patches with Format-Patch

bash
git format-patch a1b2c3d

Patch Integrity Check करना

git apply --check (या क्या बदलेगा इसकी summary के लिए --stat) आपको actually apply करने से पहले अपनी current working directory के against एक patch validate करने देता है, बिना कोई file छुए एक ऐसी patch पकड़ते हुए जो conflicting local changes की वजह से cleanly apply नहीं होगी।

उदाहरण: Checking Patch Integrity

bash
git apply --check changes.patch
git apply --stat changes.patch

Patches Apply करना

git apply <patch> आपकी working directory पर एक plain diff-style patch को एक unstaged change की तरह apply करता है, file contents update करते हुए लेकिन कोई commit न बनाते हुए — आपको फिर भी बाद में खुद result git add और commit करना होगा।

उदाहरण: Applying Patches

bash
git apply changes.patch

Patches Apply और Commit करना

git am <patch> specifically format-patch से आई patches के लिए बना है: यह changes apply करता है AND patch file में embedded author, date, और message इस्तेमाल करके original commit recreate करता है — effectively किसी और की commit को आपकी repository में transplant करते हुए जैसे आपने इसे pull से receive किया हो।

उदाहरण: Applying and Committing Patches

bash
git am 0001-fix-login-bug.patch
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. git diff से एक patch बनाना और commit information की उम्मीद करना, जबकि git format-patch में author और message शामिल होते हैं।
  2. बिना git apply --check के एक अलग base commit पर एक patch apply करना, ताकि यह बीच में fail हो जाए।
  3. एक format-patch file के लिए git apply इस्तेमाल करना और एक commit की उम्मीद करना, जबकि git am इसे एक commit की तरह apply करता है।
चैप्टर सारांश
  • Advanced commands में submodules, hooks, और GitFlow workflows शामिल हैं।
  • git bisect और git blame यह track करने में मदद करते हैं कि changes कब और कहाँ हुए।
  • git archive और git patch changes package और share करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.