浅入Git学习⑤--管理修改、撤销修改、删除文件

管理修改

之前一直以为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回到之前的版本
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值