# Git & GitHub | oriantation class + cohort 3 + tea class 1

* * *

# 01\_Git\_And\_GitHub\_Why\_Learning\_Matters

### **Question** :: >

Sab se pehle question aata hai ke hum Git & GitHub hi kyun Study karna chahte hain? Git ke bina bhi to work ho raha tha, fir kya wajah thi ke Git & GitHub ko create/build/introduce karna pada?

### **Ans with Example :: >**

1.  Man lo ke ek developer hai Ahmad jo apni local machine par code run karta hai. Vo apna code ek pen drive mein save karke apne teammate ke saath share karta hai taake vo bhi uska use kar sake.
    
2.  Jab teammate vo code dekhta hai to khud se bhi kuch changes kar deta hai aur phir se Ahmad ko updated code ki pen drive de deta hai.
    
3.  Ab Ahmad ko nahi pata ke code mein kya ho raha hai, kaun se changes hue hain, kahan-kahan changes hue hain.
    
4.  Kuch der baad teammate ko realize hota hai ke uske code mein issue tha to vo unko fix karta hai. Phir se Ahmad ko pen drive dena parta hai. Yeh cycle aisi hi chalti rahi...
    

### **Problems They Face :: >**

1.  Agar teammate changes karta to Ahmad ko pata nahi chalta tha ke code mein kya change hua -
    
2.  Agar Ahmad changes karta to teammate ko pata nahi chalta tha ke code mein kya change hua -
    
3.  Kaunsi files change hui, kaunsi delete hui - iska pata nahi chalta tha -
    
4.  Jab bhi koi changes karta to har baar pen drive share karna parta tha - Dono ek saath kaam nahi kar sakte the (collaboration bohat mushkil tha) -
    
5.  Code ke changes ka koi history/record nahi rehta tha - purana version nahi mil sakta tha
    

### Solution - Part 1 : Git ( Local Version Control ) :: >

Now... jab unho ne problem find kiya to unho ne ek CODE TRACKER banaya jise "Git" ka naam diya. Actually, "Git" ek Version Control System (VCS) hai. Market mein doosre VCS bhi hain jaise Mercurial, Apache Subversion (SVN) etc, lekin Git most popular hai.

**Ab Git yeh kaam karta hai** …

1.  Unke code ke sab changes track karta hai -
    
2.  Batata hai ke code mein kisne, kab aur kya changes kiye -
    
3.  Modified/changed files ki history dikhaata hai -
    
4.  1 developer bhi use kar sakta hai (local mein) -
    
5.  Iss tarah se unho ne ek problem solve kar liya.
    
    * * *
    

**Lekin abhi bhi ek BADA PROBLEM tha …**

1.  Pen drive har baar share karna padta tha -
    
2.  Kaunsa version latest/updated tha iska confusion hota tha -
    
3.  Ek central backup nahi tha -
    
4.  Sirf local machine par code tha - jiske paas pen drive hoti, sirf vo hi code kar sakta tha , Yeh collaboration ka bohat bada problem tha.
    

### **Solution - Part 2 : Remote Repository (GitHub) :: >**

To unho ne mil kar ek single source SOLUTION banaya taake sab developers apna updated code ek central server par push kar saken, jise "GitHub" ka naam diya. Actually, jise ham "server" boltay hain, us concept ko "Remote Repository" kehte hain. GitHub sirf ek example hai. Doosre remote services bhi hain jaise GitLab, Bitbucket etc.

**Ab GitHub (Remote) yeh kaam karta hai ::>**

1.  Ek central server hai (cloud mein) jahan par collaboration ho sakti hai developers ki -
    
2.  Jitne bhi developers hain, sab GitHub par connect ho jate hain -
    
3.  Jab koi developer code update karta hai, vo GitHub par "Push" kar deta hai - Baaki sab developers easily latest code dekh sakte hain aur use "Pull" kar lete hain -
    
4.  Har kisi ko instantly pata chal jata hai ke code mein kya change hua -
    
