您的位置:首页 > 其它

Git学习笔记 chapter04--撤销修改和删除文件

2017-07-24 11:22 399 查看
点击进入:廖雪峰老师的Git教程

继续修改我们的README.MD文件。

$ cat README.MD
This is my first use.
This is my second use.
Git tracks changes.
Git tracks changes of files.
my stupid boss still prefers SVN.
$ 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.MD

no changes added to commit (use "git add" and/or "git commit -a")


如果想要撤销我们的修改,使用git checkout –file就可以了

$ git checkout -- README.MD
$ git status
On branch master
nothing to commit, working tree clean

$ cat README.MD
This is my first use.
This is my second use.
Git tracks changes.
Git tracks changes of files.


可以看到我们的文件已经被更改到之前的状态了。

命令git checkout – README.MD意思就是,把README.MD文件在工作区的修改全部撤销,这里有两种情况:

一种是README.MD自修改后还没有被放到暂存区,现在,撤销修改就回到和版本库一模一样的状态;

一种是README.MD已经添加到暂存区后,又作了修改,现在,撤销修改就回到添加到暂存区后的状态。

总之,就是让这个文件回到最近一次git commit或git add时的状态。

git checkout – file命令中的–很重要,没有–,就变成了“切换到另一个分支”的命令

假设我们不仅做了修改,还git add到暂存区了,但是还没有commit。

那么git reset HEAD file命令可以把暂存区的修改撤销掉(unstage),重新放回工作区。

git reset命令既可以回退版本,也可以把暂存区的修改回退到工作区。当我们用HEAD时,表示最
adcd
新的版本。


$ cat README.MD
This is my first use.
This is my second use.
Git tracks changes.
Git tracks changes of files.
my boss is a supid.

$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified:   README.MD


执行git reset HEAD file命令之后,可以看到已经把暂存区的修改回退到工作区了。

$ git reset HEAD README.MD
Unstaged changes after reset:
M       README.MD

$ cat README.MD
This is my first use.
This is my second use.
Git tracks changes.
Git tracks changes of files.
my boss is a supid.

$ 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.MD

no changes added to commit (use "git add" and/or "git commit -a")


此时继续使用git checkout – file命令,就可以丢弃工作区的修改了。

如果我们不小心把修改commit 了,那么参考版本回退的知识,我们可以使用git reset –hard [commit id][HEAD^]回退到上一个版本,当然前提是没有git push到远程仓库。

如果我们要删除一个文件呢,这里新建一个文件hello.txt做实验。

$ cat hello.txt
I am hello.txt

$ git add hello.txt
warning: LF will be replaced by CRLF in hello.txt.
The file will have its original line endings in your working directory.

$ git commit -m "create file hello.txt"
[master 3614f8b] create file hello.txt
1 file changed, 1 insertion(+)
create mode 100644 hello.txt


直接在终端下使用rm 删除文件

$ rm hello.txt


这个时候,git status一下

$ 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:    hello.txt

no changes added to commit (use "git add" and/or "git commit -a")


可以看到我们删除了文件后,工作区和版本库不一致,git status显示哪些文件被删除了。

如果是我们删除错了,并且版本库中还有这个文件,那么可以使用 git checkout – hello.txt命令把误删的文件恢复到最新版本。

$ git checkout -- hello.txt

$ git status
On branch master
nothing to commit, working tree clean

$ cat hello.txt
I am hello.txt


或者,我们确定要完全删除这个文件,那么就直接在版本库中也删除掉,使用git rm 和git commit命令来完成

$ rm hello.txt

$ git rm hello.txt
rm 'hello.txt'

$ git commit -m "git remove hello.txt"
[master 9fac884] git remove hello.txt
1 file changed, 1 deletion(-)
delete mode 100644 hello.txt

$ ls
README.MD

$ git status
On branch master
nothing to commit, working tree clean


可以看到文件被彻底删除啦。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: