1、工作区
C:\fyliu\lfyTemp\gitLocalRepository\yangjie
2、版本库
我们使用git init命令创建的.git就是我们的版本库。Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创建的第一个分支master
,以及指向master
的一个指针叫HEAD。
我们在工作区修改,通过git add命令添加到暂存区stage,通过git commit将暂存区的内容提交到本地库,这样,暂存区就变得干净了,没有从工作区git add到暂存区的,git commit不会将其提交到本地库。
3、撤销修改
1》 你对版本库维护的文件进行了修改,但还没有git add到暂存区。你可以删掉最后一行,手动把文件恢复到上一个版本的状态。使用git status查看下,也提供了git checkout...方法进行撤销修改。
命令:git checkout -- 文件
解析:可以丢弃工作区的修改。总之,就是让指定的文件回退到最近一次git add或者git commit时的状态。命令中的--
很重要,没有--
,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout
命令
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git status On branch master nothing to commit, working tree clean lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ vim readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ cat readme.txt GIt is a distributed version control system. Git is free software distributed under the GPL. Git has a mutable index called stage. Git tracks changes of files. My stupid boss still prefers SVN. lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (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: readme.txt no changes added to commit (use "git add" and/or "git commit -a") lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git checkout -- readme.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git status On branch master nothing to commit, working tree clean lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $
2》你已经git add,但还没有git commit。
命令:git reset HEAD 文件
解析:命令git reset HEAD <file>
可以把暂存区的修改撤销掉(unstage),即丢弃暂存区的修改,同时重新放回工作区。git reset
命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD
时,表示最新的版本。
总结:
1>丢弃工作区的修改,git checkout -- file
2>丢弃暂存区的修改,并将修改返回到工作区。git reset HEAD <file>
3>回退本地库的修改,前提是还没有提交到远程库。git reset --hard commit_id
4、删除文件,从本地库恢复文件。
我们使用rm test.txt删除了工作区的文件,这时候工作区与我们的版本库就不一致了。
1》一种情况,我们确实需要删除该文件,所以我们要修改版本库信息,告诉版本库我们确实需要删除该文件。
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ rm test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (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: test.txt no changes added to commit (use "git add" and/or "git commit -a") lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git rm test.txt rm 'test.txt' lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git commit -m "remove test.txt" [master d2891e0] remove test.txt 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $
2》另外一种情况,我们想要恢复文件到工作区。这时候从我们上面的git status命令的提示可以知道(use "git checkout -- <file>..." to discard changes in working directory)可以恢复文件。(相当于本地库,即版本库保存了文件的副本)
lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ vim test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ ls LICEENSE readme.txt test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git add test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git commit -m "add test.txt" [master 927067b] add test.txt 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ rm test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (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: test.txt no changes added to commit (use "git add" and/or "git commit -a") lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git checkout -- test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ git status On branch master nothing to commit, working tree clean lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $ ls LICEENSE readme.txt test.txt lfy@lfy-PC MINGW64 /c/fyliu/lfyTemp/gitLocalRepository/yangjie (master) $
所有评论(0)