5.  Code ka complete backup safely reh jaata hai cloud par Is tarah se collaboration ki problem bhi completely solve ho gaya!
    
    * * *
    
    ### **Important Note - Git is Independent :: >**
    
    Git ka use 1 developer bhi kar sakta hai apni local machine par:
    
    *   Apne code ke changes dekh sakta hai
        
    *   kal kya changes kiye the, aaj kya changes kiye hain
        
    *   Agar code mein koi issue ho jaye to purane code mein easily ja sakte hain -
        
    *   Git ko internet ya server ki zaroorat nahi - yeh LOCAL software hai Jab Git bana tha to sirf local use ke liye bana tha taake aap apne projects ko track kar sako.
        
    
    Baad mein jab collaboration ki zaroorat padi, tab GitHub jaisi remote services bani jo Git ko cloud par host karti hain.
    
    Simple Words Mein:
    
    *   Git = Local Version Control System (Apke computer par)
        
    *   GitHub = Remote Repository Hosting (Internet par, collaboration ke liye)
        
    *   GitLab = GitHub jaisa ek aur remote option
        

### Who Built Git :: >

Git ko "Linus Torvalds" ne 2005 mein banaya tha. Vo Linux Operating System par kaam kar rahe the aur us time Linux bohat tezi se grow ho raha tha. Hazaron developers ek saath kaam kar rahe the aur code ko track karna bohat mushkil ho gaya tha.

Pehle vo doosre version control systems use kar rahe the (jaise SVN) lekin vo slow aur limited the. To Linus ne socha ke khud ka version control system banana chahiye jo: - Bohat fast ho - Distributed ho (har developer ke paas poora code ho) - Reliable ho Unhone sirf kuch hafton mein Git bana diya as a side project! Aaj Git duniya ka sabse popular version control system hai.

**Fun Fact :** "Git" ka matlab English mein "annoying ya foolish person" hota hai. Linus ne mazak mein yeh naam rakha tha kyunke Git version control kaafi strict aur complicated tha! Waise bhi, Linux ke baad Git ek aur "Linus creation" hai jo duniya badal gaya!

### **Important Clarification Git vs GitHub :**

Aaj kal bohat se log resume likhtay hain "GitHub seekha" lekin TECHNICALLY GALAT HAI! Sach yeh hai:

*   ❌ "GitHub seekha" = GALAT
    
*   ✅ "Git seekha" = SAHI Kyun?
    

GitHub sirf ek HOSTING SERVICE hai jahan par code store karta hai. GitHub seekhna matlab sirf button click karna aur repository banalo. Git ek COMPLETE VERSION CONTROL SYSTEM hai jo seekhna padta hai and ham jald hi seekhe gay.

1.  Commands samajhna (init, add, commit, push, pull, branch, merge, etc.) -
    
2.  Concepts samajhna (staging, commits, history, etc.) - Workflows seekhna (how to collaborate properly) -
    
3.  Problem solving (conflicts resolve karna, etc.)
    

### **Git aur GitHub mai kya Difference hai ?**

*   "Git = Version Control System (tool/language)
    
*   GitHub = Git ke liye hosting service (platform)"
    
*   Most students GitHub account banate hain but Git properly nahi seekhte. To GitHub repository banate hain par actually kaam nahi aata Git ka! Isliye jab interview mein puchche, to proper answer dena!
    

* * *

# **02\_Git\_Complete\_Guide\_A\_to\_Z**

### Let's Learn Git :: >

Ok, sab se pehle question aata hai ke Git internally code/project ko track kaise karta hai? Vo kaise hamara complete code ke changes ko track karta hai aur hum to kuch bhi see nahi karte, fir Git ko kaise pata chal jaata hai ke code mein kya change hua?

**Ans ::>** Jab Linus Torvalds ne Git banana shuru kiya to usne socha: "Mujhe code ke changes track to krne hain, par inhe store kahan karu?" , Database (MongoDB, MySQL, PostgreSQL) mein store kar sakta tha, lekin database separate server par rehta hai - portable nahi hota (move/shift mushkil). To usne ek CLEVER SOLUTION nikala: Project ke andar hi ek ".git" folder banaya (hidden folder), Is folder mein ek special "database" hota hai (actually complex file structure hain), Jo har single change ko record karta hai.

