撤销修改
git checkout – filename可以丢弃⼯作区的修改。
$ git checkout -- test2.txt
这个命令是把test2.txt在工作区的修改撤销,有2种情况:
- test2.txt修改后不在暂存区,即没有add,现在撤销就回到版本库一样的状态。
这是当前版本库中的文件内容
$ cat test2.txt
1234567890
0987654321
然后修改文件内容如下
$ cat test2.txt
1234567890
0987654321
修改了test2.txt文件
撤销修改后文件状态
$ git checkout -- test2.txt
$ cat test2.txt
1234567890
0987654321
可以看到文件回到了和版本库一样的状态。
- test2.txt add 到暂存区后,又进行了修改,这时候撤销就会回到add到暂存区后的状态。
add到暂存区后文件的内容
$ git add test2.txt
$ cat test2.txt
1234567890
0987654321
然后修改文件内容$ vim test2.txt
撤销修改然后查看文件内容
$ git checkout -- test2.txt
$ cat test2.txt
1234567890
0987654321
文件回到了刚add后的状态。
总结一下,就是撤销修改会让这个文件回到最近一次add 或者 commit时的状态。
git reset HEAD filename可以把暂存区的修改撤销,重新放回⼯作区。HEAD表示最新的版本。
删除文件
当用rm 删除文件时,工作区和版本库的内容就不一致了。
$ rm test2.txt
$ git status
On branch master
Changes not staged for commit:
(use "git add/rm <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
deleted: test2.txt
no changes added to commit (use "git add" and/or "git commit -a")
这种时候分2个情况:
- 你误删了,那么你可以直接撤销删除还原文件,删除也是一种修改操作,所以用
$ git checkout -- test2.txt
撤销。 - 你确实想删除,那么删掉之后进行commit。
$ git commit test2.txt -m "删除test2.txt"
[master 7338878] 删除test2.txt
1 file changed, 2 deletions(-)
delete mode 100644 test2.txt
移动文件
使用 git mv
移动文件,也可用来重命名文件。
例1:将test.txt移动到dic文件夹。
$ git mv test.txt dic/
例2:将test.txt 重命名为test1.txt。
$ git mv test.txt test1.txt