Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม

Git Revert Commit: คู่มือทีละขั้นตอน

คำสั่ง git revert จะยกเลิก commit เพื่อคืน repository ไป commit ก่อนหน้า แทนที่จะลบ commit revert จะสร้าง commit ใหม่ที่ย้อนกลับการเปลี่ยนแปลงของ published commit ทำให้ commit แรกยังคงเป็นส่วนของประวัติโปรเจกต์


เมื่อทำงานกับ Git repository อาจสร้าง commit ด้วยโค้ดที่ยังไม่พร้อมเก็บใน repository นั่นคือที่มาของคำสั่ง git revert คำสั่งนี้ให้ undo commit เพื่อคืน repository ไป commit ก่อนหน้า

บทช่วยสอนนี้อธิบายการ revert โค้ด และการใช้ git revert command พร้อมตัวอย่าง จนจบคุณจะเชี่ยวชาญ git revert commit

การ Revert โค้ด (Reverting Your Code)

บางครั้งทำงานกับ Git repository อาจสังเกตว่าทำผิดพลาดใน commit

เช่น สมมติเพิ่งเสร็จ feature ใหม่ แต่พบบั๊กต้องแก้ เมื่อ commit bug fix พบว่า commit working directory ของ feature ใหม่ด้วย ทำให้ commit มีทั้ง bug fix และ feature ใหม่ สับสน collaborators แทนที่จะแยกเป็น 2 commits

ในสถานการณ์นี้ อาจอยาก revert repository กลับสู่สถานะก่อน push commit ให้โอกาส push commits ใหม่โดย revert last commit

คำสั่ง Git Revert

คำสั่ง git revert ให้ undo commit แต่แทนที่จะลบ commit ออกจากโปรเจกต์ git revert จะหาความแตกต่างระหว่าง 2 commits ล่าสุด และสร้าง commit ใหม่ที่ revert การเปลี่ยนแปลงเหล่านั้น

git revert มีประโยชน์เพรองรักษาประวัติโปรเจกต์ของ repository แทนที่จะลบ commit ทั้งหมด ให้ revert ไป commit อื่น ทำให้ยังมี record ครบถ้วนของทุก commit ที่ push

ควรใช้ git revert เมื่ออยาก reverse last commit แทนที่จะแก้ไข last commit เอง ให้ revert commit และ push commit ใหม่ไป codebase

ไวยากรณ์:

git revert

ตัวอย่างการทำงาน:

ตัวอย่าง Git Revert

Local machine มี example repository 1 ไฟล์: README.md เพิ่งแก้ไขไฟล์ อยาก push เข้า commit:

git commit -m "docs: Update README.md with author info"

คำสั่งสร้าง commit message docs: Update README.md with author info แก้ไข README.md อีกครั้ง commit อีกครั้ง:

git commit -m "docs: Update order of authors by contributions"

สร้าง commit อีกอัน ใช้ git log --pretty=oneline ดู commits สรุป:

6f52d877873e7d3b52c929647384dfdf2488da22 (HEAD -> master) docs: Update order of authors by contributions
57d763663e619088159bb7629243456f88feab79 docs: Update README.md with author info

ตอนนี้อยาก revert last commit พบว่าลำดับ authors ใน README ผิด ต้อง revert กลับสู่ก่อน push last commit

ใช้คำสั่ง:

git revert HEAD

รันคำสั่ง text editor เปิดให้พิมพ์ message ใหม่ ในคำสั่ง HEAD หมายถึง latest commit ตัวอย่างพิมพ์ message revert author order commit ผลลัพธ์:

[master b66c29a] revert author order commit "docs: Update order of authors by contributions" 1 file changed, 1 deletion(-)

รัน git log --pretty=oneline อีกครั้ง:

b66c29a8c4c226fa9ae8cd8f9e086c0a73e6ecfe (HEAD -> master) revert author order commit "docs: Update order of authors by contributions"
6f52d877873e7d3b52c929647384dfdf2488da22 docs: Update order of authors by contributions
57d763663e619088159bb7629243456f88feab79 docs: Update README.md with author info

ตอนนี้ commit history มี 3 commits แทนที่จะลบ commit ล่าสุด git revert สร้าง commit ใหม่ที่ undo การเปลี่ยนแปลงของ previous commit ทำให้ยังมี complete history ของทุก commits

git revert ต้องการ commit reference ในตัวอย่างระบุ HEAD (revert ไป last commit) หากอยาก revert ไป commit อื่น ระบุ hash value จาก git log แทน HEAD ตัวอย่าง revert ไป commit แรก:

git revert 57d763663e619088159bb7629243456f88feab79

คำสั่ง revert repository ไป commit ที่มี SHA hash นั้น

Git Revert vs. Reset

Revert และ reset ใช้ undo การเปลี่ยนแปลงจาก commits ก่อนหน้า แต่พฤติกรรมต่างกันเล็กน้อย

git revert undo previous commit รัน git revert จะ undo การเปลี่ยนแปลงระหว่าง 2 commits แล้วสร้าง commit ใหม่ที่มีโค้ดหลัง undo

git reset revert repository กลับสู่ previous state โดยลบ commits ทั้งหมดในช่วงที่ระบุ หาก run git reset อ้างอิง previous commit commits ระหว่าง current state และ commit นั้นจะถูกลบ

Revert commit มัก preferred over reset เพราะ git revert ไม่เปลี่ยนประวัติโปรเจกต์ ให้นักพัฒนารักษา record การเปลี่ยนแปลงได้ถูกต้อง

นอกจากนี้ git revert revert ไป individual commit ณ จุดใดในประวัติได้ git reset ทำงานย้อนกลับจาก commit ปัจจุบันเท่านั้น หมายถึง หากอยาก undo commit เก่าในประวัติ ต้องลบ commits ก่อนหน้าทั้งหมด แล้ว commit ใหม่ ทำให้ยากที่จะรักษาประวัติถูกต้อง ซึ่งสำคัญสำหรับ source control tools อย่าง Git

สรุป

คำสั่ง git revert ให้ undo การเปลี่ยนแปลงที่ทำไปใน code repository ตั้งแต่ commit เฉพาะ แทนที่จะลบ commit git revert ระบุการเปลี่ยนแปลงระหว่าง current commit และ previous commit และสร้าง commit ใหม่ revert การเปลี่ยนแปลงเหล่านั้น

บทช่วยสอนนี้อธิบายการใช้ git revert command revert commit ใน Git ตอนนี้คุณพร้อม revert commits ด้วย git revert อย่างมืออาชีพแล้ว!