Skip to content

Git Stage2: Staging Area

Ab jab aapko lage ki aapne kaam kar liye ab isko save kar du, To save karne se pahle aap use add karege staging area me, Ye bilkul aisa hai eit 🧱 paathne wale logo ne mitti ko saanche(working dir) me le ke eit bana diya aur usko ab ek jaga pe rakh diya ki isko thodi der baad me finalize karke (for ex count karke) sukhne ke liye rakh dunga (commit kar dunga)

aba agar aap bagal me rakhe hue eit ko (code ko) le ke thoda change karna chahte hai to kar sakte hai. iske liye aap jaise hi change karenge wo automatically phir se aapke saanche me (working dir) me aa jaayega

Add

Code ko working dir se staging area bhejne ki procedure ko add kahte hai

What happend when we do git add

For example this is out file

File1.txt

Hello this is my first file.

STEP0: Byte count

Is file me last me ek new line hai to byte count karte samay usko bhi count karna hoga

Hello(5)
space(1)
this(4)
space(1)
is(2)
space(1)
my(2)
space(1)
first(5)
space(1)
file.(5)
newline(1)
-----------------
TOTAL = 30 bytes

STEP1: Git file read karta hai

Git OS se bolta hai: mujhe file1.txt ke raw bytes de do

OS git to de deta hai:

Hello this is my first file.\n

STEP2: Blob format me convert karta hai

Blob ka ek format hota hai waisa formate me got is raw bytes ko convert kar deta hai

blob 30\0Hello this is my first file.\n

Breakdown:

  • blob → object type
  • 30 → content size (bytes)
  • \0 → NULL separator
  • baaki → file content

STEP3: SHA-1 calculate hota hai

Git ab exact isi byte sequence(yani jo blob banaya) ka SHA-1 nikalta hai:

SHA1("blob 30\0Hello this is my first file.\n")

Result:

Ye maine nikala git bhi aise hi sha1 nikalta hai

[x@archlinux Git-Learn]$ echo -n "blob 30\0Hello this is my first file.\n:" | sha1sum
9f25664bea1f5d6f300e182fb18981e6c977e79a  -
  • file name se independent
  • location se independent
  • sirf content identity hai

STEP4: Blob Object compress hota hai

Git ab:

  1. poore blob data ko jo STEP2 me bani thi (blob 30\0Hello this is my first file.\n)
  2. zlib compression se compress karta hai

STEP5: Ab Compressed data .git/objects/ me store hota hai

Ab 2 problame hai:

  1. Store kis dir me hoga
  2. File ka naam kya hoga

Solution:

  1. Ye to confirm hai ki ye /git/objects me store hoga lekin /objects ke andar kaha?
  2. To /objects ke andar aur bhi dir banti hai
  3. Ye dir name aur file name sha1 se calculate hota hai

dir name -> sha1 ka first 2 char
filename -> sha1 ka baki ka 38 digit

Structure:

For Ex: hamare File1.txt ka sha11 ye hai to uska dir kaise banega
SHA1: 9f25664bea1f5d6f300e182fb18981e6c977e79a
File name: .git/objects/9f/25664bea1f5d6f300e182fb18981e6c977e79a

Default Dir - .git/objects/
Another dir using first 2 char of sha1: 9f/
file name for blob using rest 38 char: 25664bea1f5d6f300e182fb18981e6c977e79a

STEP6: Index (staging area) update hota hai

Git ka index (.git/index) ek binary file hota hai. Har staged file ke liye Git ek index entry add karta hai. Ek index entry me ye sab hota hai:

  1. file path
  2. blob SHA-1
  3. file mode
  4. stage number
  5. ctime / mtime (timestamps)
  6. file size
  7. inode / device info (filesystem tracking ke liye)

How to see this index?

you can see the index content by below command, below command just give imp details (like timestams and size are not part of content identity uske bine bhi commit ho sakta hai to wo sab cheeje git nahi dikhata) not all details which is in index

[x@archlinux Git-Learn]$ git ls-files --stage
100644 9f25664bea1f5d6f300e182fb18981e6c977e79a 0       first_file.txt
  • File mode / permissions: 100644
  • Blob SHA-1: 9f25664bea1f5d6f300e182fb18981e6c977e79a
  • 0: Stage numberc
  • first_file.txt: File path Working directory me ye file ka naam/path

If two file content is same then there will be diffrent entry in index

here both file have same content still their entry is diffrent but object me ek hi blobm store hoga

[x@archlinux Git-Learn]$ git ls-files --stage
100644 5d21a70f33d0b2d9a3615da618c7ccc9c6ebaccb 0       first_file.txt
100644 5d21a70f33d0b2d9a3615da618c7ccc9c6ebaccb 0       sec.txt

Important

Scenario: maine ek file banayi aur usko stage me add kar diya to ab phir uska blob banega aur wo object me store hoga, ab mai us file ko modify karta hu to aur phir se stage me add karta hu to uska naya blob store hoga aur uska pahle waha index me entry replace ho jaayegi naye wale sha1 se. magar uska blob abhi bhi obj me hoga.

Scenario: ab maine abhi commit nahi kiya bas add kar ke modify kiya aur save karke fir se add kar diya ab mujhe us purane wale file ko wapas lana hai to kaise aa sakta hu kyoki uska blob to abhi bhi pada hai

Ham chahe to us purane blob se purane file ka content read kar sakte hai ya kisi file me save kar sakte hai

#us blob ka content sirf print karne ke liye
git show <SHA-1>

[x@archlinux Git-Learn]$ git show deb79c25c53e11ba83a539136f1b1af18ba25727
Hello this is my first file, i am writing it using cat > Command.

#us blob ka content kisi file me save karne ke liye
git cat-file -p <SHA-1>

[x@archlinux Git-Learn]$ git cat-file -p deb79c25c53e11ba83a539136f1b1af18ba25727
Hello this is my first file, i am writing it using cat > Command.

# Blob ke content ko fxile.txt me save  karega
[x@archlinux Git-Learn]$ git cat-file -p deb79c25c53e11ba83a539136f1b1af18ba25727 > x.txt
[x@archlinux Git-Learn]$