Git基本用法2

本文衔接上文,主要讲如何修改缓存区内容

回退到缓存区状态

使用git checkout filename可以将指定文件回退到缓存区的状态

被修改的文件

git checkout可以将被修改的文件变回缓存区的文件

比如,用文本编辑器vim或sublime将文件web1.html从

<html>
	<body>
		This is web page one
	</body>
</html>

改为

<html>
	<body>
		HH, this is not web page one
	</body>
</html>

使用git status可以看到此时web1.html属于被修改状态

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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:   web1.html

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

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$

调用git checkout web1.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout web1.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$

用文本编辑器vim或sublime打开web1.html, 发现内容变回

<html>
	<body>
		This is web page one
	</body>
</html>

内容回退到缓存区版本的状态


被删除的文件

git checkout可以将被删除的文件变回缓存区的文件

在系统中将web1.html删除,则状态为

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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:    web1.html

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

此时可以用git checkout恢复文件

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git checkout web1.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
nothing to commit, working tree clean

在系统中可以发现被删除的web1.html又回来了


从缓存区中撤销

使用git reset HEAD -- filename可以将指定缓存区文件撤销

被修改的文件

使用git reset HEAD可以将被修改文件撤销

比如,修改web2.html从

<html>
	<body>
		This is web page two
	</body>
</html>

改为

<html>
	<body>
		Hello World<br>
		This is web page two
	</body>
</html>

然后将其提交到缓存区

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   web2.html

此时文件web2.html处于可以提交到本地仓库的状态,我们可以通过git reset HEAD filename将文件从缓存区撤销

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git reset HEAD web2.html
Unstaged changes after reset:
M       web2.html

此时文件变回未提交到缓存区前的状态

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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:   web2.html

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


被删除的文件

使用git reset HEAD可以将被删除文件撤销

比如,在系统中删除web2.html,查看状态

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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:    web2.html

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

用git add web2.html将文件web2.html提交到缓存区

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git add web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        deleted:    web2.html

此时可以用git reset 将提交到缓存区的内容撤回到本地

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ git reset HEAD web2.html
Unstaged changes after reset:
D       web2.html

hp@pc MINGW64 ~/Desktop/Blog/Git test (master)
$ 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:    web2.html

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

上一篇:Git基本操作1

下一篇:Git基本操作3


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值