**Now, jab koi code likhta hai aur Git se commit karte hain to:**

1.  Git automatically uski sab files ko scan karta hai
    
2.  Dekhta hai ke kya changes hue.
    
3.  Har change ko + aur - mein show karta hai :
    
    \+ = Nai line add hui - = Line remove/delete hui
    
4.  Un changes ke liye ek unique ID (hash) generate karta hai, Taake future mein is unique ID se specific change dekh saken.
    
5.  Sab kuch .git folder mein save kar deta hai , Future mein jab purane changes dekhne ho ya purana code chahiye, vo commands se dekh sakta hai
    
    ### Simple Example :
    
    **Ahmad :** "file.js mein 5 nai lines add kiye"
    
    ↓
    
    **Git :** ".git folder mein record save kiya (+ 5 lines)"
    
    ↓
    
    **Ahmad :** "git log command chalata hai"
    
    ↓
    
    **Git :** ".git folder se data nikaal kar Ahmad ko show karta hai"
    
    **IMPORTANT Point ::**
    
    .git Folder Hidden Hota Hai, Isliye Linus ne .git folder ko "hidden" banaya Taake user accidentally usse touch ya delete na kar de Windows/Mac/Linux mein hidden files/folders hote hain, Jisse .git folder safe aur protected rehta hai.
    

## GIT COMMANDS :

* * *

### **Command 1 : git init**

Ab samjho, agar hum apna khud ka Git system banana chahte hain (like Linus ne banaya tha), to terminal mein git ke commands likhnay partay hain.

```powershell
git init           # git init command : 1
```

**Git kehta hai:** "Tum git init command do, main ek .git folder banao dun ga jo hidden rhay ga tumhay usmay kuh nai krna "

**What Happens:**

Tum: "git init" command terminal mein likho aur Enter press karo

↓

**Git:** Automatically ek hidden ".git" folder banata hai jisme apko kuch nai krna

**Example:**

$ git init

**Output:** "Initialized empty Git repository in /path/to/your/project/.git/"

(Means: .git folder successfully created!)

For Verification (Check karna ke .git folder ban gaya):

**$ ls -a**

**Output:** . .. .git file.js index.html

(Dekho! .git folder dikh raha hai hidden files ke sath)

**Note:**

**ls = List files** (sirf visible files dikhata hai)

**ls -a = List ALL files** (hidden files bhi dikhata hai)

.git folder hidden hota hai isliye "ls -a" use karna padta hai.

* * *

### **Command 2 : git add . or git add**

Ab hmare paas .git folder ready hai. Lekin Git abhi tak hmare code ko track nahi kar raha.

```powershell
git add <filename>    #  It will add specific file  . you will use it in 5% cases
```

**or**

```powershell
git add .     # It will add all files  . you will use it in 95% cases
```

Git bolta hai: "Tumne code likha hai, par mujhe bataya nahi ke kaunsi files ko track karu!"

To Git kehta hai: "Tum .git add command do. Main us file ko "staging area" mein daal deta hun. Abhi track nahi, bas commit karne ke liye ready kar dunga".

What Happens:

**Tum** : "git add file.js" (ya "git add ." sab files ke liye)

↓

**Git :** "Ok, maine file.js ko track list mein daal diya"

↓

Ab ye file "staging area" mein chali gai (ose pta chl gya he ke kis file ko track krna he)

**Example:**

**$ git add file.js**

(Koi output nahi - silently uss file ko track kar leta hai)

or

**$ git add .**

(Sab files track ko track kr leta he )

ab Git ko complete pta he ke konsi file ko track krna he and usne file complete ready kr di he but track nai kia.

* * *

### **Command 3 : git commit**

Ab Git ko pata chal gaya ke kaunsi files track karni hain aur sab kuch staging area mein ready hai. Lekin abhi tak Git ne files ko .git folder mein SAVE nahi kiya.

