Printing is unavailable

This course is licensed for personal online viewing only.

Public preview • CL-P000000-C41-L1629-CFCE1180

Home Dashboard
Chapter 211 · Module 5 — Practical Change and History Management

Temporarily Saving Work with git stash

4,303
words
24
min read
2
tools

1. Learning Outcomes#

इस chapter को पूरा करने के बाद आप:

  • समझ पाएँगे कि git stash की जरूरत कब पड़ती है।
  • बिना commit किए हुए current changes को temporarily सुरक्षित रख पाएँगे।
  • git stash push से tracked working-tree और staged changes save कर पाएँगे।
  • descriptive message के साथ stash बना पाएँगे।
  • समझ पाएँगे कि default stash में कौन-से changes शामिल होते हैं और कौन-से नहीं।
  • -u option से untracked files को भी stash में शामिल कर पाएँगे।
  • git stash list और git stash show से saved stashes inspect कर पाएँगे।
  • stash@{0} जैसे stash references समझ पाएँगे।
  • git stash apply और git stash pop के बीच सही चुनाव कर पाएँगे।
  • stash restore करते समय conflict आने पर उसे safely handle कर पाएँगे।
  • किसी specific stash को git stash drop से remove कर पाएँगे।
  • git stash clear use करने से पहले आवश्यक safety checks कर पाएँगे।

2. जब काम अधूरा हो, लेकिन branch बदलनी पड़े#

मान लीजिए आप feature-payment branch पर काम कर रहे हैं।

आपने कुछ files modify की हैं। कुछ changes staged भी हैं। लेकिन काम अभी complete नहीं है, इसलिए commit करना उचित नहीं लग रहा।

उसी समय production में urgent bug आ जाता है।

आपको तुरंत दूसरी branch पर जाकर bug fix करना है।

अब समस्या यह है:

  • current work अभी अधूरा है;
  • आप उसे खोना नहीं चाहते;
  • सिर्फ branch बदलने के लिए अधूरा commit भी नहीं बनाना चाहते।

ऐसी situation में git stash बहुत उपयोगी होता है।

सरल भाषा में:

Key Idea

git stash आपके current uncommitted changes को temporarily side में सुरक्षित रखता है, ताकि working directory फिर से clean हो सके और आप किसी दूसरे काम पर जा सकें।

इसे एक temporary locker की तरह समझिए।

आप अपना अधूरा काम locker में रखते हैं, जरूरी दूसरा काम करते हैं, फिर वापस आकर locker से अपना काम निकाल लेते हैं।

लेकिन एक बात याद रखें: stash permanent project history का replacement नहीं है। Long-term meaningful work के लिए proper commits ही सही तरीका हैं।

3. git stash का basic workflow#

पूरे workflow को पहले एक साथ देखिए:

Visual Diagram80%

Rendering diagram…

Flow को step-by-step समझिए:

  1. आपके पास uncommitted changes हैं।
  2. git stash push उन्हें stash entry में save करता है।
  3. working tree सामान्यतः clean state में लौट आता है।
  4. अब आप दूसरी branch या दूसरे task पर काम कर सकते हैं।
  5. वापस आने के बाद stash restore करते हैं।
  6. apply करने पर stash entry बची रहती है।
  7. pop successful होने पर stash entry remove हो जाती है।

यही git stash का मुख्य mental model है।

4. Stash करने से पहले current state check करें#

कोई भी change temporarily save करने से पहले repository की स्थिति देखना अच्छी habit है।

BASH
git status

मान लीजिए git status से आपको पता चलता है कि:

  • src/payment.js modified है;
  • README.md staged है;
  • notes.txt नई untracked file है।

अब आपको समझना होगा कि plain git stash push इनमें से किन changes को save करेगा।

5. git stash push से current tracked changes save करना#

Basic command है:

BASH
git stash push

यह command current tracked files के uncommitted changes को stash में save करता है।

इसमें सामान्यतः दोनों प्रकार के tracked changes शामिल हो सकते हैं:

  • working tree में modified लेकिन unstaged changes;
  • index में मौजूद staged changes।

