🌐 Detecting your location…
📢 Advertisement — Configure AdSense in Appearance → Customize → AdSense Settings

Git Stash: الدليل الكامل – الحفظ والتطبيق والبوب والإفلات والتفرع من Stash

⏱️4 min read  ·  686 words

git stashيحفظ تغييرات دليل العمل الخاص بك مؤقتًا حتى تتمكن من تبديل السياق دون ارتكاب عمل غير مكتمل. إنها واحدة من أوامر Git الأكثر فائدة ولكنها غير مستغلة بشكل كافٍ. يغطي هذا الدليل كل أمر فرعي مع أمثلة عملية.

المفهوم الأساسي

# You're in the middle of feature work and need to switch branches
git status
# M  src/auth.ts    ← modified
# M  src/utils.ts   ← modified
# ?? src/new-file.ts  ← untracked

git checkout main  # ERROR: would overwrite changes

# git stash saves and cleans your working directory
git stash
# Saved working directory and index state WIP on feature: abc123 Add auth

git checkout main  # ✅ now clean working tree

# ... do what you needed on main ...

git checkout feature
git stash pop      # restore your changes

أوامر git stash

# Save stash with a message (recommended — harder to forget what it was)
git stash push -m "WIP: auth middleware refactor"

# Short form (saves tracked changes only)
git stash

# Include untracked files
git stash push --include-untracked   # or -u

# Include untracked AND ignored files (rare)
git stash push --all                 # or -a

# Partial stash — choose specific files
git stash push -m "just auth changes" -- src/auth.ts src/auth.test.ts

سرد وفحص المخابئ

# List all stashes
git stash list
# stash@{0}: WIP on feature: abc123 Add auth
# stash@{1}: WIP on main: def456 Fix bug
# stash@{2}: On feature: Manual save before experiment

# Show what's in a stash (summary)
git stash show            # most recent
git stash show stash@{1} # specific stash

# Show full diff of a stash
git stash show -p stash@{0}

الاستعادة: البوب مقابل التطبيق

# git stash pop — apply AND remove from stash list
git stash pop              # applies stash@{0} and removes it
git stash pop stash@{2}   # apply a specific stash

# git stash apply — apply but KEEP in stash list
git stash apply            # applies stash@{0}, keeps it in list
git stash apply stash@{1}

# When to use apply:
# - When you want to apply the same stash to multiple branches
# - When you're not sure the apply will be clean and want to retry
# - As a safety measure — you can always drop manually after

تعارضات عند تطبيق المخبأ

# If the stash conflicts with current branch state:
git stash pop
# CONFLICT (content): Merge conflict in src/auth.ts
# The stash entry is NOT removed when conflicts occur

# Fix conflicts normally
git add src/auth.ts
git stash drop    # manually remove the stash after resolving
# (pop would have removed it automatically if no conflicts)

التفرع من مخبأ

# Create a new branch FROM the commit where the stash was saved
# and apply the stash to it — useful when stash won't apply cleanly to current branch
git stash branch my-new-branch stash@{0}

# This is equivalent to:
# git checkout -b my-new-branch stash-parent-commit
# git stash apply stash@{0}
# git stash drop stash@{0}

إزالة المخابئ

# Remove specific stash
git stash drop stash@{0}

# Remove most recent stash
git stash drop

# Remove ALL stashes (dangerous — no undo)
git stash clear

خبأ جزئي مع الوضع التفاعلي

# Interactively choose which hunks to stash (like git add -p)
git stash push --patch    # or -p

# Shows each changed hunk and asks: stash this hunk? (y/n/d/a/e/?)
# Useful when a file has changes you want to stash and changes you want to keep

سير عمل المطور المشترك

# Workflow 1: Quick context switch
git stash push -m "WIP: new feature"
git checkout main
git pull origin main
# ... fix urgent bug ...
git checkout feature
git stash pop

