← Back to Git Course | Chapter 10: Tools & Internals | Lesson 7 of 8

Git .gitattributes

एक .gitattributes file file labels पर special instructions के एक set जैसी है, Git को बताते हुए कि कुछ files कैसे handle करनी हैं, जैसे कौन सी pictures हैं text नहीं।
Syntax
bash
# .gitattributes
pattern attribute
* text=auto
*.extension binary

.gitattributes क्या है

एक .gitattributes file आपको यह बताने देती है कि Git specific files या patterns को कैसे treat करे -- line-ending normalization, एक file को binary mark करना, या एक diff/merge strategy चुनना -- ऐसे rules जो repository clone करने वाले हर किसी पर apply होते हैं, personal Git config के उलट।

उदाहरण: What is .gitattributes

bash
echo "*.psd binary" > .gitattributes

Line Endings Normalize करना

Windows और Unix-based systems अलग-अलग line-ending characters इस्तेमाल करते हैं (CRLF बनाम LF); normalization के बिना, दोनों में shared एक repository सिर्फ line-ending differences की वजह से किसी file की हर line को बदली हुई दिखा सकता है, diffs को noise से भर देते हुए।

उदाहरण: Normalizing Line Endings

bash
echo "* text=auto" >> .gitattributes

Files को Binary Mark करना

किसी file type को explicitly binary mark करना Git को इसे text की तरह merge या diff करने की कोशिश से रोकता है, जो images या compiled archives जैसे genuinely binary formats के लिए अन्यथा meaningless या broken output produce करता।

उदाहरण: Marking Files as Binary

bash
echo "*.png binary" >> .gitattributes

Custom Diff और Merge Drivers

उन file types के लिए जिन्हें Git out of the box meaningfully diff नहीं कर सकता -- जैसे Jupyter notebooks या Word documents -- .gitattributes एक custom diff या merge driver script की ओर point कर सकती है जो उस format की structure समझती है।

उदाहरण: Custom Diff and Merge Drivers

bash
echo "*.ipynb diff=jupyternotebook" >> .gitattributes

.gitattributes बनाम .gitignore

दोनों files अलग-अलग problems solve करती हैं: .gitignore Git को बताती है कि किन files को कभी track नहीं करना, जबकि .gitattributes यह बदलती है कि Git जिन files को track कर ही रहा है उन्हें कैसे treat करता है -- line endings, diff behavior, export handling -- वे complementary हैं, alternatives नहीं।

उदाहरण: .gitattributes vs .gitignore

bash
echo "node_modules/" >> .gitignore
echo "*.psd binary" >> .gitattributes
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. .gitattributes में * text=auto add करना और git add --renormalize . न चलाना, ताकि files अपने पुराने line endings रखें।
  2. files को binary mark न करना, ताकि Git उन्हें text की तरह diff या merge करने की कोशिश करे।
  3. file को .gitattribute या gitattributes नाम देना, ताकि Git इसे न पढ़े; नाम exactly .gitattributes होना चाहिए।

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.