उदाहरण के लिए:

BASH
git status
git stash push
git status

पहला git status आपको current changes दिखाता है।

git stash push changes को temporary stash में रखता है।

दूसरा git status आपको verify करने देता है कि working tree अपेक्षित clean state में पहुँचा या नहीं।

Remember

Stash बनाने के बाद सिर्फ command successful दिखने पर निर्भर न रहें। git status से repository state verify करना अच्छी Git habit है।

6. Staged changes का क्या होता है?#

यह beginner के लिए एक important point है।

मान लीजिए:

BASH
git add README.md

अब README.md staged है।

इसके बाद:

BASH
git stash push

करने पर staged file का content भी stash में सुरक्षित किया जाता है।

लेकिन यहाँ एक subtle point है:

जब बाद में plain git stash apply या git stash pop से changes वापस लाते हैं, तो यह assume न करें कि हर file बिल्कुल उसी staged/unstaged स्थिति में लौटेगी जिसमें वह stash बनाते समय थी।

Restore करने के बाद हमेशा:

BASH
git status

चलाकर staging state verify करें।

जरूरत होने पर files को दोबारा stage करें।

इस chapter में मुख्य बात यह है:

staged content खोता नहीं है; वह stash में सुरक्षित हो सकता है।

7. Descriptive stash बनाना#

यदि आपके पास सिर्फ एक stash हो तो उसे पहचानना आसान है।

लेकिन real project में कई stashes हो सकते हैं।

इसलिए generic stash बनाने के बजाय meaningful message देना बेहतर है।

BASH
git stash push -m "payment validation work"

यहाँ:

  • git stash push नया stash बनाता है;
  • -m message देता है;
  • "payment validation work" आपको बाद में पहचानने में मदद करता है कि stash में कौन-सा काम रखा गया था।

एक और example:

BASH
git stash push -m "checkout UI before hotfix"

Descriptive message future-you के लिए बहुत useful होता है।

कुछ दिनों बाद सिर्फ यह देखना:

WIP on feature-payment

से कम information मिल सकती है।

लेकिन:

checkout UI before hotfix

तुरंत context देता है।

Tip

Stash message ऐसा रखें जिससे आपको यह पता चले कि आपने कौन-सा अधूरा काम temporarily रखा था।

8. Default stash में कौन-से changes शामिल होते हैं?#

यह distinction बहुत important है।

सामान्य git stash push मुख्य रूप से tracked files के current changes को stash करता है।

मान लीजिए repository में यह स्थिति है:

File State Plain git stash push
app.js tracked + modified Included
README.md tracked + staged Included
notes.txt untracked Not included by default

इसलिए अगर आपने कोई नई file बनाई है जिसे Git अभी track नहीं कर रहा, तो plain stash के बाद वह file working directory में रह सकती है।

9. Untracked file क्या है?#

अगर आपने नई file बनाई:

BASH
touch notes.txt

लेकिन उसे अभी:

BASH
git add notes.txt

नहीं किया, तो Git के लिए वह file untracked है।

git status में वह सामान्यतः untracked files के section में दिखाई देती है।

यदि वह file भी आपके temporary work का हिस्सा है, तो plain stash पर्याप्त नहीं होगा।

10. Untracked files को भी stash करना#

Tracked changes के साथ untracked files भी save करनी हों तो:

BASH
git stash push -u

यहाँ -u का मतलब है:

--include-untracked

Descriptive message के साथ इसे combine कर सकते हैं:

BASH
git stash push -u -m "payment page work"

अब current tracked changes के साथ untracked files भी stash में शामिल की जाती हैं।

यह real development में बहुत useful है।

उदाहरण के लिए आपने feature बनाते समय नई files create की हैं:

  • PaymentService.java
  • payment-test-data.json

यदि वे अभी untracked हैं और आपको अचानक branch बदलनी है, तो -u useful होगा।

Common Mistake