```powershell
git commit -m "I update readme file"      # you can type any message,use it professionally
```

**Git bolta hai:** "Ab git commit command do. Main in files ko .git folder mein permanently save kar dunga aur ek unique ID (hash) dunga".

**What Happens:**

Tum: git commit -m " your message "

↓

Git: ".git folder mein files permanently save kar deta hai"

↓

Har commit ko ek unique hash (ID) generate karta hai

↓

Ab changes .git folder mein permanently record ho gayi

**Example:**

**$ git commit -m "Added login feature"**

**Output :** "\[master abc123d\] Added login feature 1 file changed, 50 insertions(+)"

(Dekho! Unique ID "abc123d" diya - and iss ID se commit ko future mein identify kar sakte ho)

* * *

COMPLETE WORKFLOW:

1.  git init \[ .git folder banao (first time only) , Code likho / changes karo \]
    
2.  git add . \[ → Git ko batao: "Inn files ko track karna hai!" \]
    
3.  git commit -m "message" \[ → Git ko kahao: "Ab save kar do unn files ko!" \]
    

**REPEAT:**

Step 2,3 and 4 har baar jab code mai koi new changes kro to repeat kro!

* * *

### **Command 4 : git log**

Poori history dekh sakte ho jab chahiye.

```powershell
git log
```

(**Output:** Sab commits with hash, author, date, aur message dikhega)

Short Version (Easy View):

```powershell
git log --oneline   # oneline view short and easy view
```

(**Output:** Sirf hash aur message dikhega - ek line per ek commit)

**Example:**

$ git log --oneline

def456f third commit

xyz789e second commit

abc123d first commit

(Bilkul short aur clean!)

* * *

### **Command 5 : git status**

Current situation dekh sakte ho - kaunsi files change hui, kaunsi staged hain, kaunsi untracked hain.

```powershell
git status
```

(**Output:** file.js aur index.html change hui, new-file.js abhi track nahi hui etc)

* * *

### **Command 6: git diff**

Do commits ke beech changes dekh sakte ho.

```powershell
git diff 840c9bd 9b17369                # use this flow -->  <old commit> <latest commit>
```

(840c9bd se 9b17369 tak kya badla)

**Important:** Order matter karta hai!

*   Pehla hash = Old (start)
    
*   Doosra hash = New (end)
    
*   Agar Reverse (new commit pehle and old hash baad mai) karo gay to code kay changes bhi reverse show honge iss liay ise nai krna
    

* * *

### **Command 7: git commit -am "message"**

**Git add aur commit ek saath (shortcut).**

