हर developer Git इस्तेमाल करता है। यह साइट भी Git में stored है। फिर भी इसे शून्य से शायद ही समझाया जाता है। अच्छी खबर: Git एक बार mental model समझ लेने पर सरल है – और रोज़मर्रा के 90% काम के लिए आपको सिर्फ कुछ ही commands चाहिए।
Git कौन सी समस्या हल करता है?
सोचिए आप एक document edit कर रहे हैं और लगता है, "पिछला version बेहतर था"। version control के बिना, आप copies save करेंगे: report_final.doc, report_final_v2.doc, report_FINAL_actually.doc। Chaos।
Git आपको हर saved point पर पूरे project का snapshot देता है। आप कर सकते हैं:
- किसी भी पुराने version पर वापस जाना
- देखना कि क्या बदला और कब
- बिना working code को जोखिम में डाले experiments आज़माना
- दूसरों के साथ collaborate करना बिना एक-दूसरे को overwrite किए
Mental model: saves और save-points
दो ideas जो आपको चाहिए:
- Commit – एक save point। "fix login bug" जैसे message के साथ आपकी files का snapshot।
- Repository (repo) – एक folder जिसे Git track करता है। आपके सारे commits यहीं रहते हैं।
commits को video game के save points की तरह सोचें। आप उन्हें लगातार बनाते हैं ताकि हमेशा rewind कर सकें। git history बस commits की एक chain है।
वो 5 commands जो सब कुछ कवर करती हैं
शुरुआती की पूरी toolkit यहाँ है:
git init # start tracking this folder (do once) git add . # stage your changes (choose what to save) git commit -m "message" # create a save point git status # what's changed? (check constantly) git log # see the history of commits
add फिर commit यही rhythm है। पहले आप चुनते हैं क्या save करना है, फिर snapshot लेते हैं। आप git status लगातार चलाएँगे – यह safety check है, कभी chore नहीं।
Branching: parallel universes
branch development की एक अलग line है – project की एक copy जिसमें आप main line को प्रभावित किए बिना experiment कर सकते हैं।
git branch new-feature # create a branch git switch new-feature # move to it # ...work and commit here... git switch main # go back git merge new-feature # bring the work into main
अकेले सीखने वाले beginners के लिए branches शुरू में optional लगती हैं – और हैं भी। पर इन्हें जल्दी सीखने से आप real teams के काम करने के तरीके से comfortable हो जाते हैं। सुनहरा नियम: कुछ risky try करते समय कभी भी सीधे main पर commit न करें।
GitHub: cloud में Git
GitHub Git repositories के लिए hosting service है – आपके code के लिए Dropbox जैसा, साथ में collaboration tools। उन्हें जोड़ने वाले commands:
git clone URL # copy a repo to your computer (start) git push # upload your commits git pull # download others' commits (or your own from another machine)
push आपके saves को GitHub पर भेजता है; pull उन्हें नीचे लाता है। यही तरीका है जिससे यह website deploy होती है – एक push GitHub Pages को publish करने के लिए trigger करता है।
गलतियाँ सुधारना (भरोसा देने वाला हिस्सा)
version control की सबसे अच्छी बात: लगभग कुछ भी permanent नहीं है।
git restore file # discard uncommitted changes to a file git reset --hard HEAD # throw away uncommitted changes entirely git revert <commit-id> # undo a past commit safely git log # find any commit-id you need
गड़बड़ हो गई? Restore कर लें। सब टूट गया? Reset। कुछ गलत commit कर दिया? Revert। Git का पूरा काम ही आपको undo करने देना है।
पहले दिन से अच्छी आदतें
- छोटे और अक्सर commit करें। हर feature/fix पर एक save point, साफ message के साथ।
- ईमानदार commit messages लिखें। "fix login bug" "stuff" से बेहतर है। future-you आपको धन्यवाद देगा।
- जोखिम लेने से पहले commit करें। फिर आप हमेशा rewind कर सकते हैं।
- push करने से पहले git status जाँचें, ताकि पता हो क्या बाहर जा रहा है।
एक typical पहला session
$ cd ~/myproject $ git init # start tracking $ git add . $ git commit -m "Initial version of my app" $ git branch main # name the main branch (already set on most setups) # ... edit some files ... $ git status # "modified: index.html" $ git add . $ git commit -m "Add dark mode toggle" $ git log --oneline # 2 commits, nice and tidy
Git बड़ा लगता है, पर असल में यह सिर्फ है: save points (commit), parallel lines (branch), और sync (push/pull)। इन तीन ideas में महारत हासिल कर लें, और आप version control एक pro की तरह इस्तेमाल कर रहे हैं। और सबसे अच्छी बात – एक बार शुरू करने के बाद, आप कभी इसके बिना काम नहीं करना चाहेंगे।