नई file बनाई, plain git stash push चलाया और मान लिया कि सब कुछ stash हो गया। Untracked files default stash में शामिल नहीं होतीं। पहले git status देखें और जरूरत हो तो -u use करें।

11. Stash बनाने के बाद verify करना#

Safe workflow:

BASH
git status
git stash push -u -m "payment validation work"
git status
git stash list

इस sequence का purpose समझिए।

पहला git status:

current state समझने के लिए।

git stash push ...:

work temporarily save करने के लिए।

दूसरा git status:

working tree verify करने के लिए।

git stash list:

यह confirm करने के लिए कि stash entry मौजूद है।

12. Saved stashes देखना — git stash list#

Repository के सभी available stashes देखने के लिए:

BASH
git stash list

आपको entries इस तरह की दिखाई दे सकती हैं:

stash@{0}: On feature-payment: payment validation work

stash@{1}: On feature-cart: cart quantity update

stash@{2}: On main: temporary README update

यहाँ हर stash का एक reference है।

उदाहरण:

  • stash@{0}
  • stash@{1}
  • stash@{2}

इन references की मदद से आप specific stash inspect, apply या remove कर सकते हैं।

13. stash@{0} का मतलब क्या है?#

stash@{0} सबसे recent stash को refer करता है।

एक simple stack जैसा mental model रखें:

Reference सामान्य अर्थ
stash@{0} newest stash
stash@{1} उससे पुराना stash
stash@{2} उससे भी पुराना stash

मान लीजिए आपके पास पहले:

  • stash@{0} — payment work
  • stash@{1} — cart work

है।

अब आपने नया stash बनाया।

तो नया stash stash@{0} बन जाएगा और पुराने references shift हो सकते हैं।

इसलिए blindly stash@{0} assume करने के बजाय multiple stashes होने पर पहले:

BASH
git stash list

देखना safer है।

Warning

Stash reference को permanent identity न समझें। नया stash बनने या entries remove होने पर numbering बदल सकती है।

14. Stash के अंदर क्या save है — git stash show#

किसी stash का summary देखने के लिए:

BASH
git stash show stash@{0}

अगर latest stash inspect करना है, तो सामान्य रूप से:

BASH
git stash show

भी use किया जा सकता है।

लेकिन जब multiple stashes हों, explicit reference अधिक clear होता है।

Detailed patch देखने के लिए:

BASH
git stash show -p stash@{0}

यह आपको actual diff inspect करने में मदद करता है।

Safe habit:

BASH
git stash list
git stash show stash@{0}
git stash show -p stash@{0}

पहले पहचानें।

फिर inspect करें।

उसके बाद restore या delete करने का decision लें।

15. git stash apply — work restore करें, stash को रखें#

मान लीजिए आपने अपना urgent काम पूरा कर लिया और अब original feature पर वापस आ गए।

Current latest stash restore करने के लिए:

BASH
git stash apply

या explicit reference के साथ:

BASH
git stash apply stash@{0}

apply का important behavior है:

changes restore होते हैं, लेकिन stash entry list में बनी रहती है।

यानी अगर apply successful हुआ, तब भी आप:

BASH
git stash list

चलाएँगे तो stash entry अभी भी मिल सकती है।

यह तब useful है जब:

  • आप पहले restore test करना चाहते हैं;
  • stash को backup की तरह कुछ समय रखना चाहते हैं;
  • same changes दूसरी suitable context में भी inspect या apply करना चाहते हैं।

16. Older stash restore करना#

मान लीजिए:

BASH
git stash list

देखने के बाद पता चला कि जरूरी work stash@{2} में है।

तो आप specific stash apply कर सकते हैं:

BASH
git stash apply stash@{2}

इसलिए stash list देखना महत्वपूर्ण है।

सिर्फ latest stash ही restore करना आवश्यक नहीं है।

17. git stash pop — restore और successful होने पर remove#

अगर आपको पूरा confidence है कि stash को restore करके stash list से remove भी करना है, तो:

BASH
git stash pop

