Git介绍

Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持。

Git 与 SVN 区别

GIT不仅仅是个版本控制系统,它也是个内容管理系统(CMS),工作管理系统等。

如果你是一个具有使用SVN背景的人,你需要做一定的思想转换,来适应GIT提供的一些概念和特征。

Git 与 SVN 区别点:

  • 1、GIT是分布式的,SVN不是:这是GIT和其它非分布式的版本控制系统,例如SVN,CVS等,最核心的区别。

  • 2、GIT把内容按元数据方式存储,而SVN是按文件:所有的资源控制系统都是把文件的元信息隐藏在一个类似.svn,.cvs等的文件夹里。

  • 3、GIT分支和SVN的分支不同:分支在SVN中一点不特别,就是版本库中的另外的一个目录。

  • 4、GIT没有一个全局的版本号,而SVN有:目前为止这是跟SVN相比GIT缺少的最大的一个特征。

  • 5、GIT的内容完整性要优于SVN:GIT的内容存储使用的是SHA-1哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏。

安装

  1. #这里只介绍centos的
  2. [root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
  3. [root@localhost ~]# yum -y install git-core
  4. [root@localhost ~]# git --version
  5. git version 1.7.1

  

git工作流程

一般工作流程如下:

  • 克隆 Git 资源作为工作目录。
  • 在克隆的资源上添加或修改文件。
  • 如果其他人修改了,你可以更新资源。
  • 在提交前查看修改。
  • 提交修改。
  • 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。

下图展示了 Git 的工作流程:

Git 创建仓库

  1. #git init
  2. #git init用于初始化一个git仓库,git的很多命令都在git仓库里运行。
  3. #执行完毕后,会在仓库根目录里生成一个.git目录(跟svn一样,会出现一个.svn目录),该目录包含了资源的所有元数据,其他的项目目录保持不变,这点跟svn有所不同,svn是其它子目录也会产生一个.svn目录
  4. [root@localhost /]# mkdir git1
  5. [root@localhost /]# cd git1
  6. [root@localhost git1]# git init #使用当前目录作为git的仓库,init初始化
  7. Initialized empty Git repository in /git1/.git/
  8. [root@localhost git1]# ls -al
  9. 总用量 12
  10. drwxr-xr-x 3 root root 4096 1 25 06:02 .
  11. dr-xr-xr-x. 28 root root 4096 1 25 06:02 ..
  12. drwxr-xr-x 7 root root 4096 1 25 06:02 .git #出现了一个隐藏文件.git
  13.  
  14. [root@localhost git1]# mkdir /git2
  15. [root@localhost git1]# git init /git2 #指定/git2目录作为git仓库
  16. Initialized empty Git repository in /git2/.git/
  17. [root@localhost git1]# ls -al /git2/
  18. 总用量 12
  19. drwxr-xr-x 3 root root 4096 1 25 06:03 .
  20. dr-xr-xr-x. 29 root root 4096 1 25 06:02 ..
  21. drwxr-xr-x 7 root root 4096 1 25 06:03 .git
  22.  
  23. #git clone
  24. #git clone从现有的git仓库中克隆项目到指定目录(跟svn的checkout一样),指定目录就是工作区
  25. #命令格式:git clone <repo>,克隆项目到指定目录:git clone <repo> <directory>,repo为git仓库,directory为指定目录
  26. [root@localhost /]# mkdir git_test1
  27. [root@localhost /]# cd /git_test1/
  28. [root@localhost git_test1]# git clone /git1/ #后面没有指定目录,则克隆到当前目录
  29. Initialized empty Git repository in /git_test1/git1/.git/
  30. warning: You appear to have cloned an empty repository.
  31. [root@localhost git_test1]# ls
  32. git1 #当前目录出现了一个.git1文件
  33.  
  34. [root@localhost git_test1]# mkdir /git_test2
  35. [root@localhost git_test1]# git clone /git1/ /git_test2/gittest2 #指定一个目录为克隆目录,并且指定一个新名字
  36. Initialized empty Git repository in /git_test2/gittest2/.git/
  37. warning: You appear to have cloned an empty repository.
  38. [root@localhost git_test1]# ls /git_test2/
  39. gittest2

  

Git 基本操作

  1. ##基本快照
  2. #Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比,然后选择发布还是回滚,有点类似虚拟机的快照
  3.  
  4. ##git add
  5. #git add用于将文件添加到缓存
  6. [root@localhost /]# cd /git_test1/git1/
  7. [root@localhost git1]# touch README
  8. [root@localhost git1]# touch hello.py
  9. [root@localhost git1]# git status -s
  10. ?? README
  11. ?? hello.py
  12. #git status 命令用于查看项目的当前状态。
  13. [root@localhost git1]# git add README
  14. [root@localhost git1]# git add hello.py
  15. [root@localhost git1]# git status -s
  16. A README
  17. A hello.py
  18. #如果想要添加所有文件,就直接git add .即可
  19.  
  20. #现在我们修改一个文件,查看有什么不同
  21. [root@localhost git1]# cat README
  22. #README
  23. [root@localhost git1]# git status -s
  24. AM README
  25. A hello.py
  26. #AM代表,我们将文件添加到缓存后又有了改动,我们再加一次
  27. [root@localhost git1]# git add README
  28. [root@localhost git1]# git status -s
  29. A README
  30. A hello.py #恢复了
  31.  
  32. ##git status
  33. #我们刚才用到了这个,status用来查看现在跟你上次提交后相比有没有修改,-s以获取最简单的结果输出,如果不加-s呢?
  34.  
  35. [root@localhost git1]# git status
  36. # On branch master
  37. #
  38. # Initial commit
  39. #
  40. # Changes to be committed:
  41. # (use "git rm --cached <file>..." to unstage)
  42. #
  43. # new file: README
  44. # new file: hello.py
  45. #
  46.  
  47. ##git diff
  48. #git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。
  49. #尚未缓存的改动:git diff
  50. #查看已缓存的改动: git diff --cached
  51. #查看已缓存的与未缓存的所有改动:git diff HEAD
  52. #显示摘要而非整个 diff:git diff --stat
  53.  
  54. [root@localhost git1]# echo "#python3" >>hello.py
  55. [root@localhost git1]# git status -s
  56. A README
  57. AM hello.py
  58. [root@localhost git1]# git diff
  59. diff --git a/hello.py b/hello.py
  60. index e69de29..564ae6e 100644
  61. --- a/hello.py
  62. +++ b/hello.py
  63. @@ -0,0 +1 @@
  64. +#python3
  65. #status是显示你上次提交更新后有没有再修改,diff是查看你修改了哪个文件的哪个地方
  66.  
  67. ##git commit
  68. #使用 git add 命令将想要快照的内容写入缓存区, 而执行 git commit 将缓存区内容添加到仓库中。这里快照就相当于一个版本号(tag),就跟你用软件一样,什么1.0,1.1,1.2,你提交的时候要写上你的大名,这样其它开发人员才知道这个版本是谁提交的,要责任到人,所以第一步就是这个
  69.  
  70. [root@localhost git1]# git config --global user.name 'Daniel'
  71. [root@localhost git1]# git config --global user.email Daniel@admin.com
  72. [root@localhost git1]# git add hello.py #提交hello.py的所有修改到缓存
  73. [root@localhost git1]# git status -s
  74. A README
  75. A hello.py
  76. [root@localhost git1]# git commit -m 'first1' #这里-m就是注释
  77. [master (root-commit) c009ef6] first1
  78. 2 files changed, 2 insertions(+), 0 deletions(-)
  79. create mode 100644 README
  80. create mode 100644 hello.py
  81.  
  82. [root@localhost git1]# git status
  83. # On branch master
  84. nothing to commit (working directory clean)
  85. #再次执行git status,代表我们在最近一次提交后没有什么改动,'working directory clean':干净的工作目录
  86. #如果你没有设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim,就像一下这样
  87.  
  88. # Please enter the commit message for your changes. Lines starting
  89. # with '#' will be ignored, and an empty message aborts the commit.
  90. # On branch master
  91. # Changes to be committed:
  92. # (use "git reset HEAD <file>..." to unstage)
  93. #
  94. # new file: a.txt
  95. #
  96. ~
  97.  
  98. #如果你觉得提交过程太麻烦,又要add,又要commit,你可以直接git commit -a
  99. [root@localhost git1]# echo "hello world" >> hello.py
  100. [root@localhost git1]# git commit -am 'first2'
  101. [master 0f755fd] first2
  102. 1 files changed, 1 insertions(+), 0 deletions(-)
  103.  
  104. ##git HEAD
  105. #git reset HEAD 命令用于取消已缓存的内容。
  106. [root@localhost git1]# echo "print 'hello world'" >> hello.py
  107. [root@localhost git1]# echo "#README2" >> README #修改两个文件
  108. [root@localhost git1]# git status -s #查看状态
  109. M README
  110. M hello.py
  111. [root@localhost git1]# git add . #提交
  112. [root@localhost git1]# git status -s #再次查看
  113. M README
  114. M hello.py
  115. [root@localhost git1]# git reset HEAD -- hello.py #取消hello.py的提交
  116. Unstaged changes after reset:
  117. M hello.py
  118. [root@localhost git1]# git status -s #发现不同
  119. M README
  120. M hello.py
  121.  
  122. [root@localhost git1]# git commit -m "first3"
  123. [master 8c8fc36] first3
  124. 1 files changed, 1 insertions(+), 0 deletions(-)
  125. [root@localhost git1]# git status -s
  126. M hello.py
  127. #这时候再次提交,发现hello.py并没有提交上去
  128. #简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。
  129.  
  130. ##git rm
  131. #要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交。可以用git rm <file>命令完成此项工作
  132. #如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f -- git rm -f <file>
  133. #如果把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可 -- git rm --cached <file>
  134.  
  135. [root@localhost git1]# touch test.txt
  136. [root@localhost git1]# git add test.txt
  137. [root@localhost git1]# git rm test.txt
  138. error: 'test.txt' has changes staged in the index
  139. (use --cached to keep the file, or -f to force removal) #这里我们删除不了,因为我们已经将新文件添加到了缓存区中
  140. [root@localhost git1]# git rm -f test.txt
  141. rm 'test.txt'
  142.  
  143. [root@localhost git1]# git rm --cached README
  144. rm 'README'
  145. [root@localhost git1]# ls
  146. hello.py README
  147. #不从工作区删除README
  148.  
  149. 可以递归删除,即如果后面跟的是一个目录做为参数,则会递归删除整个目录中的所有子目录和文件:
  150. git rm r *
  151.  
  152. ##git mv
  153. #用于重命名一个文件
  154. [root@localhost git1]# git add README #将刚才的README重新添加回来
  155. [root@localhost git1]# git mv README README.md
  156. [root@localhost git1]# ls
  157. hello.py README.md

  


git-day1-安装和基础使用的更多相关文章

  1. git&sourcetree安装及在IntelliIJ下拉取项目基础使用

    be careful: 1)git版本与Sourcetree版本最好一致 ,不能git为2.5,sourcetree为1.8 2)先安装git再安装Sourcetree 3)拥有git和sourcet ...

  2. Git的安装和使用教程详解

    ---恢复内容开始--- 本篇笔记聊聊Git的安装和使用教程 一.认 识 Git                                                            ...

  3. git使用---安装,提交,回退,修改,分支,标签等

    下面是对git的各种使用及命令的基础使用,来自廖雪峰老师的git教程,这个收录下,作为git的使用总结. github上面地址为:https://github.com/Zhangguoliu/lear ...

  4. Ubuntu下git的安装与使用

    Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...

  5. 4.Git的安装

    最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...

  6. git的安装以及遇到的问题

    git安装以及遇到的问题 之前没有学会如何在Ubuntu下使用git,国庆放假回来后,完成了git的安装,补回来了之前没有学会的东西. 以下是我安装的过程以及遇到问题.解决问题的过程. 这次安装git ...

  7. 20145321 Git的安装使用及今后学习规划

    20145321 Git的安装使用及今后学习规划 Git安装使用及解决遇到的问题 之前上传代码都没有按照老师的方法弄,当时看到git教程感觉很麻烦,于是都是写完之后再一个个 程序贴上去,而现在使用过后 ...

  8. Git版本控制工具(一)----git的安装及创建版本库

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  9. Mac上git的安装配置与使用简述

    Mac下git搭建及使用 之前就只是经常在GitHubs上下载代码,也没注意怎么上传项目.一开始对git都没什么了解花了几个小时去小补了下知识.如果有需要可以转去这里学习:[GIT使用简易指南] (h ...

  10. Windows下Git的安装及配置

    Git的BASH Git的为Windows提供了用于命令行运行的一个仿真BASH的Git.习惯LINUX和UNIX环境的你,可以在该BASH环境中输入“git”命令来完成各种版本控制的操作. 简介 G ...

随机推荐

  1. JS支持正则表达式的 String 对象的方法

    注意:本文中所有方法的 RegExp 类型的参数,其实都支持传入 String 类型的参数,JS会直接进行字符串匹配. (相当于用一个简单的非全局正则表达式进行匹配,但字符串并没有转换成 RegExp ...

  2. XPath语法简介

    XPath是一种在xml中查找信息的语言,具体可参考W3school XPath教程 XPath是以路径表达式来选择XML文档中的节点或节点集 === XPath节点(Node) 在 XPath 中, ...

  3. React.js 小书 Lesson24 - PropTypes 和组件参数验证

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson24 转载请注明出处,保留原文链接和作者信息. 我们来了到了一个非常尴尬的章节,很多初学的朋友 ...

  4. ORM框架SQLAlchemy的使用

    ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...

  5. JavaScript call 和apply 的理解

    这两个方法对于一些新手而言难耐弄清他们到底是怎么回事,对我我来讲我对call和apply的方法理解的也比较含糊.今天闲来无事准备彻底搞call和apply到底是怎么回事.本着互联网分享精神.我将我自己 ...

  6. Python学习之环境搭建及模块引用

    这是我学习Python过程积累的经验和踩过的坑,希望学习Python的新手们能尽量避免,以免不必要的时间浪费.今天也是我第一次接触Python. 基础语法看了两个晚上,所以如果没看的朋友们,抽时间先看 ...

  7. JDK12 concurrenthashmap源码阅读

           本文部分照片和代码分析来自文末参考资料        java8中的concurrenthashmap的方法逻辑和注解有些问题,建议看最新的JDK版本        建议阅读 concu ...

  8. Vue(五)--组件

    1.组件也需要进行注册才可以使用:分为局部注册和全局注册 <body> <div id="app" > <my-component></m ...

  9. Oracle PL/SQL Developer 上传下载Excel

    接到需求,Oracle数据库对Excel数据进行上传和下载,百度后没有很全的方案,整理搜到的资料,以备不时之需. 一.下载Oracle数据到Excel中. 下载数据到Excel在MSSql中很简单,直 ...

  10. jqGrid用法汇总(全经典)

    1.支持多种类型的数据集合作为数据源 $("#grid1").jqgrid( ........ datatype: "xml", ........ ); XML ...