git-day1-安装和基础使用
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哈希算法。这能确保代码内容的完整性,确保在遇到磁盘故障和网络问题时降低对版本库的破坏。
安装
- #这里只介绍centos的
- [root@localhost ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
- [root@localhost ~]# yum -y install git-core
- [root@localhost ~]# git --version
- git version 1.7.1
git工作流程
一般工作流程如下:
- 克隆 Git 资源作为工作目录。
- 在克隆的资源上添加或修改文件。
- 如果其他人修改了,你可以更新资源。
- 在提交前查看修改。
- 提交修改。
- 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交。
下图展示了 Git 的工作流程:
Git 创建仓库
- #git init
- #git init用于初始化一个git仓库,git的很多命令都在git仓库里运行。
- #执行完毕后,会在仓库根目录里生成一个.git目录(跟svn一样,会出现一个.svn目录),该目录包含了资源的所有元数据,其他的项目目录保持不变,这点跟svn有所不同,svn是其它子目录也会产生一个.svn目录
- [root@localhost /]# mkdir git1
- [root@localhost /]# cd git1
- [root@localhost git1]# git init #使用当前目录作为git的仓库,init初始化
- Initialized empty Git repository in /git1/.git/
- [root@localhost git1]# ls -al
- 总用量 12
- drwxr-xr-x 3 root root 4096 1月 25 06:02 .
- dr-xr-xr-x. 28 root root 4096 1月 25 06:02 ..
- drwxr-xr-x 7 root root 4096 1月 25 06:02 .git #出现了一个隐藏文件.git
- [root@localhost git1]# mkdir /git2
- [root@localhost git1]# git init /git2 #指定/git2目录作为git仓库
- Initialized empty Git repository in /git2/.git/
- [root@localhost git1]# ls -al /git2/
- 总用量 12
- drwxr-xr-x 3 root root 4096 1月 25 06:03 .
- dr-xr-xr-x. 29 root root 4096 1月 25 06:02 ..
- drwxr-xr-x 7 root root 4096 1月 25 06:03 .git
- #git clone
- #git clone从现有的git仓库中克隆项目到指定目录(跟svn的checkout一样),指定目录就是工作区
- #命令格式:git clone <repo>,克隆项目到指定目录:git clone <repo> <directory>,repo为git仓库,directory为指定目录
- [root@localhost /]# mkdir git_test1
- [root@localhost /]# cd /git_test1/
- [root@localhost git_test1]# git clone /git1/ #后面没有指定目录,则克隆到当前目录
- Initialized empty Git repository in /git_test1/git1/.git/
- warning: You appear to have cloned an empty repository.
- [root@localhost git_test1]# ls
- git1 #当前目录出现了一个.git1文件
- [root@localhost git_test1]# mkdir /git_test2
- [root@localhost git_test1]# git clone /git1/ /git_test2/gittest2 #指定一个目录为克隆目录,并且指定一个新名字
- Initialized empty Git repository in /git_test2/gittest2/.git/
- warning: You appear to have cloned an empty repository.
- [root@localhost git_test1]# ls /git_test2/
- gittest2
Git 基本操作
- ##基本快照
- #Git 的工作就是创建和保存你的项目的快照及与之后的快照进行对比,然后选择发布还是回滚,有点类似虚拟机的快照
- ##git add
- #git add用于将文件添加到缓存
- [root@localhost /]# cd /git_test1/git1/
- [root@localhost git1]# touch README
- [root@localhost git1]# touch hello.py
- [root@localhost git1]# git status -s
- ?? README
- ?? hello.py
- #git status 命令用于查看项目的当前状态。
- [root@localhost git1]# git add README
- [root@localhost git1]# git add hello.py
- [root@localhost git1]# git status -s
- A README
- A hello.py
- #如果想要添加所有文件,就直接git add .即可
- #现在我们修改一个文件,查看有什么不同
- [root@localhost git1]# cat README
- #README
- [root@localhost git1]# git status -s
- AM README
- A hello.py
- #AM代表,我们将文件添加到缓存后又有了改动,我们再加一次
- [root@localhost git1]# git add README
- [root@localhost git1]# git status -s
- A README
- A hello.py #恢复了
- ##git status
- #我们刚才用到了这个,status用来查看现在跟你上次提交后相比有没有修改,-s以获取最简单的结果输出,如果不加-s呢?
- [root@localhost git1]# git status
- # On branch master
- #
- # Initial commit
- #
- # Changes to be committed:
- # (use "git rm --cached <file>..." to unstage)
- #
- # new file: README
- # new file: hello.py
- #
- ##git diff
- #git diff 命令显示已写入缓存与已修改但尚未写入缓存的改动的区别。git diff 有两个主要的应用场景。
- #尚未缓存的改动:git diff
- #查看已缓存的改动: git diff --cached
- #查看已缓存的与未缓存的所有改动:git diff HEAD
- #显示摘要而非整个 diff:git diff --stat
- [root@localhost git1]# echo "#python3" >>hello.py
- [root@localhost git1]# git status -s
- A README
- AM hello.py
- [root@localhost git1]# git diff
- diff --git a/hello.py b/hello.py
- index e69de29..564ae6e 100644
- --- a/hello.py
- +++ b/hello.py
- @@ -0,0 +1 @@
- +#python3
- #status是显示你上次提交更新后有没有再修改,diff是查看你修改了哪个文件的哪个地方
- ##git commit
- #使用 git add 命令将想要快照的内容写入缓存区, 而执行 git commit 将缓存区内容添加到仓库中。这里快照就相当于一个版本号(tag),就跟你用软件一样,什么1.0,1.1,1.2,你提交的时候要写上你的大名,这样其它开发人员才知道这个版本是谁提交的,要责任到人,所以第一步就是这个
- [root@localhost git1]# git config --global user.name 'Daniel'
- [root@localhost git1]# git config --global user.email Daniel@admin.com
- [root@localhost git1]# git add hello.py #提交hello.py的所有修改到缓存
- [root@localhost git1]# git status -s
- A README
- A hello.py
- [root@localhost git1]# git commit -m 'first1' #这里-m就是注释
- [master (root-commit) c009ef6] first1
- 2 files changed, 2 insertions(+), 0 deletions(-)
- create mode 100644 README
- create mode 100644 hello.py
- [root@localhost git1]# git status
- # On branch master
- nothing to commit (working directory clean)
- #再次执行git status,代表我们在最近一次提交后没有什么改动,'working directory clean':干净的工作目录
- #如果你没有设置 -m 选项,Git 会尝试为你打开一个编辑器以填写提交信息。 如果 Git 在你对它的配置中找不到相关信息,默认会打开 vim,就像一下这样
- # Please enter the commit message for your changes. Lines starting
- # with '#' will be ignored, and an empty message aborts the commit.
- # On branch master
- # Changes to be committed:
- # (use "git reset HEAD <file>..." to unstage)
- #
- # new file: a.txt
- #
- ~
- #如果你觉得提交过程太麻烦,又要add,又要commit,你可以直接git commit -a
- [root@localhost git1]# echo "hello world" >> hello.py
- [root@localhost git1]# git commit -am 'first2'
- [master 0f755fd] first2
- 1 files changed, 1 insertions(+), 0 deletions(-)
- ##git HEAD
- #git reset HEAD 命令用于取消已缓存的内容。
- [root@localhost git1]# echo "print 'hello world'" >> hello.py
- [root@localhost git1]# echo "#README2" >> README #修改两个文件
- [root@localhost git1]# git status -s #查看状态
- M README
- M hello.py
- [root@localhost git1]# git add . #提交
- [root@localhost git1]# git status -s #再次查看
- M README
- M hello.py
- [root@localhost git1]# git reset HEAD -- hello.py #取消hello.py的提交
- Unstaged changes after reset:
- M hello.py
- [root@localhost git1]# git status -s #发现不同
- M README
- M hello.py
- [root@localhost git1]# git commit -m "first3"
- [master 8c8fc36] first3
- 1 files changed, 1 insertions(+), 0 deletions(-)
- [root@localhost git1]# git status -s
- M hello.py
- #这时候再次提交,发现hello.py并没有提交上去
- #简而言之,执行 git reset HEAD 以取消之前 git add 添加,但不希望包含在下一提交快照中的缓存。
- ##git rm
- #要从 Git 中移除某个文件,就必须要从已跟踪文件清单中移除,然后提交。可以用git rm <file>命令完成此项工作
- #如果删除之前修改过并且已经放到暂存区域的话,则必须要用强制删除选项 -f -- git rm -f <file>
- #如果把文件从暂存区域移除,但仍然希望保留在当前工作目录中,换句话说,仅是从跟踪清单中删除,使用 --cached 选项即可 -- git rm --cached <file>
- [root@localhost git1]# touch test.txt
- [root@localhost git1]# git add test.txt
- [root@localhost git1]# git rm test.txt
- error: 'test.txt' has changes staged in the index
- (use --cached to keep the file, or -f to force removal) #这里我们删除不了,因为我们已经将新文件添加到了缓存区中
- [root@localhost git1]# git rm -f test.txt
- rm 'test.txt'
- [root@localhost git1]# git rm --cached README
- rm 'README'
- [root@localhost git1]# ls
- hello.py README
- #不从工作区删除README
- 可以递归删除,即如果后面跟的是一个目录做为参数,则会递归删除整个目录中的所有子目录和文件:
- git rm –r *
- ##git mv
- #用于重命名一个文件
- [root@localhost git1]# git add README #将刚才的README重新添加回来
- [root@localhost git1]# git mv README README.md
- [root@localhost git1]# ls
- hello.py README.md
git-day1-安装和基础使用的更多相关文章
- git&sourcetree安装及在IntelliIJ下拉取项目基础使用
be careful: 1)git版本与Sourcetree版本最好一致 ,不能git为2.5,sourcetree为1.8 2)先安装git再安装Sourcetree 3)拥有git和sourcet ...
- Git的安装和使用教程详解
---恢复内容开始--- 本篇笔记聊聊Git的安装和使用教程 一.认 识 Git ...
- git使用---安装,提交,回退,修改,分支,标签等
下面是对git的各种使用及命令的基础使用,来自廖雪峰老师的git教程,这个收录下,作为git的使用总结. github上面地址为:https://github.com/Zhangguoliu/lear ...
- Ubuntu下git的安装与使用
Ubuntu下git的安装与使用 Ubuntu下git的安装与使用与Windows下的大致相同,只不过个人感觉在Ubuntu下使用git更方便. 首先,确认你的系统是否已安装git,可以通过git指令 ...
- 4.Git的安装
最早Git是在Linux上开发的,很长一段时间内,Git也只能在Linux和Unix系统上跑.不过,慢慢地有人把它移植到了Windows上.现在,Git可以在Linux.Unix.Mac和Window ...
- git的安装以及遇到的问题
git安装以及遇到的问题 之前没有学会如何在Ubuntu下使用git,国庆放假回来后,完成了git的安装,补回来了之前没有学会的东西. 以下是我安装的过程以及遇到问题.解决问题的过程. 这次安装git ...
- 20145321 Git的安装使用及今后学习规划
20145321 Git的安装使用及今后学习规划 Git安装使用及解决遇到的问题 之前上传代码都没有按照老师的方法弄,当时看到git教程感觉很麻烦,于是都是写完之后再一个个 程序贴上去,而现在使用过后 ...
- Git版本控制工具(一)----git的安装及创建版本库
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Mac上git的安装配置与使用简述
Mac下git搭建及使用 之前就只是经常在GitHubs上下载代码,也没注意怎么上传项目.一开始对git都没什么了解花了几个小时去小补了下知识.如果有需要可以转去这里学习:[GIT使用简易指南] (h ...
- Windows下Git的安装及配置
Git的BASH Git的为Windows提供了用于命令行运行的一个仿真BASH的Git.习惯LINUX和UNIX环境的你,可以在该BASH环境中输入“git”命令来完成各种版本控制的操作. 简介 G ...
随机推荐
- JS支持正则表达式的 String 对象的方法
注意:本文中所有方法的 RegExp 类型的参数,其实都支持传入 String 类型的参数,JS会直接进行字符串匹配. (相当于用一个简单的非全局正则表达式进行匹配,但字符串并没有转换成 RegExp ...
- XPath语法简介
XPath是一种在xml中查找信息的语言,具体可参考W3school XPath教程 XPath是以路径表达式来选择XML文档中的节点或节点集 === XPath节点(Node) 在 XPath 中, ...
- React.js 小书 Lesson24 - PropTypes 和组件参数验证
作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson24 转载请注明出处,保留原文链接和作者信息. 我们来了到了一个非常尴尬的章节,很多初学的朋友 ...
- ORM框架SQLAlchemy的使用
ORM和SQLAlchemy简介 对象关系映射(Object Relational Mapping,简称ORM),简单的来说,ORM是将数据库中的表与面向对象语言中的类建立了一种对应的关系.然后我们操 ...
- JavaScript call 和apply 的理解
这两个方法对于一些新手而言难耐弄清他们到底是怎么回事,对我我来讲我对call和apply的方法理解的也比较含糊.今天闲来无事准备彻底搞call和apply到底是怎么回事.本着互联网分享精神.我将我自己 ...
- Python学习之环境搭建及模块引用
这是我学习Python过程积累的经验和踩过的坑,希望学习Python的新手们能尽量避免,以免不必要的时间浪费.今天也是我第一次接触Python. 基础语法看了两个晚上,所以如果没看的朋友们,抽时间先看 ...
- JDK12 concurrenthashmap源码阅读
本文部分照片和代码分析来自文末参考资料 java8中的concurrenthashmap的方法逻辑和注解有些问题,建议看最新的JDK版本 建议阅读 concu ...
- Vue(五)--组件
1.组件也需要进行注册才可以使用:分为局部注册和全局注册 <body> <div id="app" > <my-component></m ...
- Oracle PL/SQL Developer 上传下载Excel
接到需求,Oracle数据库对Excel数据进行上传和下载,百度后没有很全的方案,整理搜到的资料,以备不时之需. 一.下载Oracle数据到Excel中. 下载数据到Excel在MSSql中很简单,直 ...
- jqGrid用法汇总(全经典)
1.支持多种类型的数据集合作为数据源 $("#grid1").jqgrid( ........ datatype: "xml", ........ ); XML ...