zoukankan      html  css  js  c++  java
  • 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
    

      


  • 相关阅读:
    css问题
    前端性能优化整理
    算法之排序
    浅谈webpack优化
    js设计模式
    事件模型
    浏览器缓存
    ucGUI 12864 从打点起
    ucGUI例程收藏
    Qt 自动搜索串口号列表
  • 原文地址:https://www.cnblogs.com/wazy/p/8310984.html
Copyright © 2011-2022 走看看