Specific stash के लिए:

BASH
git stash pop stash@{0}

pop को mentally ऐसे समझिए:

apply + successful होने पर stash removal

लेकिन इसे हमेशा literal two-command shortcut समझना पर्याप्त नहीं है, क्योंकि conflict case में behavior महत्वपूर्ण हो जाता है।

18. git stash apply और git stash pop का फर्क#

Behavior git stash apply git stash pop
Changes restore करता है Yes Yes
Successful restore के बाद stash रखता है Yes No
Restore test करने के लिए safer choice अक्सर Yes कम suitable
Stash entry तुरंत consume करनी हो Manual drop needed Useful
Conflict आने पर stash recovery Stash remains Stash is normally kept when pop cannot cleanly apply

Simple rule:

Key Idea

अगर unsure हैं, पहले apply करना safer mental model है। Verify करने के बाद जरूरत हो तो stash manually drop कर सकते हैं।

19. apply use करने का safe pattern#

BASH
git stash list
git stash show stash@{0}
git stash apply stash@{0}
git status

अब changes check करें।

सब सही है और stash entry अब नहीं चाहिए, तो:

BASH
git stash drop stash@{0}

इस workflow में आपको restore और deletion पर अलग-अलग control मिलता है।

20. pop use करने का workflow#

BASH
git stash list
git stash pop stash@{0}
git status

अगर apply cleanly successful हुआ, corresponding stash entry remove हो सकती है।

इसलिए pop के बाद भी:

BASH
git stash list

देखना useful है।

इससे आप actual stash state verify कर लेते हैं।

21. Stash restore करते समय conflict क्यों आ सकता है?#

Stash कोई जादुई time machine नहीं है।

मान लीजिए आपने file का पुराना version stash किया।

इसके बाद उसी file में branch पर दूसरे changes आ गए।

अब stash restore करने पर Git को दोनों versions automatically combine करने में difficulty हो सकती है।

तब conflict आ सकता है।

Visual flow:

Visual Diagram80%

Rendering diagram…

Conflict का मतलब यह नहीं है कि stash corrupt हो गया।

इसका मतलब है कि Git को decide नहीं हो रहा कि overlapping changes को final file में कैसे combine किया जाए।

22. Conflict आने पर क्या करें?#

सबसे पहले:

BASH
git status

यह conflicted files दिखाएगा।

फिर affected file खोलें।

Git conflict markers दिखाई दे सकते हैं।

आपको correct final content manually decide करना होगा।

Conflict resolve करने के बाद file stage करें:

BASH
git add path/to/file

फिर:

BASH
git status

से verify करें कि conflict resolved है।

Remember

Conflict में जल्दबाजी से stash delete न करें। पहले working files और stash list दोनों verify करें।

23. git stash pop में conflict आए तो क्या होगा?#

यह एक बहुत important practical point है।

git stash pop का intention changes restore करना और stash remove करना है।

लेकिन अगर changes cleanly apply नहीं हो पाते और conflict आता है, तो Git stash entry को सामान्यतः automatically drop नहीं करता।

इससे आपको conflict resolve करने का मौका मिलता है और stash copy भी उपलब्ध रह सकती है।

Conflict के बाद:

BASH
git status
git stash list

दोनों जरूर check करें।

Assumption के बजाय actual repository state verify करें।

24. Individual stash remove करना — git stash drop#

अगर कोई specific stash अब जरूरी नहीं है, उसे remove कर सकते हैं।

पहले list देखें:

BASH
git stash list

फिर inspect करें:

BASH
git stash show stash@{1}

और जब confirm हो जाए कि यही stash remove करना है:

BASH
git stash drop stash@{1}

यह specific stash entry remove करता है।

drop use करने से पहले reference verify करना बहुत important है, क्योंकि stash numbering बदल सकती है।

Safe pattern:

BASH
git stash list
git stash show stash@{1}
git stash drop stash@{1}
git stash list

पहली list — target identify करने के लिए।

show — content verify करने के लिए।

