git चीटशीट

2.47

git commands जो आप हर दिन इस्तेमाल करेंगे

हिन्दी: 2026-03-29

Git उन tools में से एक है जहाँ आप सालों तक लगभग 15 commands से काम चला सकते हैं — और फिर एक दिन आप गलती से अपनी main branch को rebase कर देते हैं और अचानक चाहते हैं कि कुछ और सीख लिए होते। यह cheatsheet वही "कुछ और" है, बिना panic attack के।

यहाँ एक बात है जो शुरू में कोई नहीं बताता: git वास्तव में snapshots का एक directed acyclic graph है। हर commit एक snapshot है, हर branch एक pointer है, और HEAD बस एक sticky note है जो कहता है "तुम यहाँ हो।" एक बार यह click हो जाए, तो बाकी सब — rebasing, cherry-picking, resetting — dark magic लगना बंद हो जाता है और sticky notes इधर-उधर करना लगने लगता है।

हमने इसे alphabetically के बजाय workflow के अनुसार organize किया है, क्योंकि कोई नहीं सोचता "मुझे R से शुरू होने वाला command चाहिए।" आप सोचते हैं "मुझे वो आखिरी commit undo करना है" या "मुझे अपने colleague की branch लेनी है।" अगर अभी शुरुआत कर रहे हैं तो Basic Workflow से शुरू करें, फिर comfortable होने पर branch करें (pun बिलकुल intended)। Stash और Reset sections हफ्ते में कम से कम एक बार आपकी जान बचाएंगे।

एक golden rule: जल्दी commit करें, बार-बार commit करें। छोटे commits review करने में आसान हैं, revert करने में आसान हैं, और छह महीने बाद समझने में आसान हैं जब आप git log पढ़ रहे होंगे और सोच रहे होंगे कि past-you क्या सोच रहा था।

Basic Workflow
git init
वर्तमान directory में नया repository बनाएँ
git clone <url>
Repository और उसकी पूरी history download करें
git status
Changed, staged, और untracked files दिखाएँ
git add <file>
अगले commit के लिए specific file stage करें
git add .
वर्तमान directory में सभी changes stage करें
git commit -m "<msg>"
Message के साथ staged changes commit करें
git push
Local commits remote पर upload करें
git pull
Remote changes fetch करें और अपनी branch में merge करें
Branching
git branch
सभी local branches की list
git branch -a
Local और remote branches की list
git branch <name>
नई branch बनाएँ (लेकिन current पर ही रहें)
git switch <name>
मौजूदा branch पर switch करें
git switch -c <name>
नई branch बनाएँ और उस पर switch करें
git branch -d <name>
Branch delete करें (safe — unmerged हो तो block करता है)
git branch -D <name>
Unmerged होने पर भी branch force-delete करें
git branch -m <new>
वर्तमान branch rename करें
Merging और Rebasing
git merge <branch>
Branch को अपनी current branch में merge करें
git merge --no-ff <branch>
Merge commit के साथ merge करें, भले ही fast-forward possible हो
git rebase <branch>
अपने commits को दूसरी branch के ऊपर replay करें
git rebase --abort
चल रहा rebase cancel करें और original state restore करें
git rebase --continue
Conflicts resolve करने के बाद rebasing जारी रखें
git merge --abort
चल रहा merge cancel करें
git cherry-pick <hash>
दूसरी branch से एक single commit apply करें
Stash
git stash
सभी uncommitted changes को temporarily shelve करें
git stash -u
Untracked files सहित stash करें
git stash pop
सबसे recent stash restore करें और हटा दें
git stash apply
सबसे recent stash restore करें लेकिन list में रखें
git stash list
सभी stashed changesets दिखाएँ
git stash drop
सबसे recent stash delete करें
git stash drop stash@{n}
Index से specific stash delete करें
Log और Diff
git log
Current branch की commit history दिखाएँ
git log --oneline
Compact एक-line-per-commit history
git log --graph --oneline
Branch history ASCII graph के रूप में दिखाएँ
git diff
Last commit की तुलना में unstaged changes दिखाएँ
git diff --staged
Last commit की तुलना में staged changes दिखाएँ
git diff <branch1> <branch2>
दो branches की तुलना करें
git show <hash>
किसी specific commit में changes दिखाएँ
git log -p <file>
File की पूरी change history दिखाएँ
Reset और Revert
git reset <file>
File unstage करें लेकिन changes रखें
git reset HEAD~1
आखिरी commit undo करें, changes staged रखें
git reset --soft HEAD~1
आखिरी commit undo करें, changes staged रखें (explicit)
git reset --hard HEAD~1
आखिरी commit undo करें और सभी changes permanently discard करें
git revert <hash>
पिछले commit को undo करने वाला नया commit बनाएँ
git checkout -- <file>
Specific file में unstaged changes discard करें
git restore <file>
Unstaged changes discard करें (modern syntax)
git restore --staged <file>
File unstage करें (modern syntax)
Remotes
git remote -v
सभी remotes उनकी URLs के साथ दिखाएँ
git remote add <name> <url>
नया remote repository add करें
git fetch
Remote changes merge किए बिना download करें
git fetch --prune
Fetch करें और stale remote-tracking branches हटाएँ
git pull --rebase
Remote changes pull करें और अपना काम ऊपर rebase करें
git push -u origin <branch>
Branch push करें और remote track करने के लिए set करें
git push origin --delete <branch>
Remote branch delete करें
Tags
git tag
सभी tags की list
git tag <name>
Current commit पर lightweight tag बनाएँ
git tag -a <name> -m "<msg>"
Message के साथ annotated tag बनाएँ
git push --tags
सभी local tags remote पर push करें
git tag -d <name>
Local tag delete करें
git push origin --delete <tag>
Remote tag delete करें

git checkout के बजाय git switch और git restore इस्तेमाल करें। Checkout command बहुत ज़्यादा काम करता है — switch branches handle करता है, restore files handle करता है। स्पष्ट intent, कम गलतियाँ।

History linear रखने के लिए plain git pull के बजाय git pull --rebase चलाएँ। और भी अच्छा, इसे default बना दें: git config --global pull.rebase true

आखिरी commit message में typo? git commit --amend -m "fixed message" इसे rewrite कर देता है। बस पहले से push किए commits amend मत करें — वह history rewrite करता है जिस पर आपकी team depend कर सकती है।

Plain git stash के बजाय git stash -u इस्तेमाल करें। -u flag untracked files को शामिल करता है, जो लगभग हमेशा वही है जो आप चाहते हैं। इसके बिना, जो नई files आपने अभी add नहीं की हैं वे पीछे रह जाती हैं।

git reflog आपकी time machine है। एक बुरे reset --hard के बाद भी, आपके commits लगभग 30 दिनों तक वाकई gone नहीं होते। git reflog चलाएँ, जो hash चाहिए वह ढूंढें, और recover करने के लिए git reset --hard <hash> चलाएँ।

बड़े merge या rebase से पहले, git branch backup-before-merge से एक "safety branch" बनाएँ। अगर सब कुछ गड़बड़ हो जाए, तो आप हमेशा जहाँ शुरू किया था वहाँ वापस जा सकते हैं। सस्ता insurance।

अपनी पूरी branch topology terminal में visualize करने के लिए git log --oneline --graph --all इस्तेमाल करें। Multiple active branches वाले repo में क्या हो रहा है यह समझने का सबसे तेज़ तरीका है।

Related Tools