```powershell
git commit -am "I added a login form"            # don`t try this in Professional work
```

**Note:**

Sirf modified files par kaam karta hai - New files add nahi hoti - Professional work mein git add + git commit alag alag use karo.

(Brief rakhna - zyada detail nahi chahiye)

* * *

### ⭐⭐⭐ IMPORTANT CONCEPT: HEAD ⭐⭐⭐

**HEAD Kya Hota Hai?**

→ Git mein latest/newest commit ko "HEAD" bola jata hai

→ HEAD = Current position jahan Git abhi khara hai

→ Jab naya commit karte ho, HEAD automatically us naye commit par move ho jata hai

**Example:**

Commit 1: abc123d (1st commit)

Commit 2: xyz789e (2nd commit) ← **HEAD yahan hai** (latest)

Ab agar tum naya commit karo:

**$ git commit -m " I Update Code "** ←- (3rd Commit)

**Now:**

Commit 1: abc123d (1st commit)

Commit 2: xyz789e (2nd commit)

Commit 3: def456f (3rd commit) ← HEAD ab yahan move ho gaya (NEW LATEST)

**— Simple Rule: HEAD hamesha latest commit ko point karta hai! —**

**Git log mein dekho ge to :**

$ git log

commit def456f (HEAD -> main) ← Ye latest hai Author: Abdul Date: Dec 31 2025 third commit

commit xyz789e Author: Abdul Date: Dec 30 2025 second commit

commit abc123d Author: Abdul Date: Dec 29 2025 first commit

* * *

### Command 8: git revert <commit\_hash>

Ek purane commit ko SAFELY undo karna (naya commit banake).

```powershell
git revert <commit_hash>         # git revert def456f
#Remember: yaha per vo hash id enter krna jise revert krna he vo add nai krna jissper jana he.
```

(Commit 3 ke changes ko undo kar deta hai, par history preserve rehti hai)

**How it works:**

Commit 1: "Added login"

Commit 2: "Added dashboard"

Commit 3: "Added payment" ❌ (galat tha)

$ git revert def456f

Commit 1: "Added login"

Commit 2: "Added dashboard"

Commit 3: "Added payment" (still in history)

Commit 4: "Revert: Added payment" ← Naya commit (commit 3 payment code removed)

**Important Points:**

1.  History preserve rehti hai (git reset nahi karte)
    
2.  Ek naya commit banata hai -
    
3.  Professional teams mein use karte hain
    
4.  Safe aur trackable hota hai
    

* * *

### **Command 9: git reset --hard <commit\_hash>**

Ek commit ke baad sab kuch DELETE karna (history se bhi).

```powershell
git reset --hard abc123d        #professional work mai use nai krna
```

(Commit 2 aur 3 completely remove ho jayengi)

**How it works:**

**Before:**

Commit 1: "login"

Commit 2: "dashboard"

Commit 3: "payment" ❌

**You type command :** $ git reset --hard abc123d

**After:**

Commit 1: "login" ← HEAD yahan (Commit 2 aur 3 delete!)

⚠️ IMPORTANT - DANGEROUS:

1.  History completely delete hoti hai (no backup or view of changes)
    
2.  Team mein problem aata hai - Professional work mein git revert prefer karo!
    
3.  Khud ke projects mein only use karo
    
4.  Recovery mushkil hota hai
    

**Professionally : git revert use karo, git reset --hard avoid karo!**

* * *

## **03\_What\`s\_Inside\_Git :**

**Sab se pehle question ::>**

" .git folder ke andar kya hai aur Git sab kuch manage kaise karta hai? "

Ab ham finally ye dekhte hain ke .git folder ke inside kya hai - akhir ye sab work aur files ko manage kaisay kr raha hai aur hamay commands se data kaise de raha hai.

* * *

1.  **STEP 1:** Check karo ke .git folder exist karta hai ya nahi
    
    ```powershell
    ls -a           # Output:  .   ..   .git   file.js   index.html
    ```
    
    **Notice :**
    
    *   .git folder likha hai (hidden folder)
        
    
    **Matlab :**
    
    *   .git folder hidden hai hamary code mai jo hum normally nahi dekh sakte,
        
    *   lekin "ls -a" se dekh sakte hain.
        
    
    * * *
    
2.  **STEP 2:** Check karo ham kis branch par hain
    
    ```powershell
    git branch          # Output: * main
    ```
    
    **Notice :** Piyush (\*) main branch par likha hai
    
    **Matlab :** Ham main branch par hain (ye mostly default case hai).
    

* * *

**STEP 3:** HEAD file dekho

```powershell
cat .git/HEAD          # Output:  ref: refs/heads/main
```

Notice: Ye likha hai "ref: refs/heads/main"

Matlab:

*   HEAD pointing kar raha hai refs/heads/main file ki taraf
    
*   Ye .git folder ke andar ka ek structure hai
    
*   HEAD batata hai: "Tum main branch par ho"
    

* * *

4.  **STEP 4: Main branch ka latest commit dekho**
    

```powershell
cat .git/refs/heads/main      # Output: abc123def456xyz789abc123def456xyz789abc123
```

**Notice:**

*   Ye ek long hash likha hai
    

**Matlab:**

*   Ye main branch ka latest commit hai
    
*   Ye hash hai jo hamara HEAD currently point kar raha hai
    
*   YE SAME HASH HAI JO GIT LOG MEIN SHOW HOTA HAI!
    

**Connection Samajho Yaha Per :**

.git/refs/heads/main file

↓

abc123def456... (HASH STORE HAI YAHAN)

↓

Jab tum "git log" karte ho

↓

Git yahi se hash le kar show karta hai!

↓

git log --oneline or (git log)

↓

abc123def (HEAD -> main) Your message

↓

SAME HASH! ✅

Matlab: Git yaha .git/refs/heads/main file se hash ID nikaal kar agay output deta hai!

* * *

5.  **STEP 5: Objects folder mein dekho - jahan actual data rehta hai**
    
    ```powershell
    ls .git/objects/      # Output: ab/ cd/ ef/ pack/
    ```
    
    **Notice:**
    
    *   Har commit ko yaha per only 2-letter se save kia jata he (ab, cd, ef)
        
    
    **Matlab:**
    
    *   Ye folders commit hashes ke first 2 letters se bane hain
        
    *   Har commit ke data ko organize karne ke liye ye structure use hota hai
        
    *   Jab tum "git log" ya "git diff" karte ho, Git yahan se data nikal kar deta hai
        

* * *

**STEP 6 : Samajho ke data kahan rakha hota hai, Jab tum "git log" command dete ho:**

$ git log --oneline

*   ham yay likh kr enter krtay hain
    

To Git ka system ye 4 process follow karta hai:

*   .git/HEAD file padhta he: ref: refs/heads/main (Git ko Pata chala: Tum main branch par ho)
    
*   .git/refs/heads/main file padhta he: abc123def456... (Ose Pata chala: Latest commit ye hai)
    
*   .git/objects/ walay folder mein gaya: waha se usne (Actual commit data nikala)
    
*   Display kiya hamay
    

Iska matlab:

*   Git .git/objects/ walay folder se hi sab data nikal kar hamay show karta hai jab ham log diff krtay hain!
    

* * *

*   **STEP 7 : Advanced - Commit ka complete detail dekho**
    
    ```powershell
         git cat-file -p abc123def456     # for complete data of any hash id
    ```
    
    Output:
    
    tree 096b74c23d...
    
    parent xyz789e...
    
    author Ahmad [ahmad@email.com](mailto:ahmad@email.com)
    
    date Dec 31 2025 message:
    
    Added login feature
    
    Notice:
    
    *   Ye full commit details likhe hain
        
    
    Matlab:
    
    *   Tree: File structure ka snapshot
        
    *   Parent: Pichla commit (linked list)
        
    *   Author: Kisne likha
        
    *   Date: Kab likha
        
    *   Message: Kya likha
        
    
    **Iska matlab: .git/objects/ mein sab kuch data hota hai !**
    

* * *

Remember it Always:

*   ls = Folder ka content dekhnay ke liay (list files/folders inside)
    
*   cat = File ka content dekhnay ke liay (read file content)
    

* * *

### **GIT KI COMPLETE KAHANI:**

1.  .git folder hidden rakha gaya (.git folder hidden hota hai)
    
2.  .git folder ke andar:
    
    *   HEAD file : Current branch pointer
        
    *   refs/heads/main : Latest commit hash store karta hai ← YE IMPORTANT HAI!
        
    *   objects/ : Actual commit data store karta hai
        
3.  Jab tum git log karte ho but git yay krta he:
    

*   HEAD file dekha
    
*   refs/heads/main dekha
    
*   objects/ folder se data nikala
    
*   Data Show kiya
    

4\. Jab tum git diff karte ho: Ye direct objects/ folder se sab files se data nikal kar show karta hai

5\. Jab tum git revert karte ho: Git .git/objects/ folder se purana commit la kar naya commit banata hai (purane changes ko undo karte hue).

6\. Verification ke liye tum kar sakte ho:

*   cat .git/HEAD (check: kis branch par ho)
    
*   cat .git/refs/heads/main (get: latest commit hash id - ye wahi hash id hay jo Git log mein show hota hai!)
    
*   git log --oneline (compare: same hash dikhega?)
    
*   Agar sab match ho = Synchronized! ✅
    

* * *

### **Complete Structure of .git Folder :**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769340293732/7fc51226-5b33-42ec-90f7-c64ac96f049e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769340603474/98d0a889-602b-48ab-b279-d49c0d3b1bda.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769340848823/47fd3587-8dfc-44ab-8f15-a154df5c426a.png align="center")

Sari kahani ko ham ese bi bol saktay hain ek line mai:

*   Git .git/refs/heads/main se latest commit hash le kar,
    
*   .git/objects/ folder se data show karta hai!
    

* * *

## Some more Commands :

### **For Cloning a Repository:**

*   If You want to clone any repository from GitHub then use
    

```powershell
git clone https://github.com/user/repo

