問題描述
當(dāng)你改亂了工作區(qū)某個(gè)文件的內(nèi)容,還沒有進(jìn)行git add .加入緩存區(qū)操作的時(shí)候。
想直接丟棄工作區(qū)的修改時(shí),用命令git checkout -- file進(jìn)行撤銷修改。
首先查看工作區(qū)的文件修改情況
使用git status可以查看工作區(qū)的文件修改情況,如下:
$ git status
On branch machine_unit/machine-unit-list
Your branch is up to date with 'origin/machine_unit/machine-unit-list'.
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: apps/machine_unit/views.py
modified: "docs/Chapter7/\346\234\272\347\273\204\345\210\227\350\241\250\347\232\204\345\237\272\346\234\254\351\200\273\350\276\221.md"
modified: templates/base_tpl/base-list-commom.html
no changes added to commit (use "git add" and/or "git commit -a")
可以看到修改了三個(gè)文件。
撤銷文件的修改,刪除工作區(qū)域內(nèi)容
# 首先撤銷第一個(gè)文件的修改
$ git checkout -- apps/machine_unit/views.py
# 使用git status進(jìn)行確認(rèn)
$ git status
On branch machine_unit/machine-unit-list
Your branch is up to date with 'origin/machine_unit/machine-unit-list'.
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: "docs/Chapter7/\346\234\272\347\273\204\345\210\227\350\241\250\347\232\204\345\237\272\346\234\254\351\200\273\350\276\221.md"
modified: templates/base_tpl/base-list-commom.html
no changes added to commit (use "git add" and/or "git commit -a")
# 繼續(xù)撤銷下面的兩個(gè)文件
$ git checkout -- docs/Chapter7/機(jī)組列表的基本邏輯.md
$ git checkout -- templates/base_tpl/base-list-commom.html
# 查看已經(jīng)清除感覺工作區(qū)域了。
$ git status
On branch machine_unit/machine-unit-list
Your branch is up to date with 'origin/machine_unit/machine-unit-list'.
nothing to commit, working tree clean