drop — remove करने के लिए।

दूसरी list — final state verify करने के लिए।

25. git stash clear के साथ extra सावधानी#

यह command:

BASH
git stash clear

repository की सभी stash entries remove कर देता है।

यह individual stash remove करने जैसा नहीं है।

अगर आपके पास पाँच useful stashes हैं और आप clear चलाते हैं, तो सभी stash references हट सकते हैं।

इसलिए सिर्फ list साफ दिखाने के लिए इसे casually use करना dangerous है।

clear से पहले कम से कम:

BASH
git stash list

देखें।

Important stashes को inspect करें:

BASH
git stash show stash@{0}

और तभी clear करें जब आपको सच में सभी stored stashes हटानी हों।

Warning

git stash clear bulk destructive operation है। Delete करने से पहले assume न करें कि कोई stash useless है। Verify करें।

26. Real-world workflow: urgent hotfix#

मान लीजिए आप feature branch पर हैं:

BASH
git status

आप देखते हैं कि tracked और untracked दोनों प्रकार की files current feature का हिस्सा हैं।

काम अभी complete नहीं है।

Descriptive stash बनाएँ:

BASH
git stash push -u -m "checkout validation before production hotfix"

Verify करें:

BASH
git status
git stash list

अब urgent branch पर जाएँ और काम पूरा करें।

जब original feature पर लौटें:

BASH
git stash list
git stash show stash@{0}

सही stash confirm होने के बाद safer restore approach:

BASH
git stash apply stash@{0}
git status

काम verify करें।

सब सही है तो:

BASH
git stash drop stash@{0}

यही professional habit है:

save → verify → switch → return → inspect → restore → verify → clean up

27. क्या stash remote repository पर चला जाता है?#

नहीं।

Stash सामान्यतः आपकी local Git repository से जुड़ी temporary state होती है।

git push करने से आपकी stash list team members को automatically नहीं मिलती।

इसलिए stash को team collaboration mechanism न समझें।

अगर work को team के साथ share करना है, तो meaningful commit और suitable branch workflow चाहिए।

Common Mistake

Stash को backup या remote sharing system समझना गलत है। यह मुख्य रूप से local temporary work management के लिए है।

28. Stash कब useful है?#

git stash useful है जब:

  • आपको अचानक दूसरी branch पर जाना है;
  • current changes अभी commit के लिए ready नहीं हैं;
  • quick interruption आया है;
  • आप experimental changes temporarily side में रखना चाहते हैं;
  • clean working tree की जरूरत है लेकिन current work खोना नहीं चाहते।

29. Stash कब overuse नहीं करना चाहिए?#

अगर आपके पास बहुत सारे पुरानी तारीख के stashes जमा होते जा रहे हैं, तो यह संकेत हो सकता है कि आप temporary storage को long-term work management की तरह use कर रहे हैं।

Stash message useful है, लेकिन proper commit history से ज्यादा meaningful नहीं।

इसलिए stash को temporary shelf की तरह use करें, permanent cupboard की तरह नहीं।

30. Common beginner mistakes#

1. बिना git status देखे stash करना#

आपको पता ही नहीं रहता कि repository में कौन-सी files tracked, staged या untracked हैं।

Better:

BASH
git status

पहले चलाएँ।

2. Untracked files भी automatically stash होंगी ऐसा मानना#

Plain:

BASH
git stash push

untracked files को default रूप से include नहीं करता।

जरूरत हो तो:

BASH
git stash push -u

use करें।

3. हर stash को stash@{0} समझना#

Newest stash सामान्यतः stash@{0} होता है, लेकिन नया stash बनने पर references shift होते हैं।

पहले:

BASH
git stash list

देखें।

4. apply और pop को same समझना#

दोनों restore कर सकते हैं, लेकिन apply stash को रखता है और successful pop उसे remove करता है।

5. Conflict आने के बाद stash तुरंत delete करना#

पहले:

BASH
git status
git stash list

से state समझें।

6. git stash clear casually चलाना#