# Ex :: git clone https://github.com/jhonspro/my-public-repo
```

*   Or for Private Repository, you need owner repository token
    

```powershell
git clone https://user:TOKEN@github.com/user/repo

# Ex: git clone https://johnspro:ghp_AbC123XyZ753ds2fdMnO678@github.com/johnspro/my-private-repo
```

* * *

### **For GitHub Remote** :

*   For Checking Git Remote, use this command in Powershell or CMD:
    
    ```powershell
    git remote -v    # It will show you current remote
    ```
    
*   For Updating Git Remote, use this Command,
    
    ```powershell
    git remote set-url origin <repo-url>   # It will update remote
    ```
    

* * *

3.  For Pushing Code in a Repository, use this command :
    
    ```powershell
    git push -u origin main      # It will push complete local code in remote repository
    ```
    

* * *

## Let\`s Learn Git Branch :

ham kafi dfa dekhte hain

*   git branch is main
    

but yay kia he ? technically branch ko pehle master bola jata tha but ab main bola jata he

Hamara commit hash id jo he vo ek track ke oper hota he and ham bi osi per work krtay hain one by one agay ki taraf jatay hain to osi linkdlist ko main kaha jata he

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769346523233/c4c42c98-34fd-4448-ba14-2f5629842633.png align="center")

ham main ke ilawa ek new branch bi bna sakta hain and uska kuch bi name rakh saktay hain .

new branch ki zroorat tab prti he jab hamne kisi website per work krna ho like

hmaray paas ek website he ali,ahmad and umar . yay 3 log iss website per work kr rhay hain and mil kr website build kr di he .

ab ali bolta he ke mojay ek new feature add krna he but agar vo osi branch mai work kray to agar code khraab hoa to whole website khraab ho sakti he . iss situation mai vo new branch ke HEAD (jo ke latest commit he waha se ) se alag se work kr sakta he.

### **2 commands se ham new branch bna saktay hain :**

```powershell
# Step 1: Branch banao
git branch feature/login

