Linux git(4)----工作区和暂存区

17 篇文章 0 订阅
用到的新命令


工作区(Working Directory):learn_git文件夹就是一个工作区,有一个隐藏目录.git,这个不算工作区,而是Git的版本库
[root@VM_0_11_centos learn_git]# ls -al
total 24
drwxr-xr-x   3 root root 4096 Apr 24 11:15 .
dr-xr-x---. 11 root root 4096 Apr 24 11:15 ..
drwxr-xr-x   8 root root 4096 Apr 24 11:15 .git
-rw-r--r--   1 root root   15 Apr 24 10:57 LICENSE
-rw-r--r--   1 root root  131 Apr 24 10:56 readme.txt
-rw-r--r--   1 root root    2 Apr 24 11:15 test


[root@VM_0_11_centos learn_git]# ls -al ./.git/
total 56
drwxr-xr-x  8 root root 4096 Apr 23 22:13 .
drwxr-xr-x  3 root root 4096 Apr 23 22:13 ..
drwxr-xr-x  2 root root 4096 Apr 23 17:38 branches
-rw-r--r--  1 root root   26 Apr 23 21:43 COMMIT_EDITMSG
-rw-r--r--  1 root root   92 Apr 23 17:38 config
-rw-r--r--  1 root root   73 Apr 23 17:38 description
-rw-r--r--  1 root root   23 Apr 23 17:38 HEAD     <== 指向master的一个指针叫HEAD
drwxr-xr-x  2 root root 4096 Apr 23 17:38 hooks
-rw-r--r--  1 root root  145 Apr 23 22:13 index    <== 暂存区
drwxr-xr-x  2 root root 4096 Apr 23 17:38 info
drwxr-xr-x  3 root root 4096 Apr 23 20:56 logs
drwxr-xr-x 13 root root 4096 Apr 23 21:43 objects
-rw-r--r--  1 root root   41 Apr 23 22:13 ORIG_HEAD
drwxr-xr-x  4 root root 4096 Apr 23 17:38 refs


版本库(Repository)
工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库。
Git的版本库里存了很多东西,其中最重要的就是称为stage(或者叫index)的暂存区,还有Git为我们自动创
建的第一个分支master,以及指向master的一个指针叫HEAD。
HEAD指向refs/heads/master

把文件往Git版本库里添加的时候,是分两步执行的:
第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;
第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支

[root@VM_0_11_centos .git]# cat HEAD 
ref: refs/heads/master
因为我们创建Git版本库时,Git自动为我们创建了唯一一个master(refs/heads/master)分支,所以,现在,git commit就是往master分支上提交更改。
可以简单理解为,需要提交的文件修改通通放到暂存区,然后,一次性提交暂存区的所有修改。

例:1.对readme.txt做个修改,比如加上一行内容:
[root@VM_0_11_centos learn_git]# vim readme.txt
Git is a distributed version control system.
Git is free software distributed under the GPL.
Git has a mutable index called stage.    <== 添加一行

2.工作区新增一个LICENSE文本文件(内容随便写)。
[root@VM_0_11_centos learn_git]# vim readme.txt
add a LICENESE

3.git status查看一下状态:
[root@VM_0_11_centos learn_git]# 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
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       LICENSE
no changes added to commit (use "git add" and/or "git commit -a")
Git非常清楚地告诉我们,readme.txt被修改了,而LICENSE还从来没有被添加过,所以它的状态是Untracked。

4.add到暂存库,并查看状态,暂存区状态为图1
[root@VM_0_11_centos learn_git]# git add LICENSE readme.txt
[root@VM_0_11_centos learn_git]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#       new file:   LICENSE
#       modified:   readme.txt
#

5.commit 到分支,状态为图2
[root@VM_0_11_centos learn_git]# git commit -m "understand how stage works"
[master e45a7a3] understand how stage works
 2 files changed, 2 insertions(+)
 create mode 100644 LICENSE
<== 把暂存区的所有修改提交到分支,那么工作区就是“干净”的
[root@VM_0_11_centos learn_git]# git status
# On branch master
nothing to commit, working directory clean

git-stage

图1

 

git-stage-after-commit

图2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值