一、工作区
1.添加或者删除
创建或者删除文件
2.恢复被删除的文件
(1)查看状态
git status
例子:
$ git status
On branch dev2
Your branch is up to date with 'origin/dev2'.
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: add-changename.txt
(2)恢复(在git status里面有恢复命令)
git checkout -- add-changename.txt
二、暂存区
1.添加工作区文件到暂存区
git add
2.删除暂存区的文件
git rm
例如
$ git rm add-changename.txt
rm 'add-changename.txt'
3.恢复被删除的文件(从本地仓库)
(1)查看状态
$ git status
On branch dev2
Your branch is up to date with 'origin/dev2'.
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
deleted: add-changename.txt
(2)恢复被删除的,需要恢复文件(查看状态命令里面有命令提示)
$ git reset HEAD add-changename.txt
Unstaged changes after reset:
D add-changename.txt
三、本地仓库
1.添加
git commit -m "备注"
2.删除
(1)先删除暂存区文件
$ git rm test.txt
rm 'test.txt'
(2)提交到本地仓库
$ git commit -m "remove test.txt"
[master d46f35e] remove test.txt
1 file changed, 1 deletion(-)
delete mode 100644 test.txt
3.恢复:需要版本回退
(1)查看日志
$ git log
commit 1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master)
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:06:15 2018 +0800
append GPL
commit e475afc93c209a690c39c13a46716e8fa000c366
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 21:03:36 2018 +0800
add distributed
commit eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0
Author: Michael Liao <askxuefeng@gmail.com>
Date: Fri May 18 20:59:18 2018 +0800
wrote a readme file
(2)或者查看简化日志
$ git log --pretty=oneline
1094adb7b9b3807259d8cb349e7df1d4d6477073 (HEAD -> master) append GPL
e475afc93c209a690c39c13a46716e8fa000c366 add distributed
eaadf4e385e865d25c48e7ca9c8395c3f7dfaef0 wrote a readme file
(3)或者查看自己修改记录:以便确定要回到未来的哪个版本。
$ git reflog
e475afc HEAD@{1}: reset: moving to HEAD^
1094adb (HEAD -> master) HEAD@{2}: commit: append GPL
e475afc HEAD@{3}: commit: add distributed
eaadf4e HEAD@{4}: commit (initial): wrote a readme file
(4)恢复上一版本
$ git reset --hard HEAD^
HEAD is now at e475afc add distributed
(5)恢复到某一个版本
$ git reset --hard 1094a
HEAD is now at 83b0afe append GPL
四、远程仓库
1.确实要从版本库中删除该文件
(1)那就用命令git rm删掉
git rm test06add.txt
(2)并且git commit:
git commit -m "first delete"
(3)推送到远程数据库
git push
2.恢复
(1)工作区删除错了,暂存区里还有,可以使用checkout恢复
git checkout -- test06add.txt
(2)工作区和暂存区都删除了,但是远程数据库还没有删除,使用git clone重新