# Step 2: Uspar switch karo
git checkout feature/login
```

**Lekin easier yeh hai (1 command):**

```powershell
git checkout -b feature/login    # create + switched to new branch
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769348238330/7a442ab2-62e3-43ee-8112-9507170be12c.png align="center")

and jab hmara complete feature build ho jaiy test krkay to ham osay again main branch mai add kr saktay hain . main branch mai ek new commit bna kr usmay ham apna jo hmari feature branch ka latest and working commit he vo add krday gay.

* * *

### **New Branch Ka Data Main branch Mein Add Karna :**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769348499677/7d6fce94-f00e-4e0d-b657-98ac817a4778.png align="left")

**Command 1: Main branch par jao**

```powershell
git checkout main     # output : Switched to branch 'main'
```

**Command 2: Feature branch ka data add karo**

```powershell
# whole data main branch per with all commits add hogya  
git merge feature/a  

# Whole 2nd branch ka data , main branch per with only 1 commit add hoga 
git merge --squash feature/login
```

* * *

### **Normal Marge vs Squash Marge :**

*   Normal merge may feature branch ke **sab commits separately** main branch mein add hote hain and commit bi automatically kr deta he. (Don\`t use in Professional work)
    
*   **Squash merge** may feature branch ke **sab commits ek single commit** mein compress ho jate hai but commit automatically nai hota vo khud krna prta he. \*\*(\*\*Clean only 1 commit **Recomanded)**
    
*   but yay owerright bi kr deta he code ke chnges
    

![](https://messages-prod.27c852f3500f38c1e7786e2c9ff9e48f.r2.cloudflarestorage.com/019adaaf-080e-79e5-9938-0870d1df90e4/1769350745028-019bf584-fc1b-7b80-9e08-87347f65d5cb.png?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=af634fe044bd071ab4c5d356fdace60f%2F20260125%2Fauto%2Fs3%2Faws4_request&X-Amz-Date=20260125T141905Z&X-Amz-Expires=3600&X-Amz-Signature=332b05f6adbf09ca5bb378d90aa20379ede7d83d68b64c7dba8703d2fb8cca90&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject align="left")

* * *

### **Code Collaboration: Pushing Feature Branches to Remote Repository :**

Ab jab complete code update ho jaiy and new feature add kr lia jaiy to ose merge —squash krkay new commit bna kr commit -m krkay add krdia jaiy .

ab yay code apkay pc per offline hay , and apnay main branch mai sab add krdia he to ise GitHub per push kr day, lakin push krtay time kabhi bi directly main branch mai push nai kray (not good for professionals)

hamesha apni branch mai hi push kray like agar apnay feature/login name se branch bnai thi to osi branch per,

git push -u origin feature/login

krkay push krday iss trha se GitHub per uss repository ka owner apki pull request and code changes easily dekh kr request pull or cancel kr sakta he ( Always Recomanded for Professionals ).

agar unho ne commited kr di remot repository ke inner , to fir aap again

git pull origin main

try kro takay all new commits sync ho skay apki machine ke saath.

* * *

### **Rebase in Git :**

Just suppose ke aap feature branch par kaam kar rahe ho, lekin main branch ahead ho gaya (kisi ne naye commits add kiye).

*   Main:
    
    A → B → E
    
*   Feature: C → D
    

Conflict ho sakta hai kyunke main branch aage nikal gaya.

### **Step 1: Feature branch par ho**

*   git checkout feature/login
    

### **Step 2: Main ke latest changes par rebase karo**

```powershell
git rebase main
```

ab new yay hoga:

Main: A → B → E

Feature ke commits C, D ko E ke top par replay karega

Result: A → B → E → C → D

![Rewriting History With Git Rebase | Envato Tuts+](https://cms-assets.tutsplus.com/uploads/users/585/posts/23191/preview_image/preview.png align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769362789326/5cba49e0-aff3-4403-a636-a08599cba9e4.png align="center")

* * *

## All commands Cheatsheet :

### Basic / Local Git

```css
git init
git add .
git add <filename>
git commit -m "message"
git commit -am "message"
git status
git log
git log --oneline
git diff <old-commit> <new-commit>
git revert <commit-hash>
git reset --hard <commit-hash>
```

* * *

### 🔹 Git Internals / Verification

```css
ls -a
git branch
cat .git/HEAD
cat .git/refs/heads/main
ls .git/objects/
git cat-file -p <commit-hash>
```

* * *

### Remote / GitHub

```css
git clone <repo-url>
git clone https://user:TOKEN@github.com/user/repo
git remote remove <name>
git remote -v
git remote add origin <repo-url>
git remote set-url origin <repo-url>
git push -u origin main
git pull origin main
```

* * *

### Branching

```javascript
git branch feature/login
git checkout feature/login
git checkout -b feature/login
git checkout main      // switch branch

git merge master --allow-unrelated-histories 
// complete code ek branch se dosri branch mai laanay mai hep krta he like aap master branch se main branch mai switch krkay aa gaiy hain to yaya apply krday code main branch mai aa jaiy ga , last wala --allow-unrelated-histories  yay tab use kray jab iske bgair na ho code marge

git branch -d <branch-name> // it will delete branch
```

* * *

### Merging

```css
git merge feature/a
git merge --squash feature/login
```

* * *

### Collaboration (Professional Flow)

```css
git push -u origin feature/login
```

* * *

### Rebase

```css
git rebase main
```

* * *
