Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341335
  • 博文数量: 52
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 577
  • 用 户 组: 普通用户
  • 注册时间: 2013-04-27 14:21
个人简介

知道自己该干嘛,知道自己能干嘛

文章分类

全部博文(52)

文章存档

2019年(1)

2018年(8)

2017年(2)

2016年(11)

2015年(3)

2014年(10)

2013年(17)

我的朋友

分类: 系统运维

2014-03-22 19:11:15


              近日一直再学习 git,现在把自己掌握的一些 git 常用命令,记录下来,以备不时之需。

              安装 git , 服务器已经部署 epel 源
  1.             yum install git-core    // yum安装git
  
              [root@git-env ~]# mkdir git-environment
              [root@git-env ~]# cd git-environment/
              [root@git-env git-environment]# echo "Hello world" > file
              [root@git-env git-environment]# git init    // 初始化一个本地仓库
              Initialized empty Git repository in /root/git-environment/.git/
              [root@git-env git-environment]# ls -a;ll .git/
              .  ..  file  .git
      
              drwxr-xr-x 2 root root 4096 Mar 23 15:24 branches
              -rw-r--r-- 1 root root   92    Mar 23 15:24 config
              -rw-r--r-- 1 root root   73    Mar 23 15:24 description
              -rw-r--r-- 1 root root   23    Mar 23 15:24 HEAD
              drwxr-xr-x 2 root root 4096 Mar 23 15:24 hooks
              drwxr-xr-x 2 root root 4096 Mar 23 15:24 info
              drwxr-xr-x 4 root root 4096 Mar 23 15:24 objects
              drwxr-xr-x 4 root root 4096 Mar 23 15:24 refs

             我们可以看到初始化后,本地多了一个.git文件夹,.git文件夹内保存的是
整个git项目所有的数据与资源
             我们在来看一下这些文件夹,具体保存了什么,和具体作用
 
             子目录名称                       简要描述
             branches                         Git 项目分支信息,新版 Git 已经不再使用该目录
             config                             Git 项目配置信息
             description                      Git 项目描述信息

             HEAD                              指向 Git 项目当前分支的头指针
             hooks                             默认的"hooks"脚本,被特定事件发生前后触发
             info                                里面含一个 exclude 文件,指 Git 项目要忽略的文件
             objects                           Git 的数据对象,包括:commits, trees, blobs, tags
             refs                                指向所有 Git 项目分支的指针

             创建版本库 
             git clone
             git init    // 初始化本地版本库
             git init --bare    // 更适用于远程公共库
             [root@git-env ~]# mkdir git-bare/
             [root@git-env ~]# cd git-bare/
             [root@git-env git-bare]# git init --bare
             Initialized empty Git repository in /root/git-bare/
             [root@git-env git-bare]# ls
             branches  config  description  HEAD  hooks  info  objects  refs     // 目录下仅有记录版本历史的文件
             [root@git-env git-bare]# echo "hello world" > file
             [root@git-env git-bare]# git add file 
             fatal: This operation must be run in a work tree    // bare(裸)库,再本地是无法操作的

            修改与提交
             [root@git-env ~]# mkdir git-environment
             [root@git-env ~]# cd git-environment/
             [root@git-env git-environment]# ls
             [root@git-env git-environment]# echo "hello world" > file
             [root@git-env git-environment]# git init        // 初始化环境
             Initialized empty Git repository in /root/git-environment/.git/
             [root@git-env git-environment]# echo "two" >> file
             [root@git-env git-environment]# git add file        //  添加修改的文件到索引中,如果添加很多文件可以用 git add .
             [root@git-env git-environment]# git commit -m "frist commit"    // 提交修改
             [master (root-commit) 989db21] frist commit
              1 files changed, 2 insertions(+), 0 deletions(-)
              create mode 100644 file
            [root@git-env git-environment]# echo "three" >> file
            [root@git-env git-environment]# cat file 
            hello world
            two
            three
            [root@git-env git-environment]# git diff    // 查看修改文件未添加到索引中,本地文件与上次commit 比对的结果
            diff --git a/file b/file
            index 8eb7ed2..f302393 100644
            --- a/file
            +++ b/file
            @@ -1,2 +1,3 @@
            hello world
            two
            +three
            [root@git-env git-environment]# git add file    // 添加文件至索引
            [root@git-env git-environment]# git diff --cached    // 查看当前索引与上次提交的差异
            diff --git a/file b/file
            index 8eb7ed2..f302393 100644
            --- a/file
           +++ b/file
           @@ -1,2 +1,3 @@
           hello world
           two
           +three
           [root@git-env git-environment]# git rm file --cached    // 删除索引中的文件
           rm 'file'
           [root@git-env git-environment]# git diff --cached   
           diff --git a/file b/file
           deleted file mode 100644
           index 8eb7ed2..0000000
           --- a/file
          +++ /dev/null
          @@ -1,2 +0,0 @@
          -hello world
          -two
         [root@git-env git-environment]# git status    // 当时work dir 目录的状态,可以看出我们删除了再索引中的跟踪文件
          # On branch master                                            // 然后 file 文件,变为了 Untracked 未跟踪
          # Changes to be committed:
          #   (use "git reset HEAD ..." to unstage)
          #
          #       deleted:    file
          #
          # Untracked files:
          #   (use "git add ..." to include in what will be committed)
          #
          #       file

          [root@git-env git-environment]# git add file     // 文件重新加入索引
          [root@git-env git-environment]# git diff --cached    // 比较索引中与上次提交之前的差异
            diff --git a/file b/file
            index 8eb7ed2..f302393 100644
            --- a/file
            +++ b/file
           @@ -1,2 +1,3 @@
           hello world
           two
           +three
           [root@git-env git-environment]# git status    // 现在的状态为 file 已修改, 等待提交
           # On branch master
           # Changes to be committed:
           #   (use "git reset HEAD ..." to unstage)
           #
           #       modified:   file
           #

           查看提交和历史
           [root@git-env git-environment]# git log    // 查看提交历史
            commit fe3fef2cd16f4c67f16e445e34d9a84a857266a3
            Author: cailu
            Date:   Mon Mar 24 16:59:02 2014 +0800

            three in

            commit 989db215e712e6ecff1da0cfae4e507437cad8c8
            Author: cailu
            Date:   Mon Mar 24 16:34:30 2014 +0800

            frist commit

           [root@git-env git-environment]# git log -p    // 查看提交历史的更新内容
            commit fe3fef2cd16f4c67f16e445e34d9a84a857266a3
            Author: cailu
            Date:   Mon Mar 24 16:59:02 2014 +0800

            three in

            diff --git a/file b/file
            index 8eb7ed2..f302393 100644
            --- a/file
            +++ b/file
            @@ -1,2 +1,3 @@
            hello world
            two
           +three

           commit 989db215e712e6ecff1da0cfae4e507437cad8c8
           Author: cailu
           Date:   Mon Mar 24 16:34:30 2014 +0800

           frist commit

           diff --git a/file b/file
           new file mode 100644
           index 0000000..8eb7ed2
           --- /dev/null
          +++ b/file
          @@ -0,0 +1,2 @@
          +hello world
          +two
 
         [root@git-env git-environment]# git blame file    // 以列表方式查看指定文件的提交历史
          ^989db21 (cailu 2014-03-24 16:34:30 +0800 1) hello world
          ^989db21 (cailu 2014-03-24 16:34:30 +0800 2) two
          fe3fef2c (cailu 2014-03-24 16:59:02 +0800 3) three

         撤销与重置
         [root@git-env git-environment]# cat file 
          hello world
          two
          three
          [root@git-env git-environment]# echo "four" >> file             
          [root@git-env git-environment]# echo "file" >> file    
          [root@git-env git-environment]# cat file 
          hello world
          two
          three
          four
          file
          [root@git-env git-environment]# git add .
          [root@git-env git-environment]# git reset --hard HEAD    //
