Git学习 <day3>

常用命令

git log

结合上节的git reset命令来做个测试,创建testLog.txt文件,经过如下四次修改、git add、git commit,三个区域的内容一致:

此时,三个区域的内容都是My address Sichuan, chengdu, HiTechZone
查看提交记录:

➜  mygit git:(master) git log --pretty=oneline
c52408dc6dc820dd85fef2e81ed962b9d9cbbf23 modify region from wuhou to HiTechZone
f9e8946d20537e5e58d347b9763c1892891964f7 modify address: add 'wuhou' to address
aa9ca955b1661a826ae0474239bc4bdb1d019777 add address
e6ffb5d127cb8cd38fec41fa033eeb56a7b528a9 Create my address

我要回到某个版本,有2种方式:
1. git reset –hard HEAD~向上回滚的版本个数 暂存区和工作目录都会被覆盖;git reset –mixed HEAD~向上回滚的版本个数 只有暂存区被覆盖,工作目录保持不变。
2. git reset –hard 版本号;git reset –mixed 版本号。

我想回到”add address”这个版本:

➜  mygit git:(master) ✗ git reset --hard HEAD~2 
HEAD is now at aa9ca95 add address

信息显示HEAD指针已经指向了版本号为aa9ca95,提交信息为”add address”的这个版本了。

mygit git:(master) cat testLog.txt
My address
Sichuan, chengdu

果不其然,我已经回到了过去,我又想走向未来”modify address: add ‘wuhou’ to address”这个版本,怎么办呢,这时必须使用版本号了:

➜  mygit git:(master) git log --pretty=oneline

aa9ca955b1661a826ae0474239bc4bdb1d019777 add address
e6ffb5d127cb8cd38fec41fa033eeb56a7b528a9 Create my address

我使用git log --pretty=oneline 这个命令发现,只有从第一次提交到当前版本的日志,没有办法走向未来了,这怎么办呢?没关系,git很智能的,有另外一个命令可以解决这个问题:

➜  mygit git:(master) git reflog

aa9ca95 HEAD@{0}: reset: moving to HEAD~2
c52408d HEAD@{1}: commit: modify region from wuhou to HiTechZone
f9e8946 HEAD@{2}: commit: modify address: add 'wuhou' to address
aa9ca95 HEAD@{3}: commit: add address
e6ffb5d HEAD@{4}: commit: Create my address

git reflog 可以获取到我所有对版本库的操作记录,注意是操作记录,也就是除了commit之外的其他操作也都会被记录下来。OK,现在我可以走向未来了,注意这里的版本号只有前几位,也是可以区分的:

➜  mygit git:(master) git reset --hard f9e8946
HEAD is now at f9e8946 modify address: add 'wuhou' to address

➜  mygit git:(master) cat testLog.txt
My address
Sichuan, chengdu, wuhou

综上,git log 和git reset相结合,允许我们可以向前走,也可以向后走。

git rm

创建testRm.txt文件并提交到仓库。

1. 测试git rm 的作用:

➜  mygit git:(master) git rm testRm.txt 
rm 'testRm.txt'
➜  mygit git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    testRm.txt

➜  mygit git:(master) ✗ ls
helloworld.txt

从上面的信息我们可以看到,执行完git rm testRm.txt 命令后,工作目录中已经没有了testRm.txt文件,git status 显示的信息表明:暂存区有未提交的changes,这些changes是什么呢?就是deleted: testRm.txt,同时建议我们可以使用git reset HEAD <file>... 将暂存区的更改unstage。因此,git rm 删除的是working directory工作区和staging area暂存区的文件。当然仓库的对应文件还没删除,我们还有后悔的余地。

2. 测试git rm -f 的作用:
先把上面的测试操作还原:

➜  mygit git:(master) ✗ git reset HEAD testRm.txt
Unstaged changes after reset:
D   testRm.txt
➜  mygit git:(master) ✗ git checkout testRm.txt
➜  mygit git:(master) git status
On branch master
nothing to commit, working directory clean
➜  mygit git:(master) ls
helloworld.txt testRm.txt

git reset HEAD testRm.txt 将暂存区还原,git checkout testRm.txt 将工作目录还原。下面开始测试加了-f 之后的作用:

➜  mygit git:(master) git rm -f testRm.txt 
rm 'testRm.txt'
➜  mygit git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    testRm.txt

➜  mygit git:(master) ✗ ls
helloworld.txt

可以看到,加了-f 跟之前是一样的,-f 是force的意思,可见git rm默认就是强制删除,即同时删除暂存区和工作目录。
3. 测试git rm --cached 的作用:
将testRm.txt还原,执行如下操作:

➜  mygit git:(master) git rm --cached testRm.txt 
rm 'testRm.txt'
➜  mygit git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    deleted:    testRm.txt

Untracked files:
  (use "git add <file>..." to include in what will be committed)

    testRm.txt

➜  mygit git:(master) ✗ ls
helloworld.txt testRm.txt

从以上信息可以看出,暂存区是deleted的状态,而工作目录是untracked的状态,说明git rm --cached 删除了暂存区,保留了工作目录,而仓库还是没有变化的,我们还可以通过reset对暂存区的删除操作unstage。

4. 测试未commit文件的删除:

如果文件只存在工作目录:

➜  mygit git:(master) ✗ git status
On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

    testRm.txt

nothing added to commit but untracked files present (use "git add" to track)

这时的文件还没有被git管理,因此不能使用git rm命令,直接用rm删除即可:

➜  mygit git:(master) ✗ git rm testRm.txt
fatal: pathspec 'testRm.txt' did not match any files
➜  mygit git:(master) ✗ rm testRm.txt
➜  mygit git:(master) ls
helloworld.txt

如果文件存在工作目录和暂存区,还没提交到仓库:

➜  mygit git:(master) ✗ ls
helloworld.txt testRm.txt
➜  mygit git:(master) ✗ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    new file:   testRm.txt

这时候我们执行git rm操作:

➜  mygit git:(master) ✗ git rm testRm.txt 
error: the following file has changes staged in the index:
    testRm.txt
(use --cached to keep the file, or -f to force removal)

因为仓库没有该文件,这时git就会提醒我们,是强制删除,还是只删除暂存区的。如果强制删除,工作目录和暂存区都删除了,那就再也找不回来咯。若用–cached删除,只会删除暂存区,工作目录会保留。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值