# Workflow 2: Stash before risky rebase
git stash push -u -m "Before rebase backup"
git rebase origin/main
# If rebase goes wrong: git stash pop restores your state
# (though git reflog is better for full recovery)

# Workflow 3: Test clean build
git stash push -u          # stash everything including untracked
npm run build              # verify clean build works
git stash pop              # restore your work

# Workflow 4: Apply stash to different branch
git stash push -m "shared config changes"
git checkout feature-a
git stash apply stash@{0}  # apply (keep in list)
git checkout feature-b
git stash apply stash@{0}  # apply to second branch too
git stash drop stash@{0}   # now drop it

git stash vs git Commit –amend vs git Worktree

السيناريو أفضل أداة
تبديل السياق السريع (نفس الجهاز، 5-30 دقيقة) git stash
العمل على فرعين في وقت واحد git worktree add
حفظ WIP مع إمكانية الدفع إلى جهاز التحكم عن بعد git commit -m "WIP" (الإصلاح لاحقًا)
التراجع عن الالتزام الأخير مع الاحتفاظ بالتغييرات git reset HEAD~1

الأسئلة المتداولة

س: هل يعمل git stash عبر الفروع؟
ج: نعم. Stash عبارة عن مكدس عالمي – وليس خاصًا بفرع معين. يمكنك تخزين العناصر في الفرع “أ”، والانتقال إلى الفرع “ب”، ووضع المخبأ هناك. إذا كانت الملفات تتعارض مع الفرع B، فسوف تحصل على تعارض دمج لحله.

س: هل سيقوم git stash بحفظ الملفات التي لم يتم تعقبها؟
ج: ليس افتراضيا. استخدمgit stash push -u (أو--include-untracked) لتضمين الملفات الجديدة التي لم يتم تعقبها. استخدمgit stash push -a (أو--all) لتضمين الملفات التي لم يتم تعقبها والتي تم تجاهلها.

س: لقد أسقطت مخبأتي عن طريق الخطأ – هل يمكنني استعادته؟
ج: ربما. تشغيلgit fsck --no-reflog | awk '/dangling commit/ {print $3}' للعثور على الالتزامات المتدلية. ثمgit show <hash> لتحديد محتوى مخبأتك، وgit stash apply <hash> لاستعادته. يعمل خلال 30 يومًا تقريبًا قبل تجميع البيانات المهملة في Git.

س: كم عدد المخابئ التي يمكنني الحصول عليها؟
ج: لا يوجد حد عملي. ولكن إذا كان لديك أكثر من 10 مخازن، ففكر في الالتزام بالعمل قيد التقدم أو استخدام git Worktree بدلاً من ذلك – فقائمة المخازن الطويلة هي علامة على القرارات المؤجلة.

س: هل يمكنني تخزين التغييرات المرحلية فقط؟
ج: نعم:git stash push --staged يخزن فقط ما هو موجود في الفهرس (منظم)، مع ترك التغييرات غير المرحلية في دليل العمل. مفيد لفصل التغييرات إلى وحدات منطقية أصغر.

الخلاصة

git stash يعد أمرًا ضروريًا للتبديل النظيف للسياق دون ارتكاب عمل غير مكتمل. الأوامر الرئيسية:git stash push -u -m "description" للحفظ (بما في ذلك غير المتعقبة)،git stash pop لاستعادة وإزالة،git stash apply لاستعادة والحفاظ عليها. استخدمgit stash branch عندما لا يتم تطبيق المخبأ بشكل نظيف على فرعك الحالي. المخزن عبارة عن مكدس LIFO – حافظ عليه بشكل نشط، وقم بإسقاط المخابئ التي لم تعد بحاجة إليها، ولا تدعها تتراكم أكثر من 3-4 إدخالات.

✍️ Leave a Comment

Your email address will not be published. Required fields are marked *

🌐 Read in:🇩🇪 Deutsch🇧🇷 Português🇸🇦 العربية🇮🇳 हिन्दी🇧🇩 বাংলা