Git क्या है?#
Git एक Version Control System (VCS) है जिसका इस्तेमाल developers अपने code और project files में होने वाले changes को track करने के लिए करते हैं।
Simple words mein:
Git आपके project की history को maintain करता है ताकि आप जान सकें कि क्या change हुआ, कब change हुआ और किसने change किया।
मान लीजिए आपने एक website बनाई।
आज आपने:
index.html
style.css
script.jsबनाए।
कल आपने script.js में कुछ changes किए और उससे application खराब हो गई।
Git की मदद से आप पुराने working version को देख सकते हैं और जरूरत पड़ने पर उस version पर वापस जा सकते हैं।
Git क्यों जरूरी है?#
जब project छोटा होता है तो files की copies बनाकर काम चल सकता है:
project-final
project-final-new
project-final-new-2
project-final-latest
project-final-latest-real😄 लेकिन real projects में यह तरीका जल्दी messy हो जाता है।
Git इस problem को solve करता है।
Git की मदद से आप:
- Changes track कर सकते हैं
- पुराने versions देख सकते हैं
- Previous version पर वापस जा सकते हैं
- Multiple developers के साथ काम कर सकते हैं
- Branch बना सकते हैं
- Features को अलग-अलग develop कर सकते हैं
- Code merge कर सकते हैं
- Remote repository पर code push कर सकते हैं
- Team के साथ collaboration कर सकते हैं
Git और GitHub में क्या difference है?#
यह बहुत common confusion है।
| Git | GitHub |
|---|---|
| Version Control System | Code hosting platform |
| आपके computer पर चलता है | Internet पर hosted service |
| Changes track करता है | Repository host करता है |
| Local repository रखता है | Remote repository रखता है |
git commit | Pull Request |
git branch | Repository collaboration |
Simple example:
Git = Tool
GitHub = Online PlatformGitHub के अलावा GitLab और Bitbucket जैसी services भी हैं।
Git कैसे काम करता है?#
Git को समझने के लिए तीन important areas समझिए:
Working Directory
↓
Staging Area
↓
Git Repository1. Working Directory
जहाँ आप actual files में changes करते हैं।
Example:
index.html
style.css
app.js2. Staging Area
जिसमें आप बताते हैं कि कौन से changes अगले commit में जाने चाहिए।
git add index.html3. Repository
जहाँ Git committed history store करता है।
git commit -m "Add homepage"इसलिए basic workflow:
Edit
↓
git add
↓
git commitGit Repository क्या है?#
Git repository वह project location है जहाँ Git project की history maintain करता है।
किसी existing project में Git initialize करने के लिए:
git initइसके बाद project में .git directory बनती है।
my-project/
├── .git/
├── index.html
├── style.css
└── app.js.git directory Git की internal information और history के लिए important होती है।
Git Install Check कैसे करें?#
Terminal में:
git --versionExample:
git version 2.x.xअगर Git installed है तो version दिखाई देगा।
Git Configuration#
Git में अपना name और email configure करना useful है क्योंकि commits में author information record होती है।
Name
git config --global user.name "Your Name"git config --global user.email "you@example.com"Configuration check करने के लिए:
git config --global --listSpecific value देखने के लिए:
git config user.namegit config user.emailGit Repository Initialize करना#
किसी existing project को Git repository बनाने के लिए:
git initExample:
mkdir my-project
cd my-project
git initअब Git इस project को track कर सकता है।
Git Status Command#
सबसे useful Git commands में से एक:
git statusयह बताता है:
- कौन सी files modified हैं
- कौन सी files staged हैं
- कौन सी files untracked हैं
- current branch कौन सी है
Example:
git statusजब भी confusion हो, पहले git status चलाना एक अच्छी habit है।
Git Add Command#
Changes को staging area में add करने के लिए:
git add filenameExample:
git add index.htmlMultiple files:
git add index.html style.cssCurrent directory के सभी changes add करने के लिए:
git add .ध्यान रखें
git add .का मतलब है current directory के applicable changes को staging में add करना।
Commit करने से पहले:
git statusसे check कर लेना useful है।
Git Commit Command#
Staged changes को Git history में save करने के लिए:
git commit -m "Add homepage"यहाँ:
commitGit को change save करने के लिए कहता है।
और:
-m "Add homepage"commit message देता है।
Good commit message
git commit -m "Add login page"git commit -m "Fix mobile navigation"git commit -m "Update product API"Commit message ऐसा रखें जिससे बाद में समझ आए कि change क्या था।
Basic Git Workflow#
एक normal workflow:
git status
git add .
git commit -m "Add new feature"Mental model:
Code Change
↓
git status
↓
git add
↓
Staging Area
↓
git commit
↓
Git HistoryGit Log#
Commits की history देखने के लिए:
git logShort version:
git log --onelineExample:
a82f31a Add login page
91bd22c Fix navbar
72aa912 Initial commit--oneline history को compact format में दिखाता है।
Git Diff#
Current changes देखने के लिए:
git diffयह बताता है कि files में क्या बदला है।
Staged changes देखने के लिए:
git diff --stagedइससे commit करने से पहले changes review किए जा सकते हैं।
Git Clone#
Remote repository की local copy बनाने के लिए:
git clone <repository-url>Example:
git clone https://github.com/user/project.gitइसके बाद:
cd projectअब आप local project पर काम कर सकते हैं।
Git Remote#
Remote repositories देखने के लिए:
git remote -vRemote add करने के लिए:
git remote add origin <repository-url>यहाँ origin remote का commonly used name है।
Git Push#
Local commits को remote repository पर भेजने के लिए:
git pushपहली बार branch push करते समय अक्सर:
git push -u origin main-u upstream relationship set करता है।
इसके बाद future में:
git pushकाफी हो सकता है।
Git Pull#
Remote repository से latest changes लेकर current branch को update करने के लिए:
git pullSimple workflow:
Remote Repository
↓
git pull
↓
Local RepositoryTeam project में काम करते समय push करने से पहले latest changes pull करना useful हो सकता है।
Git Fetch#
Remote से latest information download करने के लिए:
git fetchDifference समझिए:
git fetch
↓
Remote information download करता है
git pull
↓
Fetch + integration करता हैअगर आप पहले remote changes inspect करना चाहते हैं तो git fetch useful हो सकता है।
Git Branch क्या है?#
Branch को project की अलग development line की तरह समझ सकते हैं।
Example:
main
│
├── feature-login
├── feature-payment
└── bugfix-navbarइससे आप main code को directly disturb किए बिना feature develop कर सकते हैं।
Current Branch Check करना#
git branchCurrent branch के सामने * दिखाई देता है।
Example:
* main
feature-loginNew Branch बनाना#
git branch feature-loginलेकिन यह command सिर्फ branch create करती है।
उस branch पर जाने के लिए:
git switch feature-loginBranch Create और Switch Together#
Modern Git में:
git switch -c feature-loginयह:
- नई branch बनाता है
- उसी branch पर switch करता है
Branch Switch करना#
git switch mainया किसी existing branch पर:
git switch feature-loginBranch Delete करना#
Merged local branch delete करने के लिए:
git branch -d feature-loginForce delete:
git branch -D feature-login-D use करते समय सावधान रहें क्योंकि unmerged work खो सकता है।
Git Merge#
एक branch के changes दूसरी branch में combine करने के लिए:
git switch main
git merge feature-loginMental model:
feature-login
↓
merge
↓
mainMerge conflict होने पर Git आपको बताएगा कि कौन से parts conflict कर रहे हैं।
Merge Conflict क्या है?#
जब Git automatically decide नहीं कर पाता कि दो changes में कौन सा version रखना है, तो merge conflict हो सकता है।
Example:
<<<<<<< HEAD
Current code
=======
Incoming code
>>>>>>> feature-loginआपको manually correct code रखना होगा।
फिर:
git add .
git commitया situation के अनुसार merge process complete करें।
Git Restore#
Working tree में किए गए unstaged changes को restore करने के लिए:
git restore filenameExample:
git restore style.cssध्यान दें: इससे uncommitted changes खो सकते हैं। इसलिए command use करने से पहले confirm करें कि changes की जरूरत नहीं है।
Git Reset#
Git में reset powerful command है और इसे carefully use करना चाहिए।
Staged file को staging area से हटाने के लिए:
git reset HEAD filenameModern Git में:
git restore --staged filenameयह file के changes को delete नहीं करता; केवल staging से हटाता है।
Git Revert#
किसी पुराने commit के effect को reverse करने के लिए:
git revert <commit-hash>यह generally एक नया commit बनाकर previous change को reverse करता है।
Shared branches पर history को safely undo करने के लिए revert अक्सर useful approach होता है।
.gitignore क्या है?#
कुछ files को Git tracking से बाहर रखने के लिए .gitignore file use होती है।
Example:
node_modules/
.env
dist/
*.logTypical project में:
my-project/
├── .git/
├── .gitignore
├── package.json
├── src/
└── README.md.env क्यों ignore करते हैं?
.env में API keys, passwords या other secrets हो सकते हैं।
उन्हें public repository में commit नहीं करना चाहिए।
Git में Untracked File क्या है?#
अगर नई file बनाई गई है और Git अभी उसे track नहीं कर रहा:
git statusमें वह untracked दिखाई दे सकती है।
Example:
Untracked files:
app.jsTrack करने के लिए:
git add app.jsGit में Modified और Staged का Difference#
मान लीजिए आपने app.js change किया।
पहले:
Modifiedफिर:
git add app.jsअब:
Stagedफिर:
git commit -m "Update app logic"अब change commit history में चला गया।
Git में HEAD क्या है?#
HEAD generally उस commit को refer करता है जिस पर आपका current Git context/branch point कर रहा है।
Simple mental model:
HEAD
↓
Current Commit
↓
Previous Commit
↓
Older CommitGit commands जैसे git reset, git revert और git diff समझने में HEAD concept useful है।
Git Tags#
Specific commits को important version names देने के लिए tags use किए जा सकते हैं।
Example:
git tag v1.0.0Tags देखने के लिए:
git tagयह releases को identify करने में useful है।
Git Stash#
अगर आपके पास uncommitted changes हैं लेकिन temporarily branch switch करना है, तो:
git stashChanges temporarily stash हो जाते हैं।
बाद में:
git stash popसे वापस ला सकते हैं।
Stashes देखने के लिए:
git stash listPractical Git Workflow#
मान लीजिए आप किसी website में login feature बना रहे हैं।
Step 1: Repository clone करें
git clone https://github.com/example/project.git
cd projectStep 2: New branch बनाएं
git switch -c feature-loginStep 3: Code लिखें
login.html
login.js
login.cssStep 4: Changes check करें
git statusStep 5: Changes stage करें
git add .Step 6: Commit करें
git commit -m "Add login feature"Step 7: Remote पर push करें
git push -u origin feature-loginइसके बाद team workflow के अनुसार Pull Request बनाया जा सकता है।
Daily Git Workflow#
एक developer के daily workflow को simple तरीके से:
git pull
git statusकाम करें:
Write Code
↓
Test
↓
git status
↓
git add .
↓
git commit
↓
git pushलेकिन exact workflow team/project के rules पर depend कर सकता है।
Common Git Mistakes#
1. Commit से पहले status check न करना
Habit:
git status2. हर चीज़ git add . कर देना
पहले check करें कि कौन सी files stage होनी चाहिए।
git status3. Secrets commit करना
इन files को carefully handle करें:
.env
credentials
private keys
API secrets.gitignore का use करें।
4. बिना समझे git reset --hard चलाना
यह working changes को permanently discard कर सकता है।
Command use करने से पहले अच्छी तरह समझें कि आप क्या reset कर रहे हैं।
5. Poor commit messages
Bad:
git commit -m "changes"Better:
git commit -m "Fix checkout validation"6. सीधे main branch पर काम करना
Team projects में feature branches useful होती हैं:
feature-login
feature-payment
bugfix-navbarUseful Git Commands Quick Reference#
| काम | Command |
|---|---|
| Git version | git --version |
| Repository initialize | git init |
| Repository clone | git clone <url> |
| Status check | git status |
| Changes देखना | git diff |
| Stage file | git add <file> |
| Stage all | git add . |
| Commit | git commit -m "message" |
| History | git log |
| Short history | git log --oneline |
| Branch list | git branch |
| New branch | git branch <name> |
| Create + switch | git switch -c <name> |
| Switch branch | git switch <name> |
| Merge | git merge <branch> |
| Remote list | git remote -v |
| Pull changes | git pull |
| Fetch changes | git fetch |
| Push changes | git push |
| Stash changes | git stash |
| Restore stash | git stash pop |
| Create tag | git tag <name> |
| Undo commit effect | git revert <hash> |
Git Commands याद रखने का आसान तरीका#
पूरे Git को एक simple flow में याद रखें:
START
↓
git clone / git init
↓
git status
↓
Make Changes
↓
git add
↓
git commit
↓
git pushTeam project में:
git pull
↓
Make Changes
↓
git add
↓
git commit
↓
git pushBranch-based development:
main
↓
git switch -c feature
↓
Code
↓
add
↓
commit
↓
push
↓
Merge / Pull RequestKnowledge Check#
Q1. Git क्या है?
A. Programming Language
B. Version Control System
C. Database
D. Operating System
Answer: B
Q2. git add क्या करता है?
A. Remote repository delete करता है
B. Changes को staging area में रखता है
C. Branch delete करता है
D. Git install करता है
Answer: B
Q3. Commit बनाने के लिए कौन सा command use होता है?
A. git save
B. git commit
C. git push
D. git upload
Answer: B
Q4. Remote repository से changes लेने के लिए commonly कौन सा command use होता है?
A. git pull
B. git send
C. git upload
D. git copy
Answer: A
Q5. New branch create और switch करने के लिए कौन सा command useful है?
A. git branch -delete
B. git switch -c <name>
C. git new <name>
D. git create <name>
Answer: B
Practical Exercises#
Exercise 1 — अपना पहला Repository बनाएं
mkdir git-practice
cd git-practice
git initफिर एक file बनाएं:
README.mdऔर:
git status
git add README.md
git commit -m "Add README"Exercise 2 — Branch Practice
git switch -c feature-testएक file बनाएं और commit करें:
git add .
git commit -m "Add feature test"फिर main पर वापस जाएँ:
git switch mainExercise 3 — Git History देखें
कम से कम 3 commits बनाकर:
git log --onelineचलाएँ और अपनी project history देखें।
Exercise 4 — Stash Practice
कुछ changes करें लेकिन commit न करें:
git statusफिर:
git stashऔर बाद में:
git stash popसे changes वापस लाएँ।
Git सीखने के बाद क्या सीखें?#
The Easy Master learning path में Git के बाद GitHub आता है।
Recommended sequence:
Computer Fundamentals
↓
Terminal / Shell
↓
Git
↓
GitHub
↓
Internet Fundamentals
↓
DNS
↓
HTTP / HTTPS
↓
Browser DevToolsGit के बाद GitHub सीखने से आपको local Git + remote collaboration दोनों concepts समझने में मदद मिलेगी।
Summary#
आसान भाषा में Git को ऐसे याद रखें:
Git आपके project की history और changes को manage करने वाला Version Control System है।
सबसे important commands:
git init
git clone
git status
git add
git commit
git log
git branch
git switch
git merge
git pull
git fetch
git push
git stash
git revertसबसे important workflow:
Change
↓
Status
↓
Add
↓
Commit
↓
Push
और team development में:
Pull
↓
Branch
↓
Code
↓
Test
↓
Commit
↓
Push
↓
Review / Merge
Git का goal commands याद करना नहीं है — बल्कि project changes को safely track और manage करना समझना है।