重置未提交的工作目录到之前的提交后的状态
          HEAD is now at fe3fef2 three in
          [root@git-env git-environment]# cat file 
          hello world
          two
          three
         
         [root@git-env git-environment]# echo "three" >> one
          [root@git-env git-environment]# cat one 
          one
          three
          [root@git-env git-environment]# git checkout one    // 重置未提交的文件到之前提交后的状态,针对单一文件
          [root@git-env git-environment]# cat one 
          one


          [root@git-env git-environment]# echo "one" > file
          [root@git-env git-environment]# git add .
          [root@git-env git-environment]# git commit -m "one" 
          [master 2ff42cf] one
           1 files changed, 1 insertions(+), 5 deletions(-)
          [root@git-env git-environment]# ls
          file
          [root@git-env git-environment]# cat file 
          one
          [root@git-env git-environment]# echo "two" > file     
          [root@git-env git-environment]# git add .          
          [root@git-env git-environment]# git commit -m "two"
          [master 1278c9a] two
           1 files changed, 1 insertions(+), 1 deletions(-)
          [root@git-env git-environment]# cat file 
          two
          [root@git-env git-environment]# git blame file
          1278c9af (cailu 2014-03-24 17:41:04 +0800 1) two
         [root@git-env git-environment]# git revert -n 1278c9af    // 撤销hash值对应的操作,文件恢复为上一个提交的版本
          Finished one revert.
          [root@git-env git-environment]# cat file 
          one

          分支
          git branch    // 显示本地所有分支
          git checkout     // 切换分支
          git branch     // 创建一个新分支
          git branch -d     // 删除一个分支           





                                                                                                                 emar_Cail

          
        参考资料: 
                        http://www.ibm.com/developerworks/cn/opensource/os-cn-tourofgit/


阅读(1299) | 评论(0) | 转发(0) |
0

上一篇:Cobbler Web_UI

下一篇:git hook

给主人留下些什么吧!~~