Git撤銷未提交(commit)的修改
1.新建test.txt,添加內容如下:
hello world
2.git add test.txt
3.修改test.txt為
hello world。 hello china
(使用git status可以看到“待提交”的文件)
4.使用checkout命令撤銷修改
git checkout test.txt
這個時候test.txt的內容恢復為
hello world
(使用git status可以看到沒有了“待提交”的文件)
如果第3步與第4步之間使用了:git add test.txt(此時使用git status會發現test.txt處於已經添加的狀態),那么暫時無法使用checkout命令撤銷修改(就算使用checkout命令之后,test.txt仍然處於已經添加的狀態)。
此時需要使用reset 命令:
git reset HEAD test.txt(注意:如果工程沒有提交commit過,那么這條命令無法執行)
此時再運行git status命令,會發現test.txt現在處於未添加的狀態。
這個時候可以使用git checkout test.txt命令撤銷修改。