Gitกวดวิชา


Gitและ {{title}}


Git Contribute


Git Advanced


Git Undo




Git รีเซ็ต


Git รีเซ็ต

resetเป็นคำสั่งที่เราใช้เมื่อเราต้องการย้ายที่เก็บกลับไปเป็นก่อนหน้าcommitละทิ้งการเปลี่ยนแปลงใดๆ ที่เกิดขึ้นหลังจากcommitนั้น

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

Git รีเซ็ตขั้นตอนที่ 1

ขั้นตอนที่ 2: ย้ายที่เก็บกลับไปที่ขั้นตอนนั้น:

Git รีเซ็ตขั้นตอนที่2

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


Git รีเซ็ต ค้นหาการคอมมิตใน Log

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

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

  • อักขระเจ็ดตัวแรกของcommit hash- นี่คือสิ่งที่เราต้องอ้างถึงในคำสั่งรีเซ็ตของเรา
  • ที่ commit message

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

ตัวอย่าง

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!

เราต้องการกลับไปที่commit: 9a9add8 (origin/master) Added .gitignoreอันสุดท้ายก่อนที่เราจะเริ่มยุ่งกับสิ่งต่างๆ



Git รีเซ็ต

We reset our repository back to the specific commit using git reset commithash (commithash being the first 7 characters of the commit hash we found in the log):

Example

git reset 9a9add8

Now let's check the log again:

Example

git log --oneline
9a9add8 (HEAD -> master, 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!

Warning: Messing with the commit history of a repository can be dangerous. It is usually ok to make these kinds of changes to your own local repository. However, you should avoid making changes that rewrite history to remote repositories, especially if others are working with them.


Git Undo Reset

Even though the commits are no longer showing up in the log, it is not removed from Git.

If you know the commit hash you can reset to it:

Example

git reset e56ba1f

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!

Test Yourself With Exercises

Exercise:

reset to the commit with the hash abc1234:

git