git stage 暂存_Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)

今天和git搏斗了一下午,发现了修改的文件一直commit不了。网上查了一下才发现原来git的模型里还有工作区和暂存区的说法。

工作区:在git管理下的正常目录都算是工作区。我们平时的编辑工作都是在工作区完成。

暂存区:可以理解为一个临时区域。里面存放将要提交文件的快照。

历史区:commit后,记录的归档。

三者的转换关系如下图:

需要注意的是:提交一个文件需要先git add 把它放到暂存区,然后才能用git commit真正提交。

这是一个和svn在使用上一个很大的区别。一直commit发现提交不上去,找了好久才发现,原来是没有提交到暂存区。

实验

下面来演示一下:

首先新建一个叫做learn_git的目录,并初始化:

phantom01@phantom01-VirtualBox:~/work$ mkdir learn_git

phantom01@phantom01-VirtualBox:~/work$ cd learn_git/

phantom01@phantom01-VirtualBox:~/work/learn_git$ git init

# Initialized empty Git repository in /home/phantom01/work/learn_git/.git/

然后git status查看现在的状态:

phantom01@phantom01-VirtualBox:~/work/learn_git$ git status

# On branch master

#

# Initial commit

#

# nothing to commit (create/copy files and use "git add" to track)

会发现现在什么都没有。毕竟这个目录里面我们还没有放东西嘛。

接下来,新建一个叫做readme.md的文件,并在里面写点内容。

然后git status来查看下状态:

phantom01@phantom01-VirtualBox:~/work/learn_git$ git status

# On branch master

#

# Initial commit

#

# Untracked files:

# (use "git add ..." to include in what will be committed)

#

# readme.md

#

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

发现现在readme.md是"Untracked files",说明git现在还没有开始追踪这个文件,这时需要我们用git add来把这个文件添加进git 的管理。然后git status来查看当前状态。

phantom01@phantom01-VirtualBox:~/work/learn_git$ git add readme.md

phantom01@phantom01-VirtualBox:~/work/learn_git$ git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use "git rm --cached ..." to unstage)

#

# new file: readme.md

#

会发现,现在文件已经变成了"Changes to be committed"中的"new file"。

此时,我们刚才修改的部分已经被提交至暂存区。

我们修改一下文件中的内容,然后在查看一下状态:

phantom01@phantom01-VirtualBox:~/work/learn_git$ git status

# On branch master

#

# Initial commit

#

# Changes to be committed:

# (use "git rm --cached ..." to unstage)

#

# new file: readme.md

#

# Changes not staged for commit:

# (use "git add ..." to update what will be committed)

# (use "git checkout -- ..." to discard changes in working directory)

#

# modified: readme.md

#

会发现,状态中又多了一个:

Changes not staged for commit:

(use "git add ..." to update what will be committed)

(use "git checkout -- ..." to discard changes in working directory)

modified: readme.md

其中"Changes not staged for commit"是说没有被提交到暂存区。

接下来我们用git commit提交一下:

phantom01@phantom01-VirtualBox:~/work/learn_git$ git commit -m "ci1"

# [master (root-commit) 41adae7] ci1

# 1 file changed, 1 insertion(+)

# create mode 100644 readme.md

phantom01@phantom01-VirtualBox:~/work/learn_git$ git status

# On branch master

# Changes not staged for commit:

# (use "git add ..." to update what will be committed)

# (use "git checkout -- ..." to discard changes in working directory)

#

# modified: readme.md

#

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

#

会发现"be committed"那一段不见了,而"not staged"还在。这说明一段内容被提交了,而后一段内容没有被提交。

接下来,我们只需要在git add和commit一次就好了。

参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值