← Back to Git Course | Chapter 8: Best Practices | Lesson 4 of 6

Git Common Mistakes

Common Git mistakes same rug पर बार-बार ठोकर खाने जैसे हैं: गलत branch पर save करना, या बहुत जल्दी push करना। इन्हें जानना आपको काम खोए बिना उन्हें fix करने में मदद करता है।

गलत Branch पर Commit करना

जब आप एक feature branch पर होना चाहते थे तब सीधे main में commit करना काम खोए बिना fix करने लायक है: अपनी current commit पर एक नई branch बनाएँ, फिर main को वापस उसकी पहले वाली position पर reset करें (git reset --hard origin/main) ताकि commit सिर्फ नई branch पर रहे।

उदाहरण: Committing to the Wrong Branch

bash
git branch feature-x
git reset --hard HEAD~1
git checkout feature-x

एक File Add करना भूल जाना

यह realize करना कि आप पहले से commit करने के बाद एक file git add करना भूल गए एक बिल्कुल नई commit की माँग नहीं करता — missing file stage करें और अभी बनाई commit में इसे fold करने के लिए git commit --amend --no-edit चलाएँ, जब तक आपने अभी push न किया हो।

उदाहरण: Forgetting to Add a File

bash
git add forgotten-file.txt
git commit --amend --no-edit

Accidental Hard Reset

एक git reset --hard जो intended से ज़्यादा पीछे जाता है catastrophic महसूस होता है, लेकिन commits आमतौर पर actually delete नहीं होतीं — git reflog अभी भी record रखता है कि आपकी branch थोड़ी देर पहले कहाँ point करती थी, इसलिए आप पुराना commit hash ढूँढकर उस पर वापस reset कर सकते हैं।

उदाहरण: Accidental Hard Reset

bash
git reflog
git reset --hard a1b2c3d

Messy States Fix करना

जब एक merge या rebase आपकी branch को एक ऐसी state में छोड़ दे जिसे आप समझते और trust नहीं करते, बाहर निकलने का सबसे तेज़ तरीका अक्सर इससे लड़ते रहना नहीं है — git reset --hard origin/<branch> local confusion फेंक देता है और आपको remote पर सबसे हाल की known-good state के साथ realign कर देता है (किसी भी uncommitted local काम की कीमत पर)।

उदाहरण: Fixing Messy States

bash
git reset --hard origin/main

पहले से Tracked एक File को Untrack करना

पहले से Git द्वारा tracked एक file के लिए .gitignore में add करना कुछ नहीं करता — ignore rules सिर्फ *untracked* files पर apply होते हैं। इसे track करना रोकने के लिए आपको पहले git rm --cached <file> चलाना होगा, और तभी .gitignore actually इसे future commits से बाहर रखता है।

उदाहरण: Untracking an Already Tracked File

bash
echo "config.local.json" >> .gitignore
git rm --cached config.local.json
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. गलती से main पर commit करना और branch पर काम save करने से पहले git reset --hard इस्तेमाल करना, commits खोते हुए।
  2. एक भूली हुई file के लिए एक नई commit बनाना बजाय git commit --amend --no-edit इस्तेमाल करने के, history को गंदा करते हुए।
  3. एक गलत git reset --hard के बाद panic करना और यह मान लेना कि commits चली गईं, जबकि git reflog आमतौर पर दिखाता है कि उन्हें कैसे recover करना है।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 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.