撤銷修改
首先,我們在readme.md中新增一條修改
nelsen-mac:learngit mac$ echo 'i am stupid' >> readme.md
再用git status查看狀態
nelsen-mac:learngit mac$ 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: readme.md
no changes added to commit (use "git add" and/or "git commit -a")
可以看出,修改未增加git add到暫存區(stage),提示我們可以用 git checkout -- <file>...放棄修改
一種是
readme.txt
自修改後還沒有被放到暫存區,現在,撤銷修改就回到和版本庫一模一樣的狀態;
git checkout -- file
命令中的--
很重要,沒有--
,就變成了“切換到另一個分支”的命令
一種是
readme.txt
已經添加到暫存區後,又作了修改,現在,撤銷修改就回到添加到暫存區後的狀態。
然後已經git add 到暫存區
nelsen-mac:learngit mac$ git add readme.md
nelsen-mac:learngit mac$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.md
提示告訴我們,可以用git reset HEAD <file>... 撤銷
git reset
命令既可以回退版本,也可以把暫存區的修改回退到工作區。當我們用HEAD
時,表示最新的版本。
nelsen-mac:learngit mac$ 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: readme.md
說明暫存區是乾淨的,工作區有修改,然後我們放棄修改
nelsen-mac:learngit mac$ git checkout -- readme.md
如果已經git commit提交了不合適的修改到版本庫時,想要撤銷本次提交,就需要執行版本回退,參考系列三