场景一:对文件的修改尚未添加到暂存
命令:
git checkout -- 文件名
示例:
➜ test git:(master) ✗ ls
1.sql q.txt
➜ test git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 1.sql
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: q.txt
➜ test git:(master) ✗ git checkout -- q.txt
➜ test git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 1.sql
场景二: 已经添加到暂存区
命令:
git reset HEAD 文件名
git checkout -- 文件名
示例:
➜ test git:(master) ✗ ls
1.sql q.txt
➜ test git:(master) ✗ git status
On branch master
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: 1.sql
➜ test git:(master) ✗ git reset HEAD 1.sql
Unstaged changes after reset:
M 1.sql
➜ test git:(master) ✗ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: 1.sql
no changes added to commit (use "git add" and/or "git commit -a")
➜ test git:(master) ✗ git checkout -- 1.sql
➜ test git:(master) git status
On branch master
nothing to commit, working tree clean