管理修改
之前一直以为Git管理的是我们修改过后的文件,才发现原来我认为的是错,Git跟踪管理的是修改,而并非文件
首先,我们对readme.txt进修改
$ cat readme.txt
11111
2222
3333
4444
5555
6666
然后,添加
$ git add readme.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
再修改readme.txt,然后提交
$ git commit -m "add"
[master 0c85a37] add
2 files changed, 4 insertions(+), 1 deletion(-)
查看状态
$ 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
第二次的并没有被提交
因此我们可以看出Git管理的是修改,我们使用git add命令,只是把修改放入了暂存区,使用 git commit只负责把暂存区的修改提交到当前master分支当中
撤销修改
撤销修改可以分为三种情况
第一种:修改后还没有被放到暂存区
$ cat readme.txt
\f0\fs24 \cf0 11111\
2222\
3333\
4444\
5555\
6666\
7777
8888
$ 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
$ git checkout -- readme.txt
$ cat readme.txt
\f0\fs24 \cf0 11111\
2222\
3333\
4444\
5555\
6666\
7777
使用git checkout -- readme.txt 就可以复原到之前的版本
第二种:修改了而且已经添加到了暂存区
$ cat readme.txt
\f0\fs24 \cf0 11111\
2222\
3333\
4444\
5555\
6666\
7777
8888}
$ git add readme.txt
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: readme.txt
$ git reset HEAD readme.txt
Unstaged changes after reset:
M readme.txt
$ 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
$ git checkout -- readme.txt
$ cat readme.txt
\f0\fs24 \cf0 11111\
2222\
3333\
4444\
5555\
6666\
7777
使用git reset HEAD readme.txt 可以把暂存区的修改给撤销
第三种: 已经提交到了版本库,但是并没有推送到远程库中,可以使用版本退回的命令撤销
git reset -- hard HEAD^
删除文件
使用rm可以直接删除文件夹中的文件
$ rm test.txt
$ 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
使用git rm test.txt可以把版本库的文件删除
$ git rm test.txt
rm 'test.txt'
$ git commit -m "remove test.txt"
[master f20e07c] remove test.txt
1 file changed, 0 insertions(+), 0 deletions(-)
delete mode 100644 test.txt
如果不小心把工作去的文件删除了,可以使用
git checkout -- test.txt回到之前的版本