zoukankan      html  css  js  c++  java
  • GIT

    1. GIT简介

    1.1 概述

      Git是一个开源的分布式版本控制系统,可以有效、高速的处理从很小到非常大的项目版本管理。 Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件。

      Git是分布式版本控制系统,那么它就没有中央服务器的,每个人的电脑就是一个完整的版本库,所以,工作的时候就不需要联网了,因为版本库都是在自己的电脑 上。现在每个人的电脑都有一个完整的版本库,那多个人如何协作呢?比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

      主要有如下特点:

        1> 版本控制;
        2> 分布式;

        3> 工作过程是将服务器上的代码下载到本地,本地开发完成后,在提交到服务器端;

      git相比于svn功能更加的强大,命令也很多。

    1.2和SVN的对比(面试)

      1> git是分布式的,svn是集中式的。(最核心)

      2> git是每个历史版本都存储完整的文件,便于恢复,svn是存储差异文件,历史版本不可恢复。(核心)

      3> git可离线完成大部分操作,svn则不能。

      4> git有着更优雅的分支和合并实现。

      5> git有着更强的撤销修改和修改历史版本的能力。

      6> git速度更快,效率更高。

      基于以上区别,git有了很明显的优势,特别在于它具有的本地仓库。

    1.3 Git的三个阶段

      1>工作目录

      工作目录是对项目的某个版本独立提取出来的内容。这些从 Git 仓库的压缩数据库中提取出来的文件,放在磁盘上供你使用或修改。即使用Git工作的时候创建的自己的工作目录。

      2> 暂存区域

      是一个文件,保存了下次将提交的文件列表信息,一般在 Git 仓库目录中。有时候也被称作“索引”,不过一般说法还是叫暂存区域。将工作目录的文件提交时先放在暂存区域。暂存区域可以进行两个操作:回滚和提交,即回滚到工作目录或者提交至Git仓库目录。

      3> Git 仓库目录

      是Git 用来保存项目的元数据和对象数据库的地方。这是 Git 中最重要的部分,从其它计算机克隆仓库时,拷贝的就是这里的数据。

    1.4 Git的工作流程

      在工作目录中修改文件 > 暂存文件,将文件的快照放入暂存区域 > 提交更新,找到暂存区域的文件,将快照永久性存储到 Git 仓库目录。

    如果 Git 目录中保存着的特定版本文件,就属于已提交状态。如果作了修改并已放入暂存区域,就属于已暂存状态。如果自上次取出后,作了修改但还没有放到暂存区域,就是已修改状态。

    2. Git的安装

      Git已经集成到linux,只需本地安装即可。

    [root@localhost ~]# mount /dev/cdrom /mnt
    mount: /dev/sr0 is write-protected, mounting read-only
    [root@localhost ~]# yum install git -y

    3. Git的使用

    Git的常用选项:

      add: 添加文件到暂存区域;

      branch: 显示分支和创建分支;

      checkout:切换分支以及回滚;

      clone: 克隆gitlab仓库;

      commit: 提交代码至仓库;

      init: 初始化工作目录;

      log: 显示历史版本信息;

      merge: 合并分支;

      pull: 拉取远程代码至本地;

      push: 推送本地仓库代码至远程仓库;

      reset: 回滚版本;

      status: 查看工作目录下的文本状态。

    [root@localhost work_git]# git
    usage: git [--version] [--help] [-c name=value]
    [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    [-p|--paginate|--no-pager] [--no-replace-objects] [--bare]
    [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    <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

    1> 创建Git工作目录

    [root@localhost ~]# mkdir work_git
    [root@localhost ~]# cd work_git/
    [root@localhost work_git]#

    2> 初始化目录

    [root@localhost work_git]# git init
    Initialized empty Git repository in /root/work_git/.git/
    [root@localhost work_git]# ls -a
    . .. .git                                   #生成隐藏文件,包含该文件的是git的工作目录,以后想在git目录下操作,需要到包含.git的目录下进行

    3> 创建文本

    [root@localhost work_git]# echo "content" >> zxj
    [root@localhost work_git]# cat zxj
    content

    4> 添加至暂存区

    [root@localhost work_git]# git add .       #.表示提交当前目录下的所有文件

    5> 提交至仓库

    [root@localhost work_git]# git commit -m "v1" #-m,massage,表名提交的信息,为v1版本,再根据提示信息进行注册
    [root@localhost work_git]# git commit -m "v1"
    [master (root-commit) 8f191dd] v1
    Committer: root <root@localhost.localdomain>
    Your name and email address were configured automatically based
    on your username and hostname. Please check that they are accurate.
    You can suppress this message by setting them explicitly:
    git config --global user.name "Your Name"
    git config --global user.email you@example.com
    After doing this, you may fix the identity used for this commit with:
    git commit --amend --reset-author
    1 file changed, 1 insertion(+)
    create mode 100644 zxj
    [root@localhost work_git]# git config --global user.name "Your Name"
    [root@localhost work_git]# git config --global user.email you@example.com
    [root@localhost work_git]# git commit -m "v1" #再次进行提交
    # On branch master
    nothing to commit, working directory clean
    [root@localhost work_git]# git log            #查看历史版本
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    6> 再次添加文本,提交至暂存区,提交至仓库,查看历史版本信息

    [root@localhost work_git]# echo "content" >> zxj
    [root@localhost work_git]# git add .
    [root@localhost work_git]# git commit -m "v2"
    [master 95425b4] v2
    1 file changed, 1 insertion(+)
    [root@localhost work_git]# git log
    commit 95425b40ff26e955520776162d2695b956a7d801
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 15:25:43 2019 +0800
    v2
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    7> 回滚版本至v1,复制最少commit码进行唯一标识进行回滚

    [root@localhost work_git]# git reset --hard 8f191ddf
    HEAD is now at 8f191dd v1
    [root@localhost work_git]# git log
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1
    [root@localhost work_git]# cat zxj
    content

    4. Git的撤销操作

    4.1 撤销工作区的内容

    1> 编辑已经提交的文本

    [root@localhost work_git]# echo "content" >> zxj

    2> 查看文本状态

    [root@localhost work_git]# git status
    # On branch master
    # Changes not staged for commit:
    # (use "git add <file>..." to update what will be committed)
    # (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified: zxj
    #
    no changes added to commit (use "git add" and/or "git commit -a")

    3> 撤销工作区的内容

    [root@localhost work_git]# git checkout -- zxj

    4> 查看工作区的状态

    [root@localhost work_git]# git status
    # On branch master
    nothing to commit, working directory clean
    [root@localhost work_git]# cat zxj
    content

    4.2 撤销暂存区的内容

    1> 编辑已经提交的文本

    [root@localhost work_git]# echo "content" >> zxj

    2> 提交文件至暂存区

    [root@localhost work_git]# echo "content" >> zxj
    [root@localhost work_git]# git add .

    3> 查看文本状态

    [root@localhost work_git]# git status
    # On branch master
    # Changes to be committed:
    # (use "git reset HEAD <file>..." to unstage)
    #
    # modified: zxj

    4> 撤销暂存区的文件至工作目录

    [root@localhost work_git]# git reset HEAD zxj
    Unstaged changes after reset:
    M zxj

    5> 撤销工作目录中的文件

    [root@localhost work_git]# git status
    # On branch master
    # Changes not staged for commit:
    # (use "git add <file>..." to update what will be committed)
    # (use "git checkout -- <file>..." to discard changes in working directory)
    #
    # modified: zxj
    #
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@localhost work_git]# git checkout -- zxj
    [root@localhost work_git]# git status
    # On branch master
    nothing to commit, working directory clean
    [root@localhost work_git]# cat zxj
    content

    4.3 版本控制

    1> 编辑文本文件

    [root@localhost work_git]# echo "content" >> zxj

    2> 提交至暂存区

    [root@localhost work_git]# git add zxj

    3> 提交暂存区文件

    [root@localhost work_git]# git commit -m "v3"
    [master cbb4e1f] v3
    1 file changed, 1 insertion(+)

    4> 查看历史版本信息

    [root@localhost work_git]# git log
    commit cbb4e1fde8bc68f32803a198ade9c7d353a81aff
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 16:04:41 2019 +0800
    v3
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    5> 版本回滚

      首先需要使用git log获取到提交点(commit)id,然后进行回滚。

    [root@localhost work_git]# git reset --hard 8f191ddf2e893df17e
    HEAD is now at 8f191dd v1

      查看版本信息

    [root@localhost work_git]# git log
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    6> 回退到v3

      使用reflog获取commit id

    [root@localhost work_git]# git reflog #前面的字符串就是commit id
    8f191dd HEAD@{0}: reset: moving to 8f191ddf2e893df17e
    cbb4e1f HEAD@{1}: commit: v3
    8f191dd HEAD@{2}: reset: moving to 8f191ddf
    95425b4 HEAD@{3}: commit: v2
    8f191dd HEAD@{4}: commit (initial): v1

      利用获得的commit id 回退到v3版本

    [root@localhost work_git]# git reset --hard cbb4e1f
    HEAD is now at cbb4e1f v3
    [root@localhost work_git]# git log
    commit cbb4e1fde8bc68f32803a198ade9c7d353a81aff
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 16:04:41 2019 +0800
    v3
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    5. Git分支介绍

      几乎所有的版本控制系统都以某种形式支持分支。 使用分支意味着可以把工作从开发主线上分离开来,以免影响开发主线。

      Git 处理分支的方式可谓是难以置信的轻量,创建新分支这一操作几乎能在瞬间完成,并且在不同分支之间的切换操作也是一样便捷。 与许多其它版本控制系统不同,Git 鼓励在工作流程中频繁地使用分支与合并,哪怕一天之内进行许多次。

    5.1 Git分支的使用

    1> 查看分支

    [root@localhost work_git]# git branch         #在一个已经提交的仓库中才有主分支的概念,未提交的仓库没有主分支的概念
    * master

    2> 创建分支

    格式:Git branch 分支名

    [root@localhost work_git]# git branch wrl
    [root@localhost work_git]# git branch
    * master
    wrl

    3> 切换分支

    格式:git heckout 分支名

    [root@localhost work_git]# git checkout wrl
    Switched to branch 'wrl'
    [root@localhost work_git]# git branch
    master
    * wrl

    4> 在wrl分支上操作文本

      切换分支后,所有的操作都在该分支下进行

    [root@localhost work_git]# ls
    zxj
    [root@localhost work_git]# cat zxj
    content
    content
    [root@localhost work_git]# echo "monkey" >> zxj
    [root@localhost work_git]# cat zxj
    content
    content
    monkey
    [root@localhost work_git]# git add .
    [root@localhost work_git]# git commit -m "v4"
    [wrl 465576c] v4
    1 file changed, 1 insertion(+)
    [root@localhost work_git]#

    5> 在wrl分支查看历史版本信息

    [root@localhost work_git]# git log
    commit 465576cb41d9c8df183826eb3fa0e5e13db13fc7
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 17:58:11 2019 +0800
    v4
    commit cbb4e1fde8bc68f32803a198ade9c7d353a81aff
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 16:04:41 2019 +0800
    v3
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    6> 切换至主分支

    [root@localhost work_git]# git checkout master
    Switched to branch 'master'
    [root@localhost work_git]# git branch
    * master
    wrl

    7> 查看主分支的版本信息,没有v4版本

    [root@localhost work_git]# git log
    commit cbb4e1fde8bc68f32803a198ade9c7d353a81aff
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 16:04:41 2019 +0800
    v3
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1

    8> 查看主分支的文本信息,并没有wrl分支添加的文本信息

    [root@localhost work_git]# cat zxj
    content
    content

    9> 合并分支

    格式: merge 分支名

    [root@localhost work_git]# git branch
    * master
    wrl
    [root@localhost work_git]# git merge wrl
    Updating cbb4e1f..465576c
    Fast-forward
    zxj | 1 +
    1 file changed, 1 insertion(+)

    10> 再次查看版本、文本信息,从分支wrl所做的改变都在

    [root@localhost work_git]# git branch
    * master                               #在主分支下
    wrl
    [root@localhost work_git]# git log
    commit 465576cb41d9c8df183826eb3fa0e5e13db13fc7
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 17:58:11 2019 +0800
    v4
    commit cbb4e1fde8bc68f32803a198ade9c7d353a81aff
    Author: Your Name <you@example.com>
    Date: Mon Jun 17 16:04:41 2019 +0800
    v3
    commit 8f191ddf2e893df17efdcf2cc4a80d76f0b8d5f0
    Author: root <root@localhost.localdomain>
    Date: Mon Jun 17 15:14:06 2019 +0800
    v1
    [root@localhost work_git]# cat zxj
    content
    content
    monkey

    总的来说:

      子分支上的内容并不会 影响主分支,只有在主分支进行合并子分支的时候才能看到子分支的内容,大大减轻了主分支开发的混乱和错误。

  • 相关阅读:
    C++ 课程设计——电梯调度系统
    PAT 1006 Sign In and Sign Out 查找元素
    PAT 1005 Spell It Right 字符串处理
    C++ 词汇表
    四六级分数竟是这样算出来的!交白卷都不会得零分 (转)
    以太坊智能合约部署
    如何设计一个通证经济体系?(转载)
    以太坊上那些暴力“利”的应用(转载)
    geth搭建以太坊私链及常用操作
    ubuntu18.04 安装 QQ
  • 原文地址:https://www.cnblogs.com/ajunyu/p/11042375.html
Copyright © 2011-2022 走看看