← Back to Git Course | Chapter 2: Basic Commands | Lesson 18 of 18

Git ignore (.gitignore)

एक .gitignore file album के लिए एक do-not-photograph list जैसी है। इस list पर जो भी है, जैसे secret passwords या गंदी temporary files, कभी शामिल नहीं होता।
Syntax
bash
# .gitignore
file_name
*.extension
directory_name/
!exception_file

Specific Files Ignore करना

एक .gitignore file उन patterns list करती है जिन्हें Git को कभी track नहीं करना चाहिए -- एक specific file ignore करने के लिए, बस अपनी line पर इसका exact name लिखें (जैसे secrets.env), और Git इसे git status या git add में offer करना बंद कर देगा।

उदाहरण: Ignoring Specific Files

bash
echo "secrets.env" >> .gitignore

Extension से Files Ignore करना

* wildcard किसी भी characters से match करता है, इसलिए *.log नाम की परवाह किए बिना .log से खत्म होने वाली हर file ignore करता है -- temporary build output या debug logs के लिए एक common pattern जिन्हें आप कभी गलती से commit नहीं करना चाहते।

उदाहरण: Ignoring Files by Extension

bash
echo "*.log" >> .gitignore

Directories Ignore करना

एक pattern को एक trailing slash से खत्म करना, जैसे node_modules/, Git को पूरी directory और इसके अंदर सब कुछ ignore करने को कहता है, जो बड़े dependency folders exclude करने के लिए ज़रूरी है जिन्हें version control में नहीं रहना चाहिए।

उदाहरण: Ignoring Directories

bash
echo "node_modules/" >> .gitignore

Ignore Rules के Exceptions बनाना

कभी-कभी आपको एक specific exception वाला एक broad ignore rule चाहिए होता है -- एक pattern को ! से prefix करना, जैसे !important.log, Git को उस एक file को track करने को कहता है भले ही एक wider rule अन्यथा इसे ignore करती।

उदाहरण: Creating Exceptions to Ignore Rules

bash
echo "*.log" >> .gitignore
echo "!important.log" >> .gitignore

Ignored Status Check करना

अगर कोई file mysteriously track होने से मना कर दे और आपको पता न चले क्यों, git check-ignore -v <file> exactly दिखाता है कि कौन सी line, किस .gitignore file में, इसे exclude करने के लिए जिम्मेदार है -- guess करने से कहीं तेज़।

उदाहरण: Checking Ignored Status

bash
git check-ignore -v debug.log
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. किसी file को पहले से commit हो जाने के बाद .gitignore में add करना और इसके गायब होने की उम्मीद करना, जबकि Git इसे तब तक track करता रहता है जब तक आप git rm --cached file न चलाएँ।
  2. *.log को गलत जगह या *.logs जैसे typo के साथ लिखना, ताकि intended files ignore न हों।
  3. यह उम्मीद करना कि .gitignore पहले से pushed secrets protect करेगा, जबकि पहले की commits अभी भी उन्हें contain करती हैं।
चैप्टर सारांश
  • git init, git clone, git add, और git commit repositories बनाते हैं और staging environment इस्तेमाल करके changes record करते हैं।
  • git status, git log, git diff, और git show यह inspect करते हैं कि क्या बदला है।
  • git rm, git mv, git restore, git clean, git stash, tags, और .gitignore files और work in progress manage करते हैं।

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.