本文衔接上文,主要讲如何修改缓存区内容
使用git checkout filename可以将指定文件回退到缓存区的状态
git checkout可以将被修改的文件变回缓存区的文件
比如,用文本编辑器vim或sublime将文件web1.html从
This is web page one
改为
HH, this is not web page one
使用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 ..." to update what will be committed)
(use "git checkout -- ..." 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, 发现内容变回
This is web page one
内容回退到缓存区版本的状态
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 ..." to update what will be committed)
(use "git checkout -- ..." 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可以将指定缓存区文件撤销
比如,修改web2.html从
This is web page two
改为
Hello World
This is web page two
然后将其提交到缓存区
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 ..." 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 ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: web2.html
no changes added to commit (use "git add" and/or "git commit -a")
比如,在系统中删除web2.html,查看状态
hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." 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 ..." 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 ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: web2.html
no changes added to commit (use "git add" and/or "git commit -a")
上一篇:Git基本操作1
下一篇:Git基本操作3