ฉันสับสนกับสิ่งนี้อยู่เสมอดังนั้นนี่คือกรณีทดสอบเตือนความจำ สมมติว่าเรามีbashสคริปต์นี้เพื่อทดสอบgit:
set -x
rm -rf test
mkdir test
cd test
git init
git config user.name test
git config user.email test@test.com
echo 1 > a.txt
echo 1 > b.txt
git add *
git commit -m "initial commit"
echo 2 >> b.txt
git add b.txt
git commit -m "second commit"
echo 3 >> b.txt
ณ จุดนี้การเปลี่ยนแปลงไม่ได้ถูกจัดเตรียมไว้ในแคชดังนั้นgit status:
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   b.txt
no changes added to commit (use "git add" and/or "git commit -a")
หากมาจากจุดนี้เราทำgit checkoutผลที่ได้คือ:
$ git checkout HEAD -- b.txt
$ git status
On branch master
nothing to commit, working directory clean
หากเราทำเช่นgit resetนั้นผลลัพธ์ก็คือ:
$ git reset HEAD -- b.txt
Unstaged changes after reset:
M   b.txt
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   b.txt
no changes added to commit (use "git add" and/or "git commit -a")
ดังนั้นในกรณีนี้ - หากการเปลี่ยนแปลงไม่ได้จัดฉากก็git resetไม่ต่างอะไรในขณะที่git checkoutเขียนทับการเปลี่ยนแปลง
ทีนี้สมมติว่าการเปลี่ยนแปลงครั้งสุดท้ายจากสคริปต์ด้านบนนั้นถูกจัดฉาก / แคชนั่นก็คือการบอกเราว่าgit add b.txtท้ายที่สุดแล้ว
ในกรณีนี้git statusณ จุดนี้คือ:
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)
    modified:   b.txt
หากมาจากจุดนี้เราทำgit checkoutผลที่ได้คือ:
$ git checkout HEAD -- b.txt
$ git status
On branch master
nothing to commit, working directory clean
หากเราทำเช่นgit resetนั้นผลลัพธ์ก็คือ:
$ git reset HEAD -- b.txt
Unstaged changes after reset:
M   b.txt
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)
    modified:   b.txt
no changes added to commit (use "git add" and/or "git commit -a")
ดังนั้นในกรณีนี้ - ถ้าการเปลี่ยนแปลงถูกจัดฉากgit resetโดยทั่วไปจะทำการเปลี่ยนแปลงตามฉากเป็นการเปลี่ยนแปลงที่ไม่มีการจัดฉาก - ในขณะที่git checkoutจะเขียนทับการเปลี่ยนแปลงทั้งหมด