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-安装和基础使用的更多相关文章

  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. PyQt5 应用在 TeamViewer 下无法使用全屏模式

    PyQt5 应用在 TeamViewer 下无法使用全屏模式 问题描述 使用 PyQt5 (版本为 5.7)中的 QtWebEngineView 构建的桌面应用,部署到远程机器(Windows 7 平 ...

  2. C#操作Redis String字符串

    /// <summary> /// Redis String 操作 /// </summary> public static void Redis_String() { Red ...

  3. html元素两种分类。替换元素和不可替换元素;块级元素和行内元素

    根据元素本身特点来分类: 替换元素替换元素根据其标签和属性来决定元素的具体显示内容.有<img><input><textarea><select>< ...

  4. 送专利啦~~ .Net高阶异常处理之TopLevelEH

    我们知道,.Net的应用程序运行在.net framework虚拟机上,对于在运行时发生的错误,我们有try...catch可以捕捉,实在不济,对于winform和asp.net 我们都有全局的事件可 ...

  5. Go.网络篇-2

    package main import ( "io/ioutil" "os" "io" "log" "net/ ...

  6. [android] 练习viewpagerindicator的使用(一)

    主要是学习一下使用这个库 activity_main.xml <?xml version="1.0" encoding="utf-8"?> < ...

  7. java实现Redis分布式锁

    网上到处都是分布式锁的代码,基本都是通过setNX 和 expire 这两个不是原子操作,肯定会有问题,不乏好多人通过用setNX的value当做过期时间来弥补等等.但是好像都不太好,或者多少有点问题 ...

  8. enter键触发事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. linux sheel script demo

    demo1 (输入/输出) 1.1. target : 输入姓.名, 输出姓名 1.2. create directory mkdir ~/bin 1.3. create & edit she ...

  10. replace的坑

    问题:html中代码段包含了$,在使用replace替换时,$直接被替换了解决:先把文本中的$全部替换成自己定义的标签,最后在还原回去原因:在介绍replace的文档中,$&代表插入匹配的子串 ...