Skip to content

Commit 9a5c973

Browse files
docs(git): add comprehensive guide on git stash
New tutorial covering stash basics, commands, techniques, and best practices.
1 parent 0fc621d commit 9a5c973

File tree

1 file changed

+158
-0
lines changed

1 file changed

+158
-0
lines changed

tutorial/vcs/git_stash.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: Mastering Git Stash
3+
layout: default
4+
parent: Git
5+
grand_parent: Version Control System
6+
description: "Mastering Git Stash"
7+
---
8+
9+
# Mastering Git Stash: A Comprehensive Guide
10+
11+
## Table of Contents
12+
13+
- [Introduction to Git Stash](#introduction-to-git-stash)
14+
- [What is Git Stash?](#what-is-git-stash)
15+
- [Basic Git Stash Commands](#basic-git-stash-commands)
16+
- [Advanced Git Stash Techniques](#advanced-git-stash-techniques)
17+
- [Real-World Use Cases](#real-world-use-cases)
18+
- [Best Practices](#best-practices)
19+
- [Common Pitfalls and Solutions](#common-pitfalls-and-solutions)
20+
21+
## Introduction to Git Stash
22+
23+
Git stash is a powerful feature that allows developers to temporarily shelve changes they're not ready to commit. It's
24+
like a magical clipboard for your code modifications, enabling you to switch contexts quickly without losing your
25+
work-in-progress.
26+
27+
## What is Git Stash?
28+
29+
Git stash is essentially a temporary storage mechanism that lets you:
30+
31+
- Save current changes without committing them
32+
- Clean your working directory
33+
- Switch branches or work on different tasks
34+
- Retrieve your saved changes later
35+
36+
Think of it as a quick way to "park" your current modifications without creating an incomplete or messy commit.
37+
38+
### How Git Stash Works Internally
39+
40+
When you use `git stash`, Git:
41+
42+
- Creates a temporary commit in your local Git history
43+
- Reverts your working directory to the last committed state
44+
- Stores the changes in a stack-like structure
45+
- Allows you to retrieve these changes later
46+
47+
## Basic Git Stash Commands
48+
49+
### Stashing Changes
50+
51+
```bash
52+
# Basic stash command
53+
git stash
54+
55+
# Stash with a descriptive message
56+
git stash save "Work in progress: feature X"
57+
58+
# Stash including untracked files
59+
git stash -u
60+
```
61+
62+
### Retrieving Stashed Changes
63+
64+
```bash
65+
# Apply the most recent stash
66+
git stash apply
67+
68+
# Pop the most recent stash (apply and remove)
69+
git stash pop
70+
71+
# Apply a specific stash
72+
git stash apply stash@{2}
73+
```
74+
75+
### Managing Stash List
76+
77+
```bash
78+
# List all stashes
79+
git stash list
80+
81+
# Show contents of a specific stash
82+
git stash show stash@{1}
83+
84+
# Delete a specific stash
85+
git stash drop stash@{1}
86+
87+
# Clear all stashes
88+
git stash clear
89+
```
90+
91+
## Advanced Git Stash Techniques
92+
93+
### Partial Stashing
94+
95+
```bash
96+
# Interactively choose which changes to stash
97+
git stash save -p "Partial stash"
98+
```
99+
100+
### Stashing Selected Files
101+
102+
```bash
103+
# Stash only specific files
104+
git stash push path/to/file1 path/to/file2
105+
```
106+
107+
## Real-World Use Cases
108+
109+
### Scenario 1: Unexpected Urgent Task
110+
111+
Imagine you're working on a feature branch and suddenly need to fix a critical bug on the main branch:
112+
113+
1. Your current branch has uncommitted changes
114+
2. You can't commit these half-finished changes
115+
3. Use `git stash` to save your work
116+
4. Switch to main branch
117+
5. Fix the urgent bug
118+
6. Return to your feature branch
119+
7. Use `git stash pop` to continue your work
120+
121+
### Scenario 2: Collaborative Development
122+
123+
During pair programming or team collaboration:
124+
125+
- Quickly save experimental changes
126+
- Share your stashed work with teammates
127+
- Retrieve work across different development environments
128+
129+
## Best Practices
130+
131+
- Use descriptive messages when stashing
132+
- Regularly review and clean up your stash list
133+
- Don't use stash as a long-term code storage mechanism
134+
- Remember that stashes are local to your repository
135+
136+
## Common Pitfalls and Solutions
137+
138+
### Stash Conflicts
139+
140+
When applying a stash, you might encounter merge conflicts. Resolve these manually by:
141+
142+
- Examining the conflict markers
143+
- Editing the files to reconcile differences
144+
- Adding the resolved files
145+
- Continuing the stash application
146+
147+
### Large Stash Accumulation
148+
149+
Prevent stash clutter by:
150+
151+
- Regularly applying or dropping stashes
152+
- Using meaningful stash messages
153+
- Implementing a personal stash management routine
154+
155+
## Conclusion
156+
157+
Git stash is a versatile tool that offers developers flexibility and efficiency in managing code changes. By
158+
understanding its nuances, you can significantly improve your workflow and coding productivity.

0 commit comments

Comments
 (0)