本文衔接上文,主要讲如何修改缓存区内容
回退到缓存区状态
使用git checkout filename可以将指定文件回退到缓存区的状态
被修改的文件
git checkout可以将被修改的文件变回缓存区的文件
比如,用文本编辑器vim或sublime将文件web1.html从
<html>
<body>
This is web page one
</body>
</html>
改为
<html>
<body>
HH, this is not web page one
</body>
</html>
使用git status可以看到此时web1.html属于被修改状态
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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: web1.html
no changes added to commit (use "git add" and/or "git commit -a")
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
调用git checkout web1.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout web1.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$
用文本编辑器vim或sublime打开web1.html, 发现内容变回
<html>
<body>
This is web page one
</body>
</html>
内容回退到缓存区版本的状态
被删除的文件
git checkout可以将被删除的文件变回缓存区的文件
在系统中将web1.html删除,则状态为
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: web1.html
no changes added to commit (use "git add" and/or "git commit -a")
此时可以用git checkout恢复文件
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout web1.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
nothing to commit, working tree clean
在系统中可以发现被删除的web1.html又回来了
从缓存区中撤销
使用git reset HEAD -- filename可以将指定缓存区文件撤销
被修改的文件
使用git reset HEAD可以将被修改文件撤销比如,修改web2.html从
<html>
<body>
This is web page two
</body>
</html>
改为
<html>
<body>
Hello World<br>
This is web page two
</body>
</html>
然后将其提交到缓存区
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: web2.html
此时文件web2.html处于可以提交到本地仓库的状态,我们可以通过git reset HEAD filename将文件从缓存区撤销
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git reset HEAD web2.html
Unstaged changes after reset:
M web2.html
此时文件变回未提交到缓存区前的状态
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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: web2.html
no changes added to commit (use "git add" and/or "git commit -a")
被删除的文件
使用git reset HEAD可以将被删除文件撤销比如,在系统中删除web2.html,查看状态
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: web2.html
no changes added to commit (use "git add" and/or "git commit -a")
用git add web2.html将文件web2.html提交到缓存区
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: web2.html
此时可以用git reset 将提交到缓存区的内容撤回到本地
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git reset HEAD web2.html
Unstaged changes after reset:
D web2.html
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
deleted: web2.html
no changes added to commit (use "git add" and/or "git commit -a")
下一篇:Git基本操作3