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 शामिल होते हैं और कौन-से नहीं।
-uoption से 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 clearuse करने से पहले आवश्यक 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 बहुत उपयोगी होता है।
सरल भाषा में:
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 को पहले एक साथ देखिए:
Rendering diagram…
Diagram could not be rendered.
Flow को step-by-step समझिए:
- आपके पास uncommitted changes हैं।
git stash pushउन्हें stash entry में save करता है।- working tree सामान्यतः clean state में लौट आता है।
- अब आप दूसरी branch या दूसरे task पर काम कर सकते हैं।
- वापस आने के बाद stash restore करते हैं।
applyकरने पर stash entry बची रहती है।popsuccessful होने पर stash entry remove हो जाती है।
यही git stash का मुख्य mental model है।
4. Stash करने से पहले current state check करें#
कोई भी change temporarily save करने से पहले repository की स्थिति देखना अच्छी habit है।
git statusमान लीजिए git status से आपको पता चलता है कि:
src/payment.jsmodified है;README.mdstaged है;notes.txtनई untracked file है।
अब आपको समझना होगा कि plain git stash push इनमें से किन changes को save करेगा।
5. git stash push से current tracked changes save करना#
Basic command है:
git stash pushयह command current tracked files के uncommitted changes को stash में save करता है।
इसमें सामान्यतः दोनों प्रकार के tracked changes शामिल हो सकते हैं:
- working tree में modified लेकिन unstaged changes;
- index में मौजूद staged changes।
उदाहरण के लिए:
git status
git stash push
git statusपहला git status आपको current changes दिखाता है।
git stash push changes को temporary stash में रखता है।
दूसरा git status आपको verify करने देता है कि working tree अपेक्षित clean state में पहुँचा या नहीं।
Stash बनाने के बाद सिर्फ command successful दिखने पर निर्भर न रहें। git status से repository state verify करना अच्छी Git habit है।
6. Staged changes का क्या होता है?#
यह beginner के लिए एक important point है।
मान लीजिए:
git add README.mdअब README.md staged है।
इसके बाद:
git stash pushकरने पर staged file का content भी stash में सुरक्षित किया जाता है।
लेकिन यहाँ एक subtle point है:
जब बाद में plain git stash apply या git stash pop से changes वापस लाते हैं, तो यह assume न करें कि हर file बिल्कुल उसी staged/unstaged स्थिति में लौटेगी जिसमें वह stash बनाते समय थी।
Restore करने के बाद हमेशा:
git statusचलाकर staging state verify करें।
जरूरत होने पर files को दोबारा stage करें।
इस chapter में मुख्य बात यह है:
staged content खोता नहीं है; वह stash में सुरक्षित हो सकता है।
7. Descriptive stash बनाना#
यदि आपके पास सिर्फ एक stash हो तो उसे पहचानना आसान है।
लेकिन real project में कई stashes हो सकते हैं।
इसलिए generic stash बनाने के बजाय meaningful message देना बेहतर है।
git stash push -m "payment validation work"यहाँ:
git stash pushनया stash बनाता है;-mmessage देता है;"payment validation work"आपको बाद में पहचानने में मदद करता है कि stash में कौन-सा काम रखा गया था।
एक और example:
git stash push -m "checkout UI before hotfix"Descriptive message future-you के लिए बहुत useful होता है।
कुछ दिनों बाद सिर्फ यह देखना:
WIP on feature-payment
से कम information मिल सकती है।
लेकिन:
checkout UI before hotfix
तुरंत context देता है।
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 बनाई:
touch notes.txtलेकिन उसे अभी:
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 करनी हों तो:
git stash push -uयहाँ -u का मतलब है:
--include-untracked
Descriptive message के साथ इसे combine कर सकते हैं:
git stash push -u -m "payment page work"अब current tracked changes के साथ untracked files भी stash में शामिल की जाती हैं।
यह real development में बहुत useful है।
उदाहरण के लिए आपने feature बनाते समय नई files create की हैं:
PaymentService.javapayment-test-data.json
यदि वे अभी untracked हैं और आपको अचानक branch बदलनी है, तो -u useful होगा।
नई file बनाई, plain git stash push चलाया और मान लिया कि सब कुछ stash हो गया। Untracked files default stash में शामिल नहीं होतीं। पहले git status देखें और जरूरत हो तो -u use करें।
11. Stash बनाने के बाद verify करना#
Safe workflow:
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 देखने के लिए:
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 workstash@{1}— cart work
है।
अब आपने नया stash बनाया।
तो नया stash stash@{0} बन जाएगा और पुराने references shift हो सकते हैं।
इसलिए blindly stash@{0} assume करने के बजाय multiple stashes होने पर पहले:
git stash listदेखना safer है।
Stash reference को permanent identity न समझें। नया stash बनने या entries remove होने पर numbering बदल सकती है।
14. Stash के अंदर क्या save है — git stash show#
किसी stash का summary देखने के लिए:
git stash show stash@{0}अगर latest stash inspect करना है, तो सामान्य रूप से:
git stash showभी use किया जा सकता है।
लेकिन जब multiple stashes हों, explicit reference अधिक clear होता है।
Detailed patch देखने के लिए:
git stash show -p stash@{0}यह आपको actual diff inspect करने में मदद करता है।
Safe habit:
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 करने के लिए:
git stash applyया explicit reference के साथ:
git stash apply stash@{0}apply का important behavior है:
changes restore होते हैं, लेकिन stash entry list में बनी रहती है।
यानी अगर apply successful हुआ, तब भी आप:
git stash listचलाएँगे तो stash entry अभी भी मिल सकती है।
यह तब useful है जब:
- आप पहले restore test करना चाहते हैं;
- stash को backup की तरह कुछ समय रखना चाहते हैं;
- same changes दूसरी suitable context में भी inspect या apply करना चाहते हैं।
16. Older stash restore करना#
मान लीजिए:
git stash listदेखने के बाद पता चला कि जरूरी work stash@{2} में है।
तो आप specific stash apply कर सकते हैं:
git stash apply stash@{2}इसलिए stash list देखना महत्वपूर्ण है।
सिर्फ latest stash ही restore करना आवश्यक नहीं है।
17. git stash pop — restore और successful होने पर remove#
अगर आपको पूरा confidence है कि stash को restore करके stash list से remove भी करना है, तो:
git stash popSpecific stash के लिए:
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:
अगर unsure हैं, पहले apply करना safer mental model है। Verify करने के बाद जरूरत हो तो stash manually drop कर सकते हैं।
19. apply use करने का safe pattern#
git stash list
git stash show stash@{0}
git stash apply stash@{0}
git statusअब changes check करें।
सब सही है और stash entry अब नहीं चाहिए, तो:
git stash drop stash@{0}इस workflow में आपको restore और deletion पर अलग-अलग control मिलता है।
20. pop use करने का workflow#
git stash list
git stash pop stash@{0}
git statusअगर apply cleanly successful हुआ, corresponding stash entry remove हो सकती है।
इसलिए pop के बाद भी:
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:
Rendering diagram…
Diagram could not be rendered.
Conflict का मतलब यह नहीं है कि stash corrupt हो गया।
इसका मतलब है कि Git को decide नहीं हो रहा कि overlapping changes को final file में कैसे combine किया जाए।
22. Conflict आने पर क्या करें?#
सबसे पहले:
git statusयह conflicted files दिखाएगा।
फिर affected file खोलें।
Git conflict markers दिखाई दे सकते हैं।
आपको correct final content manually decide करना होगा।
Conflict resolve करने के बाद file stage करें:
git add path/to/fileफिर:
git statusसे verify करें कि conflict resolved है।
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 के बाद:
git status
git stash listदोनों जरूर check करें।
Assumption के बजाय actual repository state verify करें।
24. Individual stash remove करना — git stash drop#
अगर कोई specific stash अब जरूरी नहीं है, उसे remove कर सकते हैं।
पहले list देखें:
git stash listफिर inspect करें:
git stash show stash@{1}और जब confirm हो जाए कि यही stash remove करना है:
git stash drop stash@{1}यह specific stash entry remove करता है।
drop use करने से पहले reference verify करना बहुत important है, क्योंकि stash numbering बदल सकती है।
Safe pattern:
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:
git stash clearrepository की सभी stash entries remove कर देता है।
यह individual stash remove करने जैसा नहीं है।
अगर आपके पास पाँच useful stashes हैं और आप clear चलाते हैं, तो सभी stash references हट सकते हैं।
इसलिए सिर्फ list साफ दिखाने के लिए इसे casually use करना dangerous है।
clear से पहले कम से कम:
git stash listदेखें।
Important stashes को inspect करें:
git stash show stash@{0}और तभी clear करें जब आपको सच में सभी stored stashes हटानी हों।
git stash clear bulk destructive operation है। Delete करने से पहले assume न करें कि कोई stash useless है। Verify करें।
26. Real-world workflow: urgent hotfix#
मान लीजिए आप feature branch पर हैं:
git statusआप देखते हैं कि tracked और untracked दोनों प्रकार की files current feature का हिस्सा हैं।
काम अभी complete नहीं है।
Descriptive stash बनाएँ:
git stash push -u -m "checkout validation before production hotfix"Verify करें:
git status
git stash listअब urgent branch पर जाएँ और काम पूरा करें।
जब original feature पर लौटें:
git stash list
git stash show stash@{0}सही stash confirm होने के बाद safer restore approach:
git stash apply stash@{0}
git statusकाम verify करें।
सब सही है तो:
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 चाहिए।
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:
git statusपहले चलाएँ।
2. Untracked files भी automatically stash होंगी ऐसा मानना#
Plain:
git stash pushuntracked files को default रूप से include नहीं करता।
जरूरत हो तो:
git stash push -uuse करें।
3. हर stash को stash@{0} समझना#
Newest stash सामान्यतः stash@{0} होता है, लेकिन नया stash बनने पर references shift होते हैं।
पहले:
git stash listदेखें।
4. apply और pop को same समझना#
दोनों restore कर सकते हैं, लेकिन apply stash को रखता है और successful pop उसे remove करता है।
5. Conflict आने के बाद stash तुरंत delete करना#
पहले:
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
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:
applykeeps the stash.popnormally removes it after a successful application.- If
popresults 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.
-uincludes untracked files.git statusshould 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 showwhen necessary. - Prefer targeted
git stash drop. - Treat
git stash clearas 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 stashincomplete uncommitted work को temporarily side में रखने के लिए है।git stash pushtracked staged और unstaged content को stash कर सकता है।- Untracked files default stash में शामिल नहीं होतीं।
- Untracked files के लिए
git stash push -uuse करें। - Meaningful message के लिए
git stash push -m "message"use करें। git stash listavailable stashes दिखाता है।stash@{0}newest stash को refer करता है, लेकिन numbering बदल सकती है।git stash showstash inspect करने में मदद करता है।git stash applychanges restore करता है और stash entry रखता है।git stash popchanges restore करता है और successful application के बाद stash remove करता है।- Conflict आने पर पहले
git statusऔरgit stash listसे actual state verify करें। - Specific stash हटाने के लिए
git stash dropuse करें। git stash clearसभी stashes remove कर सकता है, इसलिए इसे verification के बिना use न करें।- सबसे सुरक्षित habit है: inspect first, modify second, verify afterward।