Gitกวดวิชา


Gitและ {{title}}


Git Contribute


Git Advanced


Git Undo




Git Revert


Git Revert

revertเป็นคำสั่งที่เราใช้เมื่อเราต้องการใช้คำสั่งก่อนหน้าcommitและเพิ่มเป็นคำสั่งใหม่commitโดยคงไว้ซึ่งความlogครบถ้วนสมบูรณ์

ขั้นตอนที่ 1: ค้นหาก่อนหน้าcommit:

Git Revert ขั้นตอนที่ 1

ขั้นตอนที่ 2: ใช้เพื่อสร้างใหม่commit:

Git Revert ขั้นตอนที่2

มาสร้างใหม่commitโดยที่เราได้ "บังเอิญ" ลบไฟล์:

ตัวอย่าง

git commit -m "Just a regular update, definitely no accidents here..."
[master 16a6f19] Just a regular update, definitely no accidents here...
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 img_hello_git.jpg

ตอนนี้เรามีส่วนในcommitประวัติศาสตร์ของเราที่เราต้องการย้อนกลับไป ลองทำrevertกันดูนะครับ


Git Revert ค้นหาการผูกมัดใน Log

อย่างแรก เราต้องหาจุดที่อยากกลับไป logในการทำเช่น นั้นเราต้องผ่าน

เพื่อหลีกเลี่ยงรายการบันทึกที่ยาวมาก เราจะใช้ --onelineตัวเลือก ซึ่งให้เพียงหนึ่งบรรทัดต่อการแสดงคอมมิต:

  • อักขระเจ็ดตัวแรกของcommit hash
  • ที่commit message

ลองหาจุดที่เราต้องการrevert:

ตัวอย่าง

git log --oneline
52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

เราต้องการเปลี่ยนกลับเป็นค่าก่อนหน้าcommit: 52418f7 (HEAD -> master) Just a regular update, definitely no accidents here...และเราเห็นว่านี่เป็นเวอร์ชันcommitล่าสุด



Git ย้อนกลับ HEAD

We revert the latest commit using git revert HEAD (revert the latest change,  and then commit), adding the option --no-edit to skip the commit message editor (getting the default revert message):

Example

git revert HEAD --no-edit
[master e56ba1f] Revert "Just a regular update, definitely no accidents here..."
 Date: Thu Apr 22 10:50:13 2021 +0200
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 img_hello_git.jpg

Now let's check the log again:

Example

git log --oneline
e56ba1f (HEAD -> master) Revert "Just a regular update, definitely no accidents here..."
52418f7 Just a regular update, definitely no accidents here...
9a9add8 (origin/master) Added .gitignore
81912ba Corrected spelling error
3fdaa5b Merge pull request #1 from w3schools-test/update-readme
836e5bf (origin/update-readme, update-readme) Updated readme for GitHub Branches
daf4f7c (origin/html-skeleton, html-skeleton) Updated index.html with basic meta
facaeae (gh-page/master) Merge branch 'master' of https://github.com/w3schools-test/hello-world
e7de78f Updated index.html. Resized image
5a04b6f Updated README.md with a line about focus
d29d69f Updated README.md with a line about GitHub
e0b6038 merged with hello-world-images after fixing conflicts
1f1584e added new image
dfa79db updated index.html with emergency fix
0312c55 Added image to Hello World
09f4acd Updated index.html with a new line
221ec6e First release of Hello World!

Note: To revert to earlier commits, use git revert HEAD~x (x being a number. 1 going back one more, 2 going back two more, etc.)

On the next page, we'll go over git reset, which brings the repository back to an earlier state in the commits without making a new commit.


Test Yourself With Exercises

Exercise:

Show the log of the repository, showing just 1 line per commit:

git