设为首页 收藏本站
查看: 764|回复: 0

[经验分享] Git中的工作区(Working Directory)、暂存区(stage)和历史记录区(history)

[复制链接]

尚未签到

发表于 2018-1-13 15:35:27 | 显示全部楼层 |阅读模式
  今天和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 &quot;git add&quot; to track)
  会发现现在什么都没有。毕竟这个目录里面我们还没有放东西嘛。
  接下来,新建一个叫做readme.md的文件,并在里面写点内容。
  然后git status来查看下状态:
phantom01@phantom01-VirtualBox:~/work/learn_git$ git status  
# On branch master
  
#
  
# Initial commit
  
#
  
# Untracked files:
  
#   (use &quot;git add <file>...&quot; to include in what will be committed)
  
#
  
#   readme.md
  
#
  
# nothing added to commit but untracked files present (use &quot;git add&quot; to track)
  发现现在readme.md是&quot;Untracked files&quot;,说明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 &quot;git rm --cached <file>...&quot; to unstage)
  
#
  
#   new file:   readme.md
  
#
  会发现,现在文件已经变成了&quot;Changes to be committed&quot;中的&quot;new file&quot;。
  
此时,我们刚才修改的部分已经被提交至暂存区。
  我们修改一下文件中的内容,然后在查看一下状态:
phantom01@phantom01-VirtualBox:~/work/learn_git$ git status  
# On branch master
  
#
  
# Initial commit
  
#
  
# Changes to be committed:
  
#   (use &quot;git rm --cached <file>...&quot; to unstage)
  
#
  
#   new file:   readme.md
  
#
  
# Changes not staged for commit:
  
#   (use &quot;git add <file>...&quot; to update what will be committed)
  
#   (use &quot;git checkout -- <file>...&quot; to discard changes in working directory)
  
#
  
#   modified:   readme.md
  
#
  会发现,状态中又多了一个:
  

Changes not staged for commit:  (use &quot;git add <file>...&quot; to update what will be committed)
  (use &quot;git checkout -- <file>...&quot; to discard changes in working directory)
  

  modified:   readme.md
  

  

  其中&quot;Changes not staged for commit&quot;是说没有被提交到暂存区。
  接下来我们用git commit提交一下:
phantom01@phantom01-VirtualBox:~/work/learn_git$ git commit -m &quot;ci1&quot;  
# [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 &quot;git add <file>...&quot; to update what will be committed)
  
#   (use &quot;git checkout -- <file>...&quot; to discard changes in working directory)
  
#
  
#   modified:   readme.md
  
#
  
# no changes added to commit (use &quot;git add&quot; and/or &quot;git commit -a&quot;)
  
#
  会发现&quot;be committed&quot;那一段不见了,而&quot;not staged&quot;还在。这说明一段内容被提交了,而后一段内容没有被提交。
  接下来,我们只需要在git add和commit一次就好了。

参考资料
  http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013745374151782eb658c5a5ca454eaa451661275886c6000
  
http://selfcontroller.iteye.com/blog/1786644
  知乎上关于这个的讨论:https://www.zhihu.com/question/19946553

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434713-1-1.html 上篇帖子: Maven自动部署(SCM-SVN/Git)(maven-scm 下篇帖子: Git的日常处理流程
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表