KKAIROS
دليل عمليمثال توضيحيزمن القراءة: 7 دقائق

Git Stash: ضع التعديلات غير المحفوظة جانبا

أوقف العمل مؤقتًا وارجع بشجرة نظيفة: يحفظ git stash تعديلات الملفات المتتبَّعة في مكدّس، ويعيد git stash pop أحدث إدخال ثم يحذفه من المكدّس.

تاريخ النشر:

كل أمر أدناه هو الأمر الذي نُفّذ فعلا، وكل كتلة ناتج منسوخة من السجل المسجَّل. معرّفات الاختبارات وبصمات السجلات وحالات الخروج في جدول الأدلة.

TE-59-A: يُنظّف stash شجرة العمل المتتبَّعة ويستعيد pop التعديل

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txt

الأمر:

git stash >/dev/null; echo clean=$(git status --porcelain | wc -l); echo list_before=$(git stash list | wc -l); git stash pop >/dev/null; echo list_after=$(git stash list | wc -l); echo restored=$(git status --porcelain | wc -l); cat f.txt

الناتج المسجَّل:

clean=0
list_before=1
list_after=0
restored=1
one
two

حالة الخروج: 0. يطابق الناتج النص المتوقع الذي كُتب قبل التشغيل.

TE-59-B: يحفظ git stash push -m التعديل برسالة يعرضها git stash list

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txt

الأمر:

git stash push -q -m wip; git stash list

الناتج المسجَّل:

stash@{0}: On main: wip

حالة الخروج: 0. تطابق هذه الحالة والناتج ما كُتب قبل التشغيل.

TE-59-C: خروج غير صفري متوقع: ينتهي git stash pop بحالة الخروج 1 عندما تكون قائمة stash فارغة

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one

الأمر:

git stash pop

الناتج المسجَّل: لا شيء، كان الخرج القياسي فارغا.

الخطأ القياسي من التشغيل نفسه:

No stash entries found.

حالة الخروج: 1. تطابق هذه الحالة والناتج ما كُتب قبل التشغيل.

TE-59-B: حسب الإعداد، في f.txt تعديل غير مجهَّز للـ commit. حفظه الأمر git stash push -m wip، وعرض git stash list إدخالا واحدا هو stash@{0}: On main: wip. TE-59-C: حسب الإعداد، لم يُحفظ شيء في stash. انتهى git stash pop بحالة الخروج 1 وطبع No stash entries found. إلى الخطأ القياسي.

git stash pop: التعارضات وإدخال محدد وapply

ماذا يفعل git stash pop عند حدوث تعارض؟

يبلّغ عن التعارض وينتهي بحالة الخروج 1 ويُبقي الإدخال في قائمة stash (TE-308-A). وهذا ما يقوله دليل git-stash: إذا فشل تطبيق الحالة بسبب تعارضات فلا يُحذف الإدخال من قائمة stash، وتحل التعارضات يدويا ثم تنفّذ git stash drop بنفسك.

كيف أنفّذ pop لإدخال أقدم لا الأحدث؟

أعط git stash pop اسم الإدخال من git stash list، مثل stash@{1}. يبقى الإدخال الآخر في القائمة (TE-308-B).

ما الفرق بين pop وapply؟

يعيد git stash apply التعديلات ويُبقي الإدخال. ويحذفه git stash drop. أما pop فيفعل الأمرين في خطوة واحدة (TE-308-C، وTE-59-A أعلاه لـ pop وحده).

TE-308-A: خروج غير صفري متوقع: ينتهي git stash pop مع تعارض بحالة الخروج 1 ويُبقي إدخال stash

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' > f.txt; git stash -q
printf 'three\n' > f.txt; git commit -qam three

الأمر:

git stash pop >../out.txt 2>../err.txt; rc=$?
grep -h -E '^(CONFLICT|The stash entry)' ../out.txt ../err.txt
echo list_after=$(git stash list | wc -l)
git status -s
exit $rc

الناتج المسجَّل:

CONFLICT (content): Merge conflict in f.txt
The stash entry is kept in case you need it again.
list_after=1
UU f.txt

حالة الخروج: 1. هذه هي الحالة المسجَّلة في سجل TE-308-A.

TE-308-B: يطبّق git stash pop مع اسم stash ذلك الإدخال ويترك الإدخال الآخر في القائمة

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'a\n' > f.txt; git stash push -q -m first
printf 'b\n' > f.txt; git stash push -q -m second

الأمر:

git stash list
git stash pop -q 'stash@{1}'; echo pop_exit=$?
cat f.txt
git stash list

الناتج المسجَّل:

stash@{0}: On main: second
stash@{1}: On main: first
pop_exit=0
a
stash@{0}: On main: second

حالة الخروج: 0. هذه هي الحالة المسجَّلة في سجل TE-308-B.

TE-308-C: يُبقي git stash apply الإدخال، ويحذفه git stash drop

التحضير لهذا التشغيل، داخل مجلد مؤقت (mktemp -d):

git init -q .
git symbolic-ref HEAD refs/heads/main
git config user.email test@example.invalid
git config user.name tester
git config commit.gpgsign false
printf 'one\n' > f.txt; git add f.txt; git commit -qm one
printf 'two\n' >> f.txt; git stash -q

الأمر:

git stash apply -q; echo list_after_apply=$(git stash list | wc -l) file_lines=$(wc -l < f.txt)
git stash drop -q; echo list_after_drop=$(git stash list | wc -l)

الناتج المسجَّل:

list_after_apply=1 file_lines=2
list_after_drop=0

حالة الخروج: 0. هذه هي الحالة المسجَّلة في سجل TE-308-C.

TE-308-A: حسب الإعداد، حُفظ f.txt في stash بالمحتوى two، ثم غيّر commit جديد الملف نفسه إلى three. طبع git stash pop العبارة CONFLICT (content): Merge conflict in f.txt والعبارة The stash entry is kept in case you need it again. بقي في القائمة إدخال واحد، وأظهر git status -s السطر UU f.txt. TE-308-B: حسب الإعداد، يوجد إدخالان هما first (الأقدم) وsecond (الأحدث). انتهى git stash pop 'stash@{1}' بحالة الخروج 0، وقرأ f.txt القيمة a، ولم يبقَ في القائمة سوى stash@{0}: On main: second. TE-308-C: ترك git stash apply إدخالا واحدا في القائمة وكان في f.txt سطران. ثم ترك git stash drop صفر إدخالات.

الحدود

يغطي TE-59-A وTE-59-B تعديلا غير مجهَّز على ملف متتبَّع، ويغطي TE-59-C حالة stash فارغ. لم تُختبر الملفات غير المتتبَّعة ولا التعديلات المجهَّزة. يغطي TE-308-A تعارضا عند pop، ويطبّق TE-308-B إدخالا محددا بـ pop، ويشغّل TE-308-C كلا من apply وdrop مرة واحدة. غير مختبر في هذه الصفحة: الأوامر الفرعية show وbranch وclear، والخيار --index مع pop، وخيارات push: -p أو --patch، و-k أو --keep-index، و-u أو --include-untracked، و-a أو --all، و--pathspec-from-file مع --pathspec-file-nul. يعرضها نص الاستخدام الذي يطبعه git stash -h في git 2.34.1. لم يستخدمها أي أمر git stash هنا.

Source

Evidence table

TestWhat it checksExitVerdictLog sha256 (first 12)
TE-59-Aيُنظّف stash شجرة العمل المتتبَّعة ويستعيد pop التعديل0PASS77a379f0397b
TE-59-Bيحفظ git stash push -m التعديل برسالة يعرضها git stash list0PASS6cf540b5aec0
TE-59-Cخروج غير صفري متوقع: ينتهي git stash pop بحالة الخروج 1 عندما تكون قائمة stash فارغة1PASSb014757852b8
TE-308-Aخروج غير صفري متوقع: ينتهي git stash pop مع تعارض بحالة الخروج 1 ويُبقي إدخال stash1PASSba6d136d12fe
TE-308-Bيطبّق git stash pop مع اسم stash ذلك الإدخال ويترك الإدخال الآخر في القائمة0PASScb5d80e63cfd
TE-308-Cيُبقي git stash apply الإدخال، ويحذفه git stash drop0PASS7697a6182f87

سُجّل على: bash 5.1.16(1)-release؛ git version 2.34.1؛ Python 3.10.12. اللغة C.UTF-8، المنطقة الزمنية UTC. السجلات والبصمات هي نفسها سجلات الصفحة الإنجليزية git-stash-full-en، فهذه الصفحة ترجمة نثر فقط ولم يُجرَ لها تشغيل منفصل. لكل صف سجل مسجَّل فيه أوامر الإعداد والأمر والخرج القياسي والخطأ القياسي وحالة الخروج. أول 12 حرفا من sha256 للسجل في الجدول.

كل الأدلة الكاملة: كل الأدلة