यह सभी stash entries remove कर सकता है।

Use करने से पहले हर important stash verify करें।

31. Quick self-check#

आपके पास modified tracked file और एक नई untracked file है। दोनों को temporarily stash करना है। कौन-सा command suitable है?Show Answer
BASH
git stash push -u

-u untracked files को भी stash operation में include करता है।


आप changes restore करना चाहते हैं लेकिन stash entry अभी delete नहीं करना चाहते। apply या pop में से क्या चुनेंगे?Show Answer

git stash apply।

यह changes restore करता है लेकिन stash entry को सामान्यतः list में रखता है।


stash@{0} हमेशा उसी पुराने stash को refer करता रहेगा क्या?Show Answer

नहीं।

stash@{0} current newest stash reference है। नया stash बनने या stash entries remove होने पर numbering बदल सकती है। इसलिए multiple stashes होने पर git stash list से reference verify करें।

32. Interview Preparation#

What is the purpose of git stash?Show Answer

git stash temporarily stores uncommitted changes so that a developer can obtain a cleaner working tree, switch context, and later restore those changes without creating an unnecessary work-in-progress commit.

Key Points:

  • Temporarily stores uncommitted work.
  • Useful during context switching.
  • Primarily local to the repository.
  • It is not a replacement for meaningful commits.

Why interviewer asks this:

The interviewer wants to know whether you can manage interrupted development work safely without creating unnecessary commits.


What is the difference between git stash apply and git stash pop?Show Answer

git stash apply reapplies the selected stash while keeping the stash entry. git stash pop reapplies the stash and, when the application succeeds, removes that stash entry.

Key Points:

  • apply keeps the stash.
  • pop normally removes it after a successful application.
  • If pop results in conflicts, the stash is generally kept instead of being dropped.

Why interviewer asks this:

This checks whether you understand both recovery safety and stash lifecycle management.


Are untracked files included in git stash by default?Show Answer

No. A normal git stash push does not include untracked files by default. Use git stash push -u or git stash push --include-untracked when those files also need to be stored.

Key Points:

  • Default stash focuses on tracked-file changes.
  • -u includes untracked files.
  • git status should be checked before stashing.

Why interviewer asks this:

This is a common practical mistake that can leave important new files outside the stash.


How would you safely delete an old stash?Show Answer

First inspect the stash list, identify the correct reference, review the stash if necessary, and then remove only that entry using git stash drop stash@{n}. Avoid using git stash clear unless all stashes are intentionally being removed.

Key Points:

  • Check git stash list.
  • Verify the correct stash reference.
  • Use git stash show when necessary.
  • Prefer targeted git stash drop.
  • Treat git stash clear as a bulk destructive operation.

Why interviewer asks this:

The interviewer wants to see whether you use destructive Git commands carefully.

33. Chapter Recap#

इस chapter से सबसे important बातें:

  • git stash incomplete uncommitted work को temporarily side में रखने के लिए है।
  • git stash push tracked staged और unstaged content को stash कर सकता है।
  • Untracked files default stash में शामिल नहीं होतीं।
  • Untracked files के लिए git stash push -u use करें।
  • Meaningful message के लिए git stash push -m "message" use करें।
  • git stash list available stashes दिखाता है।
  • stash@{0} newest stash को refer करता है, लेकिन numbering बदल सकती है।
  • git stash show stash inspect करने में मदद करता है।
  • git stash apply changes restore करता है और stash entry रखता है।
  • git stash pop changes restore करता है और successful application के बाद stash remove करता है।
  • Conflict आने पर पहले git status और git stash list से actual state verify करें।
  • Specific stash हटाने के लिए git stash drop use करें।
  • git stash clear सभी stashes remove कर सकता है, इसलिए इसे verification के बिना use न करें।
  • सबसे सुरक्षित habit है: inspect first, modify second, verify afterward।
Interactive learning

Practice lab

Apply this chapter with guided activities, immediate feedback, explanations, and retry.

Practice loads when this section enters view.