zoukankan      html  css  js  c++  java
  • Git学习

       先配置下本地yum环境

    [root@git-server ~]# cat /etc/yum.repos.d/CentOS-Base.repo 
    [666]
    name=666
    baseurl=file:///mnt
    gpgcheck=0
    enabled=1
    
    [root@git-server ~]# mount /dev/sr0 /mnt
    mount: block device /dev/sr0 is write-protected, mounting read-only
    [root@git-server ~]# 
    

      安装git,看到已经安装上了

    [root@git-server ~]# yum install git  -y
    Loaded plugins: fastestmirror, refresh-packagekit, security
    Setting up Install Process
    Loading mirror speeds from cached hostfile
    Package git-1.7.1-3.el6_4.1.x86_64 already installed and latest version
    Nothing to do
    [root@git-server ~]# 
    

      输入git查看一下命令用法

    [root@git-server ~]# git
    usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
               [-p|--paginate|--no-pager] [--no-replace-objects]
               [--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
               [--help] COMMAND [ARGS]
    
    The most commonly used git commands are:
       add        Add file contents to the index
       bisect     Find by binary search the change that introduced a bug
       branch     List, create, or delete branches
       checkout   Checkout a branch or paths to the working tree
       clone      Clone a repository into a new directory
       commit     Record changes to the repository
       diff       Show changes between commits, commit and working tree, etc
       fetch      Download objects and refs from another repository
       grep       Print lines matching a pattern
       init       Create an empty git repository or reinitialize an existing one
       log        Show commit logs
       merge      Join two or more development histories together
       mv         Move or rename a file, a directory, or a symlink
       pull       Fetch from and merge with another repository or a local branch
       push       Update remote refs along with associated objects
       rebase     Forward-port local commits to the updated upstream head
       reset      Reset current HEAD to the specified state
       rm         Remove files from the working tree and from the index
       show       Show various types of objects
       status     Show the working tree status
       tag        Create, list, delete or verify a tag object signed with GPG
    
    See 'git help COMMAND' for more information on a specific command.
    [root@git-server ~]# 
    

      

    设置用户名和邮箱,你以后提交代码的时候,别人可以知道用户名和邮箱,然后找到你

    [root@git-server ~]# git config --global user.name "zhangsan"
    [root@git-server ~]# git config --global user.email "zhangsan@163.com"
    [root@git-server ~]# git config --list
    user.name=zhangsan
    user.email=zhangsan@163.com
    [root@git-server ~]# 
    

      

    打开颜色支持

    [root@git-server ~]# git config --global color.ui true
    [root@git-server ~]# git config --list
    user.name=zhangsan
    user.email=zhangsan@163.com
    color.ui=true
    [root@git-server ~]# 
    

      

    创建一个目录,用于存放git

    git init 代表初始化的意思

    [root@git-server ~]# mkdir nmap
    [root@git-server ~]# cd nmap/
    [root@git-server nmap]# ll
    total 0
    [root@git-server nmap]# git init
    Initialized empty Git repository in /root/nmap/.git/
    [root@git-server nmap]# ll -al
    total 12
    drwxr-xr-x   3 root root 4096 Dec 20 17:12 .
    dr-xr-x---. 26 root root 4096 Dec 20 17:12 ..
    drwxr-xr-x   7 root root 4096 Dec 20 17:12 .git
    [root@git-server nmap]# 
    

      

    创建一个readme文件,此步骤可以省去,类似于使用手册,用前须知一样

    [root@git-server nmap]# vim readme.txt
    [root@git-server nmap]# cat readme.txt 
    #create by nmap 2016-12-20
    this is git home
    [root@git-server nmap]#
    

      

    输入命令git status,能看到下面readme.txt 带颜色,就是因为上面我们打开了color.ui,打开了颜色支持

    [root@git-server nmap]# git status
    # On branch master
    #
    # Initial commit
    #
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #	readme.txt
    nothing added to commit but untracked files present (use "git add" to track)
    [root@git-server nmap]# 
    

      

    执行git add  file,把它先添加到仓库了,然后再次执行git status,看到颜色变绿了,注意,这一步不是提交

    [root@git-server nmap]# git add readme.txt 
    [root@git-server nmap]# git status
    # On branch master
    #
    # Initial commit
    #
    # Changes to be committed:
    #   (use "git rm --cached <file>..." to unstage)
    #
    #	new file:   readme.txt
    #
    [root@git-server nmap]# 
    

    执行提交,-m后面跟的是此次提交的修改说明

    e250946  类似一个序列号

    [root@git-server nmap]# git commit -m "the first commit"
    [master (root-commit) e250946] the first commit
     1 files changed, 2 insertions(+), 0 deletions(-)
     create mode 100644 readme.txt
    [root@git-server nmap]# 
    

      

    再次执行git status 提示当前工作目录已经是干净状态,没有未提交的东西

    [root@git-server nmap]# git status
    # On branch master
    nothing to commit (working directory clean)
    [root@git-server nmap]# 
    

      

    再写一个文件练习下

    [root@git-server nmap]# vim deploy.sh
    [root@git-server nmap]# cat deploy.sh 
    #!/bin/bash
    echo hehe
    [root@git-server nmap]# git status
    # On branch master
    # Untracked files:
    #   (use "git add <file>..." to include in what will be committed)
    #
    #	deploy.sh
    nothing added to commit but untracked files present (use "git add" to track)
    [root@git-server nmap]# 
    

      

    提交

    [root@git-server nmap]# git add deploy.sh 
    [root@git-server nmap]# git commit -m "2th commit"
    [master 8a16518] 2th commit
     1 files changed, 2 insertions(+), 0 deletions(-)
     create mode 100644 deploy.sh
    [root@git-server nmap]# ll
    total 8
    -rw-r--r-- 1 root root 22 Dec 20 17:30 deploy.sh
    -rw-r--r-- 1 root root 44 Dec 20 17:14 readme.txt
    [root@git-server nmap]# 
    

      

    查看操作的历史记录,可以看到谁,邮箱是多少,什么时间,做了什么

    [root@git-server nmap]# git log
    commit 8a165184696a73c245fc07d156c9b1c4f4455ad2
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:33:04 2016 +0800
    
        2th commit
    
    commit e250946d3cf402c28cef9ab432fb778374f2002d
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:26:15 2016 +0800
    
        the first commit
    [root@git-server nmap]# 
    

      

    对一个文件修改,添加一行

    [root@git-server nmap]# echo hehe >>readme.txt 
    [root@git-server nmap]# cat readme.txt 
    #create by nmap 2016-12-20
    this is git home
    hehe
    [root@git-server nmap]# git status
    # On branch master
    # Changed but not updated:
    #   (use "git add <file>..." to update what will be committed)
    #   (use "git checkout -- <file>..." to discard changes in working directory)
    #
    #	modified:   readme.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@git-server nmap]# 
    

      

    git diff file 命令可以用来对比前后变化的内容

    [root@git-server nmap]# git diff readme.txt 
    diff --git a/readme.txt b/readme.txt
    index 943958e..d183b96 100644
    --- a/readme.txt
    +++ b/readme.txt
    @@ -1,2 +1,3 @@
     #create by nmap 2016-12-20
     this is git home
    +hehe
    [root@git-server nmap]# 
    

      

    添加文件,然后提交,查看历史记录

    [root@git-server nmap]# git add readme.txt 
    [root@git-server nmap]# git commit -m "add 2hehe"
    [master 4a0a533] add 2hehe
     1 files changed, 1 insertions(+), 0 deletions(-)
    [root@git-server nmap]# git log
    commit 4a0a533f45d6e2850a0cece2e94cb119801926c4
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:43:32 2016 +0800
    
        add 2hehe
    
    commit 8a165184696a73c245fc07d156c9b1c4f4455ad2
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:33:04 2016 +0800
    
        2th commit
    
    commit e250946d3cf402c28cef9ab432fb778374f2002d
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:26:15 2016 +0800
    
        the first commit
    ~
    ~
    ~
    ~
    [root@git-server nmap]# 
    

      

    假如上面修改错了,怎么回退呢,比如回退到上一个版本,HEAD^
    回退到上2个版本。HEAD^^
    回退到前3个版本。HEAD^^^

    [root@git-server nmap]# git reset --hard HEAD^
    HEAD is now at 8a16518 2th commit
    [root@git-server nmap]# cat readme.txt 
    #create by nmap 2016-12-20
    this is git home
    [root@git-server nmap]# 
    

      

    再次看下日志

    [root@git-server nmap]# git log
    commit 8a165184696a73c245fc07d156c9b1c4f4455ad2
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:33:04 2016 +0800
    
        2th commit
    
    commit e250946d3cf402c28cef9ab432fb778374f2002d
    Author: zhangsan <zhangsan@163.com>
    Date:   Tue Dec 20 17:26:15 2016 +0800
    
        the first commit
    [root@git-server nmap]# 
    

    怎么回退到指定版本呢,可以根据前面的序列号回退,这个是commit id 

    [root@git-server nmap]# git reflog
    8a16518 HEAD@{0}: HEAD^: updating HEAD
    4a0a533 HEAD@{1}: commit: add 2hehe
    8a16518 HEAD@{2}: commit: 2th commit
    e250946 HEAD@{3}: commit (initial): the first commit
    [root@git-server nmap]# git reset --hard e250946
    HEAD is now at e250946 the first commit
    [root@git-server nmap]# ll
    total 4
    -rw-r--r-- 1 root root 44 Dec 20 17:56 readme.txt
    [root@git-server nmap]# 
    

      

  • 相关阅读:
    mysql的IFNULL()函数FLOOR(),ROUND()函数
    何时有个内连接何时用外连接
    Mybatis中什么时候应该声明jdbcType
    FFPEG 转码记录------解决了有流,但是没有码率和FPS?
    HLS播放权限测试记录
    Redis-benchmark测试Redis性能
    JavaScript基础知识之——Location 对象详解
    Redis基础知识之—— 缓存应用场景
    Redis基础知识之—— hset 和hsetnx 的区别
    SAGE入门:开源数学系统之集大成者
  • 原文地址:https://www.cnblogs.com/nmap/p/6201635.html
Copyright © 2011-2022 走看看