Stashing ให้คุณ Save Code ไว้ภายหลังใน Git Repository
เวลาทำงานกับ Git Repository คุณอาจทำ Changes ไปที่ File ที่ต้องการ Apply เข้า Git Commit ภายหลัง
นี่คือที่มาของคำสั่ง git stash Stashing ให้คุณ Save Code บน Working Branch ไว้วันที่ภายหลัง ในบทช่วยสอนนี้ เราจะอภิปรายพร้อมตัวอย่าง Basics of Stashing ใน Git และวิธีใช้ git stash command
Git Stash คืออะไร?
Stashing ให้คุณ Save Code ใน Working Directory และ Index ไว้ภายหลัง บางครั้งเวลาเขียนโค้ด คุณจะอยาก Save Changes ที่ทำ แต่ไม่ Commit เข้า Repository
Scenario หนึ่งที่อาจเกิด: Bug Report เข้ามา คุณกำลัง Implement Feature อยู่ คุณอาจอยาก Save Changes ที่ทำไปสำหรับ Feature ก่อน แล้วค่อย Fix Bug
นี่เพราะ Bug Report น่าจะ Priority สูงกว่า Feature ที่พัฒนา คุณอยาก Fix Bug ก่อน ก่อน Commit Feature-focused Code เข้า Repository คุณไม่อยากให้ Bug Fix และ Feature อยู่ใน Commit เดียวกัน เพราะ Commits ที่ใหญ่และซับซ้อนทำให้ Developers อื่นยากต่ออ่าน History ของ Repository
กล่าวคือ Stashing ให้คุณทำงานอย่างอื่นใน Git Repository โดยไม่ต้อง Discard หรือ Commit Existing Code
Stashing ให้คุณ Save หรือ "Stash" การเปลี่ยนแปลงที่ทำไปที่ File ไว้ภายหลัง เมื่อ File ถูก Stashed แล้ว คุณทำงานอย่างอื่นได้ แล้วกลับมา Apply Changes ที่ Stashed ไว้เข้า Code ได้
Stashing Revert Current Working Directory ไป Last Commit หมายความว่า Stashing Code ให้เรา Clean Working Directory ที่ทำงานได้ Clean Directory หมายความว่าเราทำ Changes ได้โดยไม่กังวล Merge Conflicts เรายังไม่ต้องคิด Sort Changes ระหว่าง Commits
วิธี Stash Changes ใน Git
คำสั่ง git stash ใช้ Stash Code ไว้ภายหลัง เวลารัน git stash การเปลี่ยนแปลงที่ยังไม่ Push เข้า Commit ใน Current Working Directory จะ Saved ไว้ภายหลัง รวม Staged Changes (Changes ที่ Add เข้า Staging Area ด้วย git add) และ Unstaged Changes
Syntax สำหรับ git stash command:
git stash
สมมติเราทำงาน Website และ Code เก็บใน Git Repository
เราทำ Changes ไปที่ index.html และ index.js Files ใน Code เราต้องการ Stash ไฟล์เหล่านี้ไว้ภายหลังขณะ Fix Design Bug ใน index.html
เพื่อ View Changes เราใช้ git status command ในตัวอย่างคืนค่า:
On branch master Your branch is up to date with 'origin/master'. Changes to be committed: new file: index.js Changes not staged for commit: modified: index.html
นี่แสดงเราสร้าง New File (index.js) และ Modify Existing File (index.html) เราต้องการ Save Changes เหล่านี้ไว้ภายหลังขณะทำงานส่วนอื่นของ Codebase ทำได้ด้วย git stash command:
git stash
โค้ดคืนค่า:
Saved working directory and index state WIP on master: 3b16026 feat: Launch new homepage
Git stash command บันทึก Changes ที่เราทำไป Repository ไว้ภายหลัง ตอนนี้รัน git status อีกครั้ง จะเห็น Nothing to Commit:
On branch master Your branch is up to date with 'origin/master'. nothing to commit, working tree clean
Git stash command บันทึก Changes ที่เราทำไป Repository ไว้ภายหลัง ตอนนี้รัน git status อีกครั้ง จะเห็น Nothing to Commit:
เวลา Code Stashed Repository ของคุณ Revert ไป Previous Commit โดยไม่มี Changes ที่คุณทำ Changes ที่ทำไป Code ถูก Saved ไว้ภายหลัง สามารถ Apply เข้า Codebase ได้เวลาพร้อมด้วย Stash Pop Command
Stashing Untracked Changes
คำสั่ง git stash จะ Stash Staged และ Unstaged Changes ของ Files ที่ Tracked อยู่ใน Git Repository แล้ว โดย Default Stash Command ไม่ Include Untracked Changes
Staged Changes คือ Changes ที่ Added เข้า Staging Area Unstaged Changes ที่ Stashed คือ Changes ที่ทำไป Files ที่ Git Track หากคุณเปลี่ยน New File ที่ไม่ Tracked โดย Git จะไม่ Added เข้า Git Stash Ignored Files จะไม่ Added เข้า Stash
นี่คือ Default Preferences เมื่อใช้ git stash command หากต้องการ Stash Untracked Files — เช่น New File ที่ยังไม่ Staged — ใช้ Flag -u Syntax:
git stash -u
ทางเลือก หากต้องการ Include Ignored Files ใน Stash ใช้ Flag -a Syntax:
git stash -a
Flag -a บอก git stash ให้ Stash Changes ไป Ignored Files นี่คือ Files ที่ Define ใน .gitignore file ใน Code Repository (หากมี)
Apply Stashed Changes ด้วย git stash pop
คำสั่ง git stash pop ใช้ Apply Stashed Changes เข้า Repository สมมติเราตัดสินใจ Apply Stashed Changes ที่ทำในตัวอย่างก่อนหน้า ทำได้ด้วย:
git stash pop
Git stash pop command คืนค่า:
On branch master Your branch is up to date with 'origin/master'. Changes to be committed: new file: index.js Changes not staged for commit: modified: index.html Dropped refs/stash@{0} (48afd55381cf43f2332f771349c7233fb99f80a6)
เวลารัน git stash pop command Changes จาก Stash ถูก Apply เข้า Local Working Copy ของ Repository Pop Command Apply Code ใน Stash เข้า Repository แล้ว Stash ว่าง
Git stash pop command ยังคืน List ของ Changes ที่ทำไป Code แสดงว่ามีอะไรใน Stash
คุณสามารถใช้ git stash apply Apply Changes ที่ทำไป Code คุณ Keep Changes เหล่านั้นใน Stash เรา Keep Changes ใน Stash ได้ด้วย:
git stash apply
คำสั่งคืนค่า:
On branch master Your branch is up to date with 'origin/master'. Changes to be committed: new file: index.js Changes not staged for commit: modified: index.html
Applying Changes Across Multiple Branches
Apply Stashed Changes เดียวกันไปหลาย Branches ใน Codebase เป็น Use Case ที่ดีของ Stash Command เช่น คุณอาจ Decide Apply Stash ไป Branch หนึ่ง แล้ว Move ไป Branch อื่น และใช้ Stash เดียวกัน Apply Saved Changes
Multiple Stashes
คุณสามารถสร้าง Multiple Stashes ใน Git นี่หมายความว่าคุณทำ Changes ไป File หนึ่ง Stash ไว้ภายหลัง ทำ Changes ไปอีก File เก็บใน Stash อื่นแยกกัน
Tracking Stashes
เพื่อ Return List ของ Stashes ใน Repository รันคำสั่ง:
git stash list
สมมติเราอยากดู List Stashes ทั้งหมดที่สร้าง ทำได้โดย Execute คำสั่งด้านบน คำสั่งคืนค่า:
stash@{0}: WIP on master: 3b16026 feat: Launch new homepage stash@{1}: WIP on master: 3b16026 feat: Launch new homepage
Git stash list command คืน List ของ Stashes ของเรา
แต Stashes นี้ใช้แค่ Default Commit Messages ที่เชื่อมโยงกับ Stash นี่หมายความว่ายากต่อ Track ว่า Stash ไหน Include Changes ไหน เพราะเราไม่ Specify Description บน Stashes ใดๆ
ลองเพิ่ม Description ให้ Stash ใหม่ ทำได้ด้วย:
git stash save "add new change to website"
คำสั่งคืนค่า:
Saved working directory and index state On master: add new change to website
แทนที่จะได้ Default Message ที่เชื่อมโยงกับ Last Commit Stash ของเราถูก Assign Message ของตัวเอง
Applying Multiple Stashes
คำสั่ง git stash pop Apply Most Recent Stash ไป Repository (Tag stash@{0}) แต่วक्तทำงาน Multiple Stashes คุณอาจอยาก Apply Specific Stash ไป Codebase
ทำได้โดยระบุ Unique ID ที่เชื่อมโยงกับ Stash ที่ต้องการ Apply สมมติเราอยาก Apply Stash ที่ stash@{1} ไป Codebase ทำได้ด้วย:
git stash pop stash@{2}
คำสั่ง Apply Changes ที่เก็บใน stash@{2} เข้า Repository และลบ Stash
Creating a Branch Using a Stash
Stashing เป็นวิธี Convenient Store Changes ที่ทำไป Codebase ขณะทำงาน Change อื่น
แต่บางครั้ง แทนที่จะ Maintain Stash คุณอยาก Move Code เข้า Branch ของตัวเอง
Scenario นี้อาจเกิดขึ้นหากมี Conflicts เมื่อ Merge Code คุณอาจอยาก Move Code เข้า Branch อื่นหากทำ Larger Changes ไป Codebase นี่คือที่มาของ git stash branch command
คุณสามารถใช้ git stash branch สร้าง Branch ใหม่ที่ Changes ใน Stash จะ Applied Syntax:
git stash branch <new-branch> <stash-id>
Parameter "new-branch" หมายถึง ID ของ Branch ที่ควรสร้าง "stash-id" หมายถึง ID ของ Stash ที่ Code ต้องการ Apply ไป New Branch
สมมติเราอยาก Apply Changes จาก stash@{2} ไป Branch ใหม่ชื่อ "update-site" ทำได้ด้วย:
git stash branch update-site stash@{2}
คำสั่งคืนค่า:
Switched to a new branch 'update-site' On branch update-site Changes to be committed: new file: index.js Changes not staged for commit: modified: index.html Dropped stash@{2} (9a15b9cd20f8988937134d1267fafbea4c6a8647)
ก่อน git stash branch command สร้าง Branch ใหม่สำหรับ Stashed Changes Changes ที่ทำไป Repository Applied ไป New Branch แล้วเรา Switched ไป View Branch นั้น สุดท้าย Stash ถูกลบเพราะ Stashed Code Saved ใน Branch ของตัวเองแล้ว
Delete a Git Stash
เมื่อใช้ Code ใน Stash แล้ว คุณใช้ git stash drop ลบ Stash Syntax:
git stash drop <stash>
สมมติเราอยาก Drop Stash ID stash@{2} ทำได้ด้วย:
git stash drop stash@{2}
คำสั่งคืนค่า:
Dropped stash@{2} (82079798c950b053fac0efb7b1d5693864dc96e7)
ทางเลือก คุณใช้ git stash clear ลบ Stashes ทั้งหมดที่เชื่อมโยงกับ Repository Syntax:
git stash clear
คำสั่งนี้ลบ Stashes ทั้งหมดใน Repository ของเรา
Stashing Specific Files
คำสั่ง git stash Stash Tracked Files ทั้งหมดใน Current Working Directory โดย Default แตบาง Cases คุณอาจ Decide ว่าต้องการ Stash Specific File หรือ Set of Files
คุณ Stash Specific File ได้ด้วย git stash push Syntax:
git stash push -m "<message>" <file>
Parameter "message" หมายถึง Message ที่จะเชื่อมโยงกับ Stash "file" Parameter คือชื่อ File ที่ต้องการ Stash
สมมติเราอยาก Add แค่ Changes ที่ทำไป index.html file ใน Code เข้า Stash ทำได้ด้วย:
git stash push -m "feat: changed index.html file" index.html
คำสั่งคืนค่า:
Saved working directory and index state On master: feat: changed index.html file
คำสั่งเพิ่มแค่ index.html file เข้า Stash ด้วย Commit Message "feat: changed index.html file" Changes อื่นๆ ที่ทำไป Code ไม่ได้ Added
Show Differences Between Stashes
เวลาทำงาน Stashes คุณน่าจะทำ Changes หลายอย่างไป Codebase ข้าม Multiple Commits ก่อนกลับมาที่ Code ที่ Stashed
เช่น หาก Stash Code แล้วไป Fix Bug คุณอาจ Push Commits หลายอัน Fix Bug ก่อนกลับมาที่ Stashed Code นี่หมายความว่าคุณอาจอยากเห็น Summary ของ Differences ระหว่าง Stash และ Most Recent Commit ของ Code (เพื่อรู้ Changes ที่ทำนับจาก Stash)
เพื่อ View Differences ระหว่าง Stash และ Most Recent Commit ใช้ git stash show Syntax:
git stash show <stash-id>
Parameter "stash-id" คือ ID ของ Stash ที่ Changes ต้องการ Compare กับ Most Recent Commit ของ Branch สมมติเราอยาก Compare Code ใน stash@{1} กับ Current State ของ Code ทำได้ด้วย:
git stash show stash@{1}
คำสั่งคืนค่า:
index.html | 2 +- index.js | 0 2 files changed, 1 insertion(+), 1 deletion(-)
Output บอกเราทำ 2 Changes ไป Code: 1 Insertion และ 1 Deletion หากอยาก View Differences ระหว่าง Files ใช้ Flag -p stash Syntax:
git stash show -p stash@{1}
คำสั่งด้านบน Execute แล้วคืนค่า:
diff --git a/index.html b/index.html index 4dd1ef7..e859c68 100644 --- a/index.html +++ b/index.html @@ -1 +1 @@ - +