Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)
今天和git搏斗了一下午,发现了修改的文件一直commit不了。网上查了一下才发现原来git的模型里还有工作区和暂存区的说法。
- 工作区:在git管理下的正常目录都算是工作区。我们平时的编辑工作都是在工作区完成。
- 暂存区:可以理解为一个临时区域。里面存放将要提交文件的快照。
- 历史区:commit后,记录的归档。
三者的转换关系如下图:
需要注意的是:提交一个文件需要先git add <file>
把它放到暂存区,然后才能用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 <file>..." 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 <file>..." 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 <file>..." to unstage)
#
# new file: readme.md
#
# 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.md
#
会发现,状态中又多了一个:
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.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 <file>..." to update what will be committed)
# (use "git checkout -- <file>..." 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一次就好了。
参考资料
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013745374151782eb658c5a5ca454eaa451661275886c6000
http://selfcontroller.iteye.com/blog/1786644
知乎上关于这个的讨论:https://www.zhihu.com/question/19946553
Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)的更多相关文章
- git中的版本库,暂存区和工作区
- git 以及 工作区 版本库 暂存区
https://www.jianshu.com/p/a308acded2ce 这个博客介绍的比较简单 https://blog.csdn.net/qq_31828515/arti ...
- git命令--git checkout 之 撤销提交到暂存区的更改
SYJ@WIN-95I6OG3AT1N /D/gitlab/ihr-kafka-produce (master) $ git status [由于工作区文件被修改了,所以显示为红色] On branc ...
- EF CodeFirst系列(3)---EF中的继承策略(暂存)
我们初始化数据库一节已经知道:EF为每一个具体的类生成了数据库的表.现在有了一个问题:我们在设计领域类时经常用到继承,这能让我们的代码更简洁且容易管理,在面向对象中有“has a”和“is a”关系 ...
- 小丁带你走进git的世界二-工作区暂存区分支
小丁带你走进git的世界二-工作区暂存区分支 一.Git基本工作流程 1.初始化一个仓库 git init git clone git仓库分为两种情况: 第一种是在现有项目或目录下导入所有文件到 ...
- git工作区、暂存区、版本库之间的关系
区分三者关系 Git最让你迷惑的无非是它里面的各种概念了,如果是刚开始接触Git希望看完本篇介绍之后有一个清晰的认识,笔者认识也有限这里只说说个人对使用Git的感受,说一下它里面的几个最常用的概念的理 ...
- git(工作区,暂存区,管理修改,撤销修改,删除文件)
工作区和暂存区 984次阅读 Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的l ...
- 【Git】(1)---工作区、暂存区、版本库、远程仓库
工作区.暂存区.版本库.远程仓库 一.概念 1.四个工作区域 Git本地有四个工作区域:工作目录(Working Directory).暂存区(Stage/Index).资源库(Repository或 ...
- Git学习总结三(工作区和暂存区、撤销修改)
工作区和暂存区 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工作区: 版本库(Repository) 工作区有一个隐藏目录.git, ...
随机推荐
- Spring学习总结(15)——Spring AOP 拦截器的基本实现
一个程序猿在梦中解决的 Bug 没有人是不做梦的,在所有梦的排行中,白日梦最令人伤感.不知道身为程序猿的大家,有没有睡了一觉,然后在梦中把睡之前代码中怎么也搞不定的 Bug 给解决的经历?反正我是有过 ...
- File System Design Case Studies
SRC=http://www.cs.rutgers.edu/~pxk/416/notes/13-fs-studies.html Paul Krzyzanowski April 24, 2014 Int ...
- Ubuntu14.04.1安装搜狗拼音输入法
之前在Ubuntu16.04下安装过搜狗,在印象中与这次遇到的问题不一样,因此先说明一下这次Ubuntu的版本号: 参考博客http://blog.csdn.net/tao_627/article/d ...
- 再谈p2p投融资真相
近来亲自调查眼下各类p2p.重度调查对象有:人人贷.陆金所.前金所.开鑫贷.礼德財富.招財宝. 投资的有几个小观念: 首先,大家投资都习惯性的细分政府背景和非政府背景.说句实话,这对一个投资人角度来讲 ...
- logstash tcp multihost output(多目标主机输出,保证TCP输出链路的稳定性)
在清洗日志时,有一个应用场景,就是TCP输出时,须要在一个主机挂了的情况下,自已切换到下一个可用入口.而原tcp output仅支持单个目标主机设定.故本人在原tcp的基础上,开发出tcp_multi ...
- Codeforces 558B Amr and The Large Array
B. Amr and The Large Array time limit per test 1 second memory limit per test 256 megabytes input st ...
- C++中 pair 的使用方法
#include<iostream> #include<string> #include<map> using namespace std; // pair简单讲就 ...
- iOS实现抽屉效果
抽屉效果 在iOS中非常多应用都用到了抽屉效果,比如腾讯的QQ,百度贴吧- --- 1. 终于效果例如以下图所看到的 --- 2.实现步骤 1.開始启动的时候.新建3个不同颜色的View的 1.设置3 ...
- win32 Service memory leak
https://stackoverflow.com/questions/2728578/how-to-get-phyiscal-path-of-windows-service-using-net ht ...
- 存储概念解析:NAS与SAN的区别
目前存储网络技术领域中的两个主旋律是SAN(存储区域网络)和NAS(网络连接区域存储),两者都宣称是解决现代企业高容量数据存储需求的最佳选择. 正如在餐厅就餐时大厨不会为您传菜,跑堂不会为您烹制鲜橙烩 ...