• Git分布式版本控制系统


    Git分布式版本控制系统

        git remote -v 查看仓库

     企业高效持续集成平台场景介绍:

         

    二,GIT分布式版本控制系统

    2.1 Git简介

    2.1.1 git是什么?

    • GitWikipedia上的定义:它是一个免费的、分布式的版本控制工具,或是一个强调了速度快的源代码管理工具。Git最初被Linus Torvalds开发出来用于管理Linux内核的开发。每一个Git的工作目录都是一个完全独立的代码库,并拥有完整的历史记录和版本追踪能力,不依赖 于网络和中心服务器。
    • Git的出现减轻了许多开发者和开源项目对于管理分支代码的压力,由于对分支的良好控制,更鼓励开发者对自己感兴趣的项目做出贡献。其实许多开源项目 包括Linux kernel, Samba, X.org Server, Ruby on Rails,都已经过渡到使用Git作为自己的版本控制工具。对于我们这些喜欢写代码的开发者嘛,有两点最大的好处,我们可以在任何地点(在上班的地铁 上)提交自己的代码和查看代码版本;我们可以开许许多多个分支来实践我们的想法,而合并这些分支的开销几乎可以忽略不计。

    2.1.2 什么是版本控制呢?

    本地版本控制系统:

    • 许多人习惯用复制整个项目目录的方式来保存不同的版本,或许还会改名加上备份时间以示区别。这么做唯一的好处就是简单。不过坏处也不少:有时候会混淆所在的工作目录,一旦弄错文件丢了数据就没法撤销恢复。
    • 为了解决这个问题,人们很久以前就开发了许多种本地版本控制系统,大多都是采用某种简单的数据库来记录文件的历次更新差异

            

    • 其中最流行的一种叫做 rcs,现今许多计算机系统上都还看得到它的踪影。甚至在流行的 Mac OS X 系统上安装了开发者工具包之后,也可以使用 rcs 命令。它的工作原理基本上就是保存并管理文件补丁(patch)。文件补丁是一种特定格式的文本文件,记录着对应文件修订前后的内容变化。所以,根据每次 修订后的补丁,rcs 可以通过不断打补丁,计算出各个版本的文件内容。

    集中化的版本控制系统:

    • 接下来人们又遇到一个问题,如何让在不同系统上的开发者协同工作?于是,集中化的版本控制系统( Centralized Version Control Systems,简称 CVCS )应运而生。这类系统,诸如 CVS,Subversion 以及 Perforce 等,都有一个单一的集中管理的服务器,保存所有文件的修订版本,而协同工作的人们都通过客户端连到这台服务器,取出最新的文件或者提交更新。多年以来,这 已成为版本控制系统的标准做法

     

    • 这种做法带来了许多好处,特别是相较于老式的本地 VCS来说。现在,每个人都可以在一定程度上看到项目中的其他人正在做些什么。而管理员也可以轻松掌控每个开发者的权限,并且管理一个 CVCS 要远比在各个客户端上维护本地数据库来得轻松容易。
    • 事分两面,有好有坏。这么做最显而易见的缺点是中央服务器的单点故障。如果宕机一小时,那么在这一小时内,谁都无法提交更新,也就无法协同工作。要 是中央服务器的磁盘发生故障,碰巧没做备份,或者备份不够及时,就还是会有丢失数据的风险。最坏的情况是彻底丢失整个项目的所有历史更改记录,而被客户端 提取出来的某些快照数据除外,但这样的话依然是个问题,你不能保证所有的数据都已经有人事先完整提取出来过。本地版本控制系统也存在类似问题,只要整个项 目的历史记录被保存在单一位置,就有丢失所有历史更新记录的风险。

    分布式版本控制系统:

    • 于是分布式版本控制系统( Distributed Version Control System,简称 DVCS )面世了。在这类系统中,像 Git,Mercurial,Bazaar 以及 Darcs 等,客户端并不只提取最新版本的文件快照,而是把原始的代码仓库完整地镜像下来。这么一来,任何一处协同工作用的服务器发生故障,事后都可以用任何一个镜 像出来的本地仓库恢复。因为每一次的提取操作,实际上都是一次对代码仓库的完整备份

     

    • 更进一步,许多这类系统都可以指定和若干不同的远端代码仓库进行交互。籍此,你就可以在同一个项目中,分别和不同工作小组的人相互协作。你可以根据需要设定不同的协作流程,比如层次模型式的工作流,而这在以前的集中式系统中是无法实现的。

    2.1.3 git的优势在哪?

    与同类型版本控制软件:svncvs 
    SVN===>集中式版本控制系统 
    GIT===>分布式版本控制系统

     

    实际的例子:

    • 以前我所 在的小组使用SVN作为版本控制工具,当我正在试图增强一个模块,工作做到一半,由于会改变原模块的行为导致代码服务器上许多测试的失败,所以并没有提交 代码。这时候上级对我说,现在有一个很紧急的Bug需要处理, 必须在两个小时内完成。我只好将本地的所有修改diff,并输出成为一个patch文件,然后回滚有关当前任务的所有代码,再开始修改Bug的任务,等到 修改好后,在将patch应用回来。前前后后要完成多个繁琐的步骤,这还不计中间代码发生冲突所要进行的工作量。
    • 可是如果使用Git, 我们只需要开一个分支或者转回到主分支上,就可以随时开始Bug修改的任务,完成之后,只要切换到原来的分支就可以优雅的继续以前的任务。只要你愿意,每 一个新的任务都可以开一个分支,完成后,再将它合并到主分支上,轻松而优雅。
    • 因此,分布式的版本控制系统,在每个使用者电脑上就有一个完整的数据仓库,没有网络依然可以使用Git。当然为了习惯及团队协作,会将本地数据同步到Git服务器或者GitHub等远程代码仓库

    https://github.com

     

    2.2 Git的安装

    2.2.1 Windows安装git客户端

    客户端下载地址:https://www.git-scm.com/downloads

    双击安装包一路下一步即可。

    在桌面上创建一个空目录,右键点击目录

     

    选择Git Bash Here,进入git命令界面

     

    到此windowsgit客户端就安装好了

    2.2.2 Linux利用yum安装git客户端

    1. #安装环境查看
    2. [root@Git01 ~]# cat /etc/redhat-release
    3. CentOS Linux release 7.5.1804 (Core) 
    4. [root@Git01 ~]# uname -r
    5. 3.10.0-862.3.3.el7.x86_64
    6. #安装git客户端
    7. [root@Git01 ~]# yum -y install git
    8. [root@Git01 ~]# which git
    9. /usr/bin/git
    10. [root@Git01 ~]# git --version
    11. git version 1.8.3.1
    12. #Git全局配置
    13. [root@Git02 ~]# git config --global user.name "Mr.ke" #配置git使用用户
    14. [root@Git02 ~]# git config --global user.email "1140180652@qq.com"   #配置git使用邮箱
    15. [root@Git02 ~]# git config --global color.ui true   #语法高亮
    16. [root@Git02 ~]# git config --list   #查看全局配置
    17. user.email=1140180652@qq.com
    18. user.name=Mr.ke
    19. color.ui=true
    20. 说明:
    21. 如果没有提前设置Git的全局配置,那么在第一次进行代码提交的时候,会要求输入使用者的邮箱和姓名

    到此利用Yum安装Linux操作系统的git客户端就安装好了

    2.2.3 Linux源码安装git客户端:

    如果我们想要安装最新版本的git,那么就只能源码包安装了:

    1. #回退之前的yum安装
    2. [root@Git01 ~]# yum history
    3. 已加载插件:fastestmirror
    4. ID     | 登录用户                 | 日期和时间       | 操作           | 变更数 
    5. -------------------------------------------------------------------------------
    6. 6 | root <root>              | 2018-09-11 21:30 | Install        |    6   
    7. 5 | root <root>              | 2018-07-02 20:08 | I, U           |   60   
    8. 4 | root <root>              | 2018-07-03 03:54 | Install        |   93   
    9. 3 | root <root>              | 2018-07-03 03:53 | Install        |    1   
    10. 2 | root <root>              | 2018-07-03 03:53 | Install        |    1   
    11. 1 | 系统 <空>                | 2018-07-03 03:46 | Install        |  313   
    12. [root@Git01 ~]# yum history undo 6
    13. #源码安装git-2.9.5.tar.gz
    14. [root@Git01 ~]# yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel
    15. [root@Git01 ~]# yum -y install gcc perl-ExtUtils-MakeMaker
    16. [root@Git01 ~]# tar xf git-2.9.5.tar.gz -C /usr/src/
    17. [root@Git01 ~]# cd /usr/src/git-2.9.5/
    18. [root@Git01 git-2.9.5]# ./configure --prefix=/usr/local/git
    19. [root@Git01 git-2.9.5]# make && make install
    20. [root@Git01 ~]# ln -sf /usr/local/git/bin/* /usr/bin/
    21. [root@Git01 ~]# which git
    22. /usr/bin/git
    23. #源码编译需要链接git的命令库
    24. [root@Git01 ~]# ln -s /usr/libexec/git-core/* /usr/bin/
    25. [root@Git01 ~]# git --version
    26. git version 2.9.5

    至此,利用源码包安装Linux操作系统的git客户端就安装好了:

    2.3 Git的命令入门

    • 工作区 
      • 你建立的本地git项目目录
      • 暂存区 
        • 将工作区里的变更部分(与上一版本不同的部分)暂时存储起来的地方
      • 本地仓库 
        • 在本地创建的git版本仓库,用于提交工作区的变更。
      • 远程仓库 
        • githab,gitlab或者队友机器上所建立的一个仓库

     

    主机名

    IP

    备注

    Git01

    192.168.200.186

    git测试客户端一

    Git02

    192.168.200.187

    git测试客户端二

    2.3.1 git帮助文档:

    1. [root@Git ~]# git
    2. usage: git [--version] [--help] [-C <path>] [-c name=value]
    3. [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
    4. [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
    5. [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
    6. <command> [<args>]
    7. 这些是各种场合常见的 Git 命令:
    8. 开始一个工作区(参见:git help tutorial)
    9. clone      克隆一个仓库到一个新目录
    10. init       创建一个空的 Git 仓库或重新初始化一个已存在的仓库
    11. 在当前变更上工作(参见:git help everyday)
    12. add        添加文件内容至索引
    13. mv         移动或重命名一个文件、目录或符号链接
    14. reset      重置当前 HEAD 到指定状态
    15. rm         从工作区和索引中删除文件
    16. 检查历史和状态(参见:git help revisions)
    17. bisect     通过二分查找定位引入 bug 的提交
    18. grep       输出和模式匹配的行
    19. log        显示提交日志
    20. show       显示各种类型的对象
    21. status     显示工作区状态
    22. 扩展、标记和调校您的历史记录
    23. branch     列出、创建或删除分支
    24. checkout   切换分支或恢复工作区文件
    25. commit     记录变更到仓库
    26. diff       显示提交之间、提交和工作区之间等的差异
    27. merge      合并两个或更多开发历史
    28. rebase     在另一个分支上重新应用提交
    29. tag        创建、列出、删除或校验一个 GPG 签名的标签对象
    30. 协同(参见:git help workflows)
    31. fetch      从另外一个仓库下载对象和引用
    32. pull       获取并整合另外的仓库或一个本地分支
    33. push       更新远程引用和相关的对象
    34. 命令 'git help -a' 和 'git help -g' 显示可用的子命令和一些概念帮助。
    35. 查看 'git help <命令>' 或 'git help <概念>' 以获取给定子命令或概念的
    36. 帮助。

    export LANG=zh_CN.UTF8

    2.3.2 git init初始化GIT工作目录

    1. #在Linux上
    2. [root@Git01 ~]# mkdir -p /mycode
    3. [root@Git01 ~]# cd /mycode
    4. [root@Git01 mycode]# git init
    5. 初始化空的 Git 仓库于 /mycode/.git/
    6. [root@Git01 mycode]# ls
    7. [root@Git01 mycode]# ls -la
    8. 总用量 0
    9. drwxr-xr-x   3 root root  18 9月  11 23:55 .
    10. dr-xr-xr-x. 18 root root 238 9月  11 23:55 ..
    11. drwxr-xr-x  7 root root 119 9月  11 23:55 .git

    2.3.3 git add将变更添加进入暂存区

     

    1. #在Linux上
    2. [root@Git01 mycode]# touch test.txt
    3. [root@Git01 mycode]# git add test.txt
    4. #查看git工作目录的暂存区状态
    5. [root@Git01 mycode]# git status
    6. 位于分支 master
    7. 您的分支与上游分支 'test/master' 一致。
    8. 要提交的变更:
    9. (使用 "git reset HEAD <文件>..." 以取消暂存)
    10. 修改:     test.txt

    2.3.4 git commit将变更从暂存区提交到本地仓库

    1. #在linux上
    2. [root@Git01 mycode]# git commit -m "test"
    3. *** Please tell me who you are.         #请告诉我你是谁
    4. Run #运行
    5. git config --global user.email "you@example.com"  #运行命令告诉git你的邮箱
    6. git config --global user.name "Your Name"         #运行命令告诉git你的名字
    7. to set your accounts default identity.
    8. Omit --global to set the identity only in this repository.
    9. fatal: unable to auto-detect email address (got 'root@Git.(none)')
    10. 说明:
    11. 因为我们安装git时,并没有注册自己的个人信息。但是当变更的代码初次提交到本地仓库上时是需要注册你的个人信息的。因此,我们执行这两条命令注册自己的个人信息
    12. #注册自己的个人信息
    13. [root@Git01 mycode]# git config --global user.email "215379068@qq.com"
    14. [root@Git01 mycode]# git config --global user.name "Mr.chen"
    15. #再次尝试提交
    16. [root@Git01 mycode]# git commit -m "test"
    17. [master(根提交) 6bff4b4] test
    18. 1 file changed, 0 insertions(+), 0 deletions(-)
    19. create mode 100644 test.txt

    2.3.5 创建github账号,并创建个人github远程仓库

     

    创建完github账号后,我们就可以创建远程仓库了,如下图所示:

    https://github.com

     

    (我的仓库地址:

    http方式地址:  https://github.com/zhiqifeiyang/number_one.git

    Sshf方式地址:  https://github.com/zhiqifeiyang/number_one.git

    ) 

    2.3.6 git remote用于管理远程仓库

     git remote -v 查看所有远程查看

    1. [root@Git mycode]# git remote add --help
    2. 用法:git remote add [<选项>] <名称> <地址>
    3. -f, --fetch           抓取远程的分支
    4. --tags                抓取时导入所有的标签和关联对象
    5. 或不抓取任何标签(--no-tags)
    6. -t, --track <分支>    跟踪的分支
    7. -m, --master <分支>   主线分支
    8. --mirror[=<push|fetch>]
    9. 把远程设置为用以推送或抓取的镜像

    (1)git remote add 把本地仓库添加到远程仓库

    git remote add 代号 url

    添加一个远程仓库的URL 
    命令格式:git remote add <仓库的名字> <仓库的URL>

    1. #在Linux上
    2. [root@Git01 mycode]# git remote add test https://github.com/yinsendemogui/yunjisuan.git

    2.3.7 git push将本地仓库的变更推送到远程仓库的某个分支

    命令格式: 
    git push -u <远程仓库的名字> <远程仓库的某一分支名字>

    1. #在Linux上推送本地仓库变更到远程仓库的master分支
    2. [root@Git01 mycode]# git push -u test master   

    git远程仓库查看如下:

     

    扩展如若出现报错如下:

    1. fatal: Unable to find remote helper for 'https' 
    2. 说明:
    3. 这是因为我们在源码编译的时候,git功能没有安装全。
    4. 环境变量PATH找不到/usr/libexec/git-core目录
    5. #我们添加一个软连接
    6. [root@Git01 mycode]# ln -s /usr/libexec/git-core/* /usr/bin/
    7. #再次推送本地仓库的变更
    8. [root@Git01 mycode]# git push -u test master
    9. Username for 'https://github.com': 215379068@qq.com #第一次推送需要输入登陆账号和密码
    10. Password for 'https://215379068@qq.com@github.com':
    11. 对象计数中: 3, 完成.
    12. 写入对象中: 100% (3/3), 201 bytes | 0 bytes/s, 完成.
    13. Total 3 (delta 0), reused 0 (delta 0)
    14. remote:
    15. remote: Create a pull request for 'master' on GitHub by visiting:
    16. remote:      https://github.com/yinsendemogui/yunjisuan/pull/new/master
    17. remote:
    18. To https://github.com/yinsendemogui/yunjisuan.git
    19. * [new branch]      master -> master
    20. 分支 master 设置为跟踪来自 test 的远程分支 master。

    再次在浏览器进行访问查看你的github地址: https://github.com

     

    2.3.8 git clone克隆一个现有仓库到本地

    我们在另一台Git02上来模拟其他客户端进行对远程仓库克隆到本地仓库的操作

    1. [root@Git02 ~]# yum -y install git
    2. [root@Git02 ~]# mkdir -p /mycode2
    3. [root@Git02 ~]# cd /mycode2
    4. #初始化GIT工作目录并将已有远程仓库克隆到本地
    5. [root@Git01 mycode2]# git init
    6. 初始化空的 Git 仓库于 /mycode2/.git/
    7. [root@Git02 mycode2]# git clone https://github.com/yinsendemogui/yunjisuan.git
    8. 正克隆到 'yunjisuan'...
    9. remote: Counting objects: 3, done.
    10. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
    11. Unpacking objects: 100% (3/3), done.
    12. [root@Git02 mycode2]# tree
    13. .
    14. └── yunjisuan
    15. └── test.txt
    16. 1 directory, 1 file
    17. #修改仓库里的文件内容,并提交变更到本地,推送变更到远程仓库
    18. [root@Git02 mycode2]# echo "welcome" >> yunjisuan/test.txt
    19. [root@Git02 mycode2]# cd yunjisuan/
    20. #将变更加入缓存区
    21. [root@Git02 yunjisuan]# git add test.txt
    22. #将缓存区的变更提交到本地仓库
    23. [root@Git02 yunjisuan]# git commit -m "修改了test.txt"
    24. [master 40f3e3d] 修改了test.txt
    25. 1 file changed, 2 insertions(+)
    26. #由于我们并没有添加对远程仓库的管理,所以我们要直接推送远程仓库的URL
    27. [root@Git02 yunjisuan]# git push https://github.com/yinsendemogui/yunjisuan.git master
    28. Username for 'https://github.com': 215379068@qq.com
    29. Password for 'https://215379068@qq.com@github.com': 
    30. Counting objects: 5, done.
    31. Writing objects: 100% (3/3), 271 bytes | 0 bytes/s, done.
    32. Total 3 (delta 0), reused 0 (delta 0)
    33. To https://github.com/yinsendemogui/yunjisuan.git
    34. 6bff4b4..40f3e3d  master -> master
    35. #添加对远程仓库的管理
    36. [root@Git02 yunjisuan]# git remote add test https://github.com/yinsendemogui/yunjisuan.git
    37. #查看所有已经纳入git管理的远程仓库URL
    38. [root@Git02 yunjisuan]# git remote -v
    39. origin  https://github.com/yinsendemogui/yunjisuan.git (fetch)  #clone时自动添加
    40. origin  https://github.com/yinsendemogui/yunjisuan.git (push)   #clone时自动添加
    41. test    https://github.com/yinsendemogui/yunjisuan.git (fetch)  #新添加的
    42. test    https://github.com/yinsendemogui/yunjisuan.git (push)   #新添加的
    43. #再次改动test.txt文件并将变更提交到远程github仓库
    44. [root@Git02 yunjisuan]# echo "wwww" >> test.txt
    45. [root@Git02 yunjisuan]# git add test.txt
    46. [root@Git02 yunjisuan]# git commit -m "再次改动一次test.txt"
    47. [master cb1cf08] 再次改动一次test.txt
    48. 1 file changed, 1 insertion(+)
    49. [root@Git02 yunjisuan]# git push test master
    50. Username for 'https://github.com': 215379068@qq.com
    51. Password for 'https://215379068@qq.com@github.com': 
    52. Counting objects: 5, done.
    53. Writing objects: 100% (3/3), 287 bytes | 0 bytes/s, done.
    54. Total 3 (delta 0), reused 0 (delta 0)
    55. To https://github.com/yinsendemogui/yunjisuan.git
    56. 40f3e3d..cb1cf08  master -> master

    浏览器打开github查看变更提交情况

     

    2.3.9 git fetch 将远程仓库的变更拉取到本地仓库

    1. #在Git01上
    2. [root@Git01 mycode]# git fetch
    3. remote: Counting objects: 6, done.
    4. remote: Compressing objects: 100% (2/2), done.
    5. remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
    6. 展开对象中: 100% (6/6), 完成.
    7. 来自 https://github.com/yinsendemogui/yunjisuan
    8. 6bff4b4..cb1cf08  master     -> test/master
    9. #查看文件是否修改
    10. [root@Git01 mycode]# cat test.txt   #没有被修改
    11. 说明:
    12. 应用git fetch拉取到本地仓库时,并不修改本地工作目录中的代码
    13. 如果要修改,那么需要进行git merge变更合并
    14. #检查本地工作目录与本地仓库的差异
    15. [root@Git02 yunjisuan]# git checkout
    16. 您的分支落后 'origin/master' 共 2 个提交,并且可以快进。
    17. (使用 "git pull" 来更新您的本地分支)
    18. #在Git01上
    19. [root@Git01 mycode]# git merge  test/master
    20. 更新 6bff4b4..b666246
    21. Fast-forward
    22. test.txt | 5 +++++
    23. 1 file changed, 5 insertions(+)
    24. [root@Git01 mycode]# cat test.txt
    25. sdfsdfsadf
    26. welcome
    27. wwww
    28. wwww
    29. wwww

    2.3.10 get checkout检查工作目录代码与本地仓库中的代码的差异

    1. #检查本地工作目录与本地仓库的差异
    2. [root@Git02 yunjisuan]# git checkout
    3. 您的分支落后 'origin/master' 共 2 个提交,并且可以快进。
    4. (使用 "git pull" 来更新您的本地分支)

    2.3.11 git merge 将远程仓库的变更,更新到本地工作目录中

    1. #在Git01上
    2. [root@Git01 mycode]# git merge  test/master
    3. 更新 6bff4b4..b666246
    4. Fast-forward
    5. test.txt | 5 +++++
    6. 1 file changed, 5 insertions(+)
    7. [root@Git01 mycode]# cat test.txt
    8. sdfsdfsadf
    9. welcome
    10. wwww
    11. wwww
    12. wwww

    2.3.12 git pull 将远程仓库的变更拉取到本地仓库,并更新本地工作目录。

    git pull ====> git fetch + git merge

    1. #在Git01上对文件进行改动,并推送到github远程仓库
    2. [root@Git01 mycode]# ls
    3. test.txt
    4. [root@Git01 mycode]# echo "welcome to yunjisuan" >> test.txt
    5. [root@Git01 mycode]# cat test.txt
    6. sdfsdfsadf
    7. welcome
    8. wwww
    9. wwww
    10. wwww
    11. welcome to yunjisuan
    12. [root@Git01 mycode]# git add *
    13. [root@Git01 mycode]# git commit -m "git01修改了test.txt"
    14. [master 4cc28a4] git01修改了test.txt
    15. 1 file changed, 1 insertion(+)
    16. [root@Git01 mycode]# git push -u test master
    17. Username for 'https://github.com': 215379068@qq.com
    18. Password for 'https://215379068@qq.com@github.com': 
    19. 对象计数中: 3, 完成.
    20. 压缩对象中: 100% (2/2), 完成.
    21. 写入对象中: 100% (3/3), 294 bytes | 0 bytes/s, 完成.
    22. Total 3 (delta 0), reused 0 (delta 0)
    23. To https://github.com/yinsendemogui/yunjisuan.git
    24. b666246..4cc28a4  master -> master
    25. 分支 master 设置为跟踪来自 test 的远程分支 master。
    26. #在Git02上,拉取远程仓库的变更后直接合并进本地仓库的master分支
    27. [root@Git02 ~]# cd /mycode2/yunjisuan/
    28. [root@Git02 yunjisuan]# git pull test master
    29. remote: Counting objects: 3, done.
    30. remote: Compressing objects: 100% (2/2), done.
    31. remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
    32. Unpacking objects: 100% (3/3), done.
    33. 来自 https://github.com/yinsendemogui/yunjisuan
    34. * branch            master     -> FETCH_HEAD
    35. 更新 b666246..4cc28a4
    36. Fast-forward
    37. test.txt | 1 +
    38. 1 file changed, 1 insertion(+)
    39. [root@Git02 yunjisuan]# cat test.txt
    40. sdfsdfsadf
    41. welcome
    42. wwww
    43. wwww
    44. wwww
    45. welcome to yunjisuan
    46. #如果文件还没有被添加到暂存区,那么Linux命令直接改名即可
    47. [root@Git01 mycode]# git status
    48. 位于分支 master
    49. 您的分支与上游分支 'test/master' 一致。
    50. nothing to commit, working tree clean
    51. [root@Git01 mycode]# touch benet.txt
    52. [root@Git01 mycode]# git status
    53. 位于分支 master
    54. 您的分支与上游分支 'test/master' 一致。
    55. 未跟踪的文件:
    56. (使用 "git add <文件>..." 以包含要提交的内容)
    57. benet.txt   #还未提交到暂存区,但是git已经判断出你的变化文件了
    58. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    59. [root@Git01 mycode]# mv benet.txt yunjisuan.txt #直接改名
    60. [root@Git01 mycode]# git status
    61. 位于分支 master
    62. 您的分支与上游分支 'test/master' 一致。
    63. 未跟踪的文件:
    64. (使用 "git add <文件>..." 以包含要提交的内容)
    65. yunjisuan.txt               #git判断的变化文件也同时改变了
    66. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    67. #假如变动文件已经添加到了暂存区
    68. [root@Git01 mycode]# git add *
    69. [root@Git01 mycode]# git status
    70. 位于分支 master
    71. 您的分支与上游分支 'test/master' 一致。
    72. 要提交的变更:
    73. (使用 "git reset HEAD <文件>..." 以取消暂存)
    74. 新文件:   yunjisuan.txt        #变动文件已经添加到了暂存区
    75. #通过git mv 来给已经添加到暂存区的文件改名
    76. [root@Git01 mycode]# git mv yunjisuan.txt benet.txt
    77. [root@Git01 mycode]# git status
    78. 位于分支 master
    79. 您的分支与上游分支 'test/master' 一致。
    80. 要提交的变更:
    81. (使用 "git reset HEAD <文件>..." 以取消暂存)
    82. 新文件:   benet.txt
    83. [root@Git01 mycode]# ls
    84. benet.txt  test.txt         #我们发现本地文件同时改名了
    85. ##通过git reset 来给已经添加到暂存区的文件撤销掉
    86. [root@Git01 mycode]# git status
    87. 位于分支 master
    88. 您的分支领先 'test/master' 共 2 个提交。
    89. (使用 "git push" 来发布您的本地提交)
    90. 要提交的变更:
    91. (使用 "git reset HEAD <文件>..." 以取消暂存)
    92. 新文件:   benet.txt
    93. [root@Git01 mycode]# git reset benet.txt
    94. [root@Git01 mycode]# git status
    95. 位于分支 master
    96. 您的分支领先 'test/master' 共 2 个提交。
    97. (使用 "git push" 来发布您的本地提交)
    98. 未跟踪的文件:
    99. (使用 "git add <文件>..." 以包含要提交的内容)
    100. benet.txt
    101. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

    2.3.13 git mv && git reset 暂存区文件的修改和撤销

    1. #如果文件还没有被添加到暂存区,那么Linux命令直接改名即可
    2. [root@Git01 mycode]# git status
    3. 位于分支 master
    4. 您的分支与上游分支 'test/master' 一致。
    5. nothing to commit, working tree clean
    6. [root@Git01 mycode]# touch benet.txt
    7. [root@Git01 mycode]# git status
    8. 位于分支 master
    9. 您的分支与上游分支 'test/master' 一致。
    10. 未跟踪的文件:
    11. (使用 "git add <文件>..." 以包含要提交的内容)
    12. benet.txt   #还未提交到暂存区,但是git已经判断出你的变化文件了
    13. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    14. [root@Git01 mycode]# mv benet.txt yunjisuan.txt #直接改名
    15. [root@Git01 mycode]# git status
    16. 位于分支 master
    17. 您的分支与上游分支 'test/master' 一致。
    18. 未跟踪的文件:
    19. (使用 "git add <文件>..." 以包含要提交的内容)
    20. yunjisuan.txt               #git判断的变化文件也同时改变了
    21. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    22. #假如变动文件已经添加到了暂存区
    23. [root@Git01 mycode]# git add *
    24. [root@Git01 mycode]# git status
    25. 位于分支 master
    26. 您的分支与上游分支 'test/master' 一致。
    27. 要提交的变更:
    28. (使用 "git reset HEAD <文件>..." 以取消暂存)
    29. 新文件:   yunjisuan.txt        #变动文件已经添加到了暂存区
    30. #通过git mv 来给已经添加到暂存区的文件改名
    31. [root@Git01 mycode]# git mv yunjisuan.txt benet.txt
    32. [root@Git01 mycode]# git status
    33. 位于分支 master
    34. 您的分支与上游分支 'test/master' 一致。
    35. 要提交的变更:
    36. (使用 "git reset HEAD <文件>..." 以取消暂存)
    37. 新文件:   benet.txt
    38. [root@Git01 mycode]# ls
    39. benet.txt  test.txt         #我们发现本地文件同时改名了
    40. ##通过git reset 来给已经添加到暂存区的文件撤销掉
    41. [root@Git01 mycode]# git status
    42. 位于分支 master
    43. 您的分支领先 'test/master' 共 2 个提交。
    44. (使用 "git push" 来发布您的本地提交)
    45. 要提交的变更:
    46. (使用 "git reset HEAD <文件>..." 以取消暂存)
    47. 新文件:   benet.txt
    48. [root@Git01 mycode]# git reset benet.txt
    49. [root@Git01 mycode]# git status
    50. 位于分支 master
    51. 您的分支领先 'test/master' 共 2 个提交。
    52. (使用 "git push" 来发布您的本地提交)
    53. 未跟踪的文件:
    54. (使用 "git add <文件>..." 以包含要提交的内容)
    55. benet.txt
    56. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

    2.3.14 git rm 提交文件的删除变更到暂存区

    git add 可以提交新增文件,修改文件的变更到暂存区; 
    git rm 则是提交删除文件的变更到暂存区

    1. [root@Git01 mycode]# ls
    2. 1  10  2  3  4  5  6  7  8  9  benet.txt  test.txt
    3. [root@Git01 mycode]# rm -f 1 2 3 4
    4. [root@Git01 mycode]# git status
    5. 位于分支 master
    6. 您的分支领先 'test/master' 共 1 个提交。
    7. (使用 "git push" 来发布您的本地提交)
    8. 尚未暂存以备提交的变更:
    9. (使用 "git add/rm <文件>..." 更新要提交的内容)
    10. (使用 "git checkout -- <文件>..." 丢弃工作区的改动)
    11. 删除:     1
    12. 删除:     2
    13. 删除:     3
    14. 删除:     4
    15. 修改尚未加入提交(使用 "git add" /或 "git commit -a")
    16. [root@Git01 mycode]# git rm 1 2 3 4
    17. rm '1'
    18. rm '2'
    19. rm '3'
    20. rm '4'
    21. [root@Git01 mycode]# git status
    22. 位于分支 master
    23. 您的分支领先 'test/master' 共 1 个提交。
    24. (使用 "git push" 来发布您的本地提交)
    25. 要提交的变更:
    26. (使用 "git reset HEAD <文件>..." 以取消暂存)
    27. 删除:     1
    28. 删除:     2
    29. 删除:     3
    30. 删除:     4
    31. [root@Git01 mycode]# git commit -m "delte 1 2 3 4"
    32. [master 58ad6f1] delte 1 2 3 4
    33. 4 files changed, 0 insertions(+), 0 deletions(-)
    34. delete mode 100644 1
    35. delete mode 100644 2
    36. delete mode 100644 3
    37. delete mode 100644 4

    2.3.15 git diff 文件对比利器

    git diff命令可以将本地工作目录中的文件与本地仓库中的文件进行对比

    1. [root@Git01 mycode]# git diff benet.txt     
    2. [root@Git01 mycode]# echo "welcome" >> benet.txt
    3. [root@Git01 mycode]# echo "welcome" >> benet.txt
    4. [root@Git01 mycode]# git diff benet.txt
    5. diff --git a/benet.txt b/benet.txt
    6. index d510880..e672336 100644
    7. --- a/benet.txt
    8. +++ b/benet.txt
    9. @@ -2,3 +2,5 @@ www
    10. www
    11. www
    12. www
    13. +welcome            #工作目录文件与本地仓库对比,多了此行内容
    14. +welcome            #工作目录文件与本地仓库对比,多了此行内容

    2.3.16 git log 查看git提交历史纪录

    • git log:查看提交历史记录 
      • git log -2 :查看最近几条记录
      • git log -p -1 : -p显示每次提交的内容差异
      • git log --stat -2 : stat简要显示数据增改行数,这样就能看到提交中修改过的内容
      • git log --pretty=oneline :一行显示提交的历史记录
    1. #查看最近两条记录
    2. [root@Git01 mycode]# git log -2
    3. commit 0e94052ab30676d118fb6e838c1a23083b9cf9d1
    4. Author: Mr.chen <215379068@qq.com>
    5. Date:   Thu Sep 13 21:44:12 2018 +0800
    6. test
    7. commit d9cd2d8777bc33a44b749005279f14c6d8d1b0f4
    8. Merge: eed7cab 7122274
    9. Author: Mr.chen <215379068@qq.com>
    10. Date:   Thu Sep 13 21:32:31 2018 +0800
    11. Merge remote-tracking branch 'test/master'
    12. #显示最近一次提交的内容差异
    13. [root@Git01 mycode]# git log -p -1
    14. commit 0e94052ab30676d118fb6e838c1a23083b9cf9d1
    15. Author: Mr.chen <215379068@qq.com>
    16. Date:   Thu Sep 13 21:44:12 2018 +0800
    17. test
    18. diff --git a/benet.txt b/benet.txt
    19. index e69de29..5587dab 100644
    20. --- a/benet.txt
    21. +++ b/benet.txt
    22. @@ -0,0 +1,4 @@
    23. +wwww
    24. +wwww
    25. +wwww
    26. +wwww
    27. #显示提交内容的修改概要
    28. [root@Git01 mycode]# git log --stat -2
    29. commit 0e94052ab30676d118fb6e838c1a23083b9cf9d1
    30. Author: Mr.chen <215379068@qq.com>
    31. Date:   Thu Sep 13 21:44:12 2018 +0800
    32. test
    33. benet.txt | 4 ++++
    34. 1 file changed, 4 insertions(+)
    35. commit d9cd2d8777bc33a44b749005279f14c6d8d1b0f4
    36. Merge: eed7cab 7122274
    37. Author: Mr.chen <215379068@qq.com>
    38. Date:   Thu Sep 13 21:32:31 2018 +0800
    39. Merge remote-tracking branch 'test/master'
    40. #用一行显示提交的历史记录
    41. [root@Git01 mycode]# git log --pretty=oneline
    42. 0e94052ab30676d118fb6e838c1a23083b9cf9d1 test
    43. d9cd2d8777bc33a44b749005279f14c6d8d1b0f4 Merge remote-tracking branch 'test/master'
    44. eed7cab86dd33689151bce33380126623a54bac1 tttt
    45. 71222749ac1d3c0cb64eb9cb18b52ac51dec308c test
    46. ae42e7600b66d5368ba865b0d5b4992b11aa9c1e test
    47. 540b738327018eb201ec01e3847a584ce7e69f9a test2
    48. cf36f74f5cfcddfa7356bd01e2b818e8c39cb31e test
    49. 2090db9778298377b90bf0bc56030cf6062a13c9 111
    50. 4cc28a41a62ebab7fb023b00e3e0ab8107a447e0 git01修改了test.txt
    51. b6662463c1f8a0c5d04faf9e146b13c92734587a 再次改动一次test.txt
    52. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt
    53. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    54. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    55. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test

    2.4 追根溯源-git log

    2.4.1 Git还原历史数据

    Git服务程序中有一个叫做HEAD的版本指针,当用户申请还原数据时,其实就是将HEAD指针指向到某个特定的提交版本,但是因为Git是分布式版本控制系统,为了避免历史记录冲突,故使用了SHA-1计算出十六进制的哈西字符串来区分每个提交版本,另外默认的HEAD版本指针会指向到最近的一次提交版本记录,而上一个提交版本会叫HEAD^,上上一个版本则会叫做HEAD^^,当然一般会用HEAD~5来表示往上数第五个提交版本。

    • git reset --hard HEAD^ #-->还原历史提交版本上一次
    • git reset --hard 3de15d4 #-->找到历史还原点的SHA-1值后,就可以还原(值不写全,系统会自动匹配)
    1. #修改一个文件,并提交到本地仓库
    2. [root@Git01 mycode]# ls
    3. 1  10  2  3  4  5  6  7  8  9  benet.txt  test.txt
    4. [root@Git01 mycode]# echo "www" >> benet.txt
    5. [root@Git01 mycode]# echo "www" >> benet.txt
    6. [root@Git01 mycode]# echo "www" >> benet.txt
    7. [root@Git01 mycode]# cat benet.txt
    8. www
    9. www
    10. www
    11. [root@Git01 mycode]# git add *
    12. [root@Git01 mycode]# git commit -m "benet.txt添加了内容"
    13. [master d811fd3] benet.txt添加了内容
    14. 1 file changed, 3 insertions(+)
    15. #查看历史提交记录,并回滚到上一个提交版本
    16. [root@Git01 mycode]# git log --pretty=oneline
    17. d811fd378becc148db827c77ab2701df159f66bd benet.txt添加了内容        #当前工作区目录内容所处的位置
    18. d9cd2d8777bc33a44b749005279f14c6d8d1b0f4 Merge remote-tracking branch 'test/master'
    19. eed7cab86dd33689151bce33380126623a54bac1 tttt
    20. 71222749ac1d3c0cb64eb9cb18b52ac51dec308c test
    21. ae42e7600b66d5368ba865b0d5b4992b11aa9c1e test
    22. 540b738327018eb201ec01e3847a584ce7e69f9a test2
    23. cf36f74f5cfcddfa7356bd01e2b818e8c39cb31e test
    24. 2090db9778298377b90bf0bc56030cf6062a13c9 111
    25. 4cc28a41a62ebab7fb023b00e3e0ab8107a447e0 git01修改了test.txt
    26. b6662463c1f8a0c5d04faf9e146b13c92734587a 再次改动一次test.txt
    27. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt
    28. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    29. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    30. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test
    31. #回滚到前一个版本
    32. [root@Git01 mycode]# git reset --hard HEAD^
    33. HEAD 现在位于 d9cd2d8 Merge remote-tracking branch 'test/master'
    34. [root@Git01 mycode]# git log --pretty=oneline
    35. d9cd2d8777bc33a44b749005279f14c6d8d1b0f4 Merge remote-tracking branch 'test/master' #当前工作区目录内容所处的位置
    36. eed7cab86dd33689151bce33380126623a54bac1 tttt
    37. 71222749ac1d3c0cb64eb9cb18b52ac51dec308c test
    38. ae42e7600b66d5368ba865b0d5b4992b11aa9c1e test
    39. 540b738327018eb201ec01e3847a584ce7e69f9a test2
    40. cf36f74f5cfcddfa7356bd01e2b818e8c39cb31e test
    41. 2090db9778298377b90bf0bc56030cf6062a13c9 111
    42. 4cc28a41a62ebab7fb023b00e3e0ab8107a447e0 git01修改了test.txt
    43. b6662463c1f8a0c5d04faf9e146b13c92734587a 再次改动一次test.txt
    44. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt
    45. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    46. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    47. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test
    48. #查看之前改动的文件
    49. [root@Git01 mycode]# cat benet.txt  #没有任何内容
    50. [root@Git01 mycode]# 
    51. #回滚到指定提交的版本
    52. [root@Git01 mycode]# git log --pretty=oneline
    53. d9cd2d8777bc33a44b749005279f14c6d8d1b0f4 Merge remote-tracking branch 'test/master'
    54. eed7cab86dd33689151bce33380126623a54bac1 tttt
    55. 71222749ac1d3c0cb64eb9cb18b52ac51dec308c test
    56. ae42e7600b66d5368ba865b0d5b4992b11aa9c1e test
    57. 540b738327018eb201ec01e3847a584ce7e69f9a test2
    58. cf36f74f5cfcddfa7356bd01e2b818e8c39cb31e test
    59. 2090db9778298377b90bf0bc56030cf6062a13c9 111
    60. 4cc28a41a62ebab7fb023b00e3e0ab8107a447e0 git01修改了test.txt
    61. b6662463c1f8a0c5d04faf9e146b13c92734587a 再次改动一次test.txt
    62. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt   #计划回滚到这个版本
    63. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    64. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    65. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test
    66. #查看test.txt文件内容
    67. [root@Git01 mycode]# cat test.txt
    68. sdfsdfsadf
    69. welcome
    70. wwww
    71. wwww
    72. wwww
    73. welcome to yunjisuan
    74. www
    75. #回滚到45c319df640a45bf1ce0cd4e4187b106b222e93c提交的版本
    76. [root@Git01 mycode]# git reset --hard 45c319d
    77. HEAD 现在位于 45c319d 再次改动一次test.txt
    78. [root@Git01 mycode]# git log --pretty=oneline
    79. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt
    80. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    81. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    82. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test
    83. [root@Git01 mycode]# cat test.txt
    84. sdfsdfsadf
    85. welcome
    86. wwww
    87. wwww

    2.4.2 Git还原未来数据

    当我们回滚到历史某个提交版本了以后; 
    我们发现我们已经没有在那个版本之后的提交记录了; 
    也就是说,我们一旦还原了历史版本,想要再次还原回去,那么就回不去了。 
    如此一来,一旦错了。我们怎么办呢?

    • git reflog:查看未来历史更新点
    1. #查看未来历史更新点
    2. [root@Git01 mycode]# git reflog
    3. 45c319d HEAD@{0}: reset: moving to 45c319d
    4. d9cd2d8 HEAD@{1}: reset: moving to HEAD^
    5. d811fd3 HEAD@{2}: commit: benet.txt添加了内容   #计划还原到最初的这个版本
    6. d9cd2d8 HEAD@{3}: reset: moving to HEAD^
    7. 0e94052 HEAD@{4}: commit: test
    8. d9cd2d8 HEAD@{5}: merge test/master: Merge made by the 'recursive' strategy.
    9. eed7cab HEAD@{6}: commit: tttt
    10. 540b738 HEAD@{7}: commit: test2
    11. cf36f74 HEAD@{8}: commit: test
    12. 2090db9 HEAD@{9}: commit: 111
    13. 4cc28a4 HEAD@{10}: commit: git01修改了test.txt
    14. b666246 HEAD@{11}: merge refs/remotes/test/master: Fast-forward
    15. 6bff4b4 HEAD@{12}: commit (initial): test
    16. #还原到之前的版本
    17. [root@Git01 mycode]# ls
    18. test.txt
    19. [root@Git01 mycode]# git reset --hard d811fd3
    20. HEAD 现在位于 d811fd3 benet.txt添加了内容
    21. [root@Git01 mycode]# ls
    22. 1  10  2  3  4  5  6  7  8  9  benet.txt  test.txt
    23. [root@Git01 mycode]# git log --pretty=oneline
    24. d811fd378becc148db827c77ab2701df159f66bd benet.txt添加了内容
    25. d9cd2d8777bc33a44b749005279f14c6d8d1b0f4 Merge remote-tracking branch 'test/master'
    26. eed7cab86dd33689151bce33380126623a54bac1 tttt
    27. 71222749ac1d3c0cb64eb9cb18b52ac51dec308c test
    28. ae42e7600b66d5368ba865b0d5b4992b11aa9c1e test
    29. 540b738327018eb201ec01e3847a584ce7e69f9a test2
    30. cf36f74f5cfcddfa7356bd01e2b818e8c39cb31e test
    31. 2090db9778298377b90bf0bc56030cf6062a13c9 111
    32. 4cc28a41a62ebab7fb023b00e3e0ab8107a447e0 git01修改了test.txt
    33. b6662463c1f8a0c5d04faf9e146b13c92734587a 再次改动一次test.txt
    34. 45c319df640a45bf1ce0cd4e4187b106b222e93c 再次改动一次test.txt
    35. cb1cf08c325fab21e7ee29f67946866863fdedb2 再次改动一次test.txt
    36. 40f3e3d488d059a190652c05e8c28f52459acceb 修改了test.txt
    37. 6bff4b4c5f37c78e2a3b1e855057edbbeb4a9b4e test

    2.4.3 Git的标签使用

    前面回滚使用的是一串字符串,又长又难记 
    git tag <标签> -m "描述
    每次提交都可以打一个标签

    • git tag v1.0 : 给当前提交内容打一个标签(方便回滚) 
      • git tag : 查看当前所有的标签
      • git show v1.0 :查看当前1.0版本的详细信息
      • git tag v1.2 -m "描述"
      • git tag -d v1.0 :删除之前的v1.0标签
    1. #查看所有标签
    2. [root@Git01 mycode]# git tag
    3. #添加v1.0标签给当前提交的内容
    4. [root@Git01 mycode]# git tag v1.0
    5. #查看标签为v1.0的提交的详细内容
    6. [root@Git01 mycode]# git show v1.0
    7. commit def8bf29afbd2bd7b899bf1472f9e1d290995afe
    8. Author: Mr.chen <215379068@qq.com>
    9. Date:   Thu Sep 13 23:08:24 2018 +0800
    10. test3
    11. diff --git a/benet.txt b/benet.txt
    12. index 529c2d0..d510880 100644
    13. --- a/benet.txt
    14. +++ b/benet.txt
    15. @@ -1,3 +1,4 @@
    16. www
    17. www
    18. www
    19. +www        #本次提交内容的变更信息
    20. #修改benet文件内容,再次进行提交
    21. [root@Git01 mycode]# echo "www" >> benet.txt
    22. [root@Git01 mycode]# echo "www" >> benet.txt
    23. [root@Git01 mycode]# git add *
    24. [root@Git01 mycode]# git commit -m "test"
    25. [master 942131a] test
    26. 1 file changed, 2 insertions(+)
    27. #给本次提交添加v2.0标签
    28. [root@Git01 mycode]# git tag v2.0
    29. [root@Git01 mycode]# git tag
    30. v1.0
    31. v2.0
    32. #查看标签为v1.0的提交的详细变更内容
    33. [root@Git01 mycode]# git show v1.0
    34. commit def8bf29afbd2bd7b899bf1472f9e1d290995afe
    35. Author: Mr.chen <215379068@qq.com>
    36. Date:   Thu Sep 13 23:08:24 2018 +0800
    37. test3
    38. diff --git a/benet.txt b/benet.txt
    39. index 529c2d0..d510880 100644
    40. --- a/benet.txt
    41. +++ b/benet.txt
    42. @@ -1,3 +1,4 @@
    43. www
    44. www
    45. www
    46. +www
    47. #查看标签为v2.0的提交的详细变更内容
    48. [root@Git01 mycode]# git show v2.0
    49. commit 942131a50893184fb627f7d113531602dc42970a
    50. Author: Mr.chen <215379068@qq.com>
    51. Date:   Thu Sep 13 23:29:09 2018 +0800
    52. test
    53. diff --git a/benet.txt b/benet.txt
    54. index d510880..b8cf22e 100644
    55. --- a/benet.txt
    56. +++ b/benet.txt
    57. @@ -2,3 +2,5 @@ www
    58. www
    59. www
    60. www
    61. +www
    62. +www
    63. #删除v2.0标签,重新设定附带描述信息的标签
    64. [root@Git01 mycode]# git tag -d v2.0
    65. 已删除标签 'v2.0'(曾为 942131a)
    66. [root@Git01 mycode]# git tag
    67. v1.0
    68. [root@Git01 mycode]# git tag v2.0 -m "给文件benet.txt增加两行代码"
    69. [root@Git01 mycode]# git tag
    70. v1.0
    71. v2.0
    72. [root@Git01 mycode]# git show v2.0
    73. tag v2.0
    74. Tagger: Mr.chen <215379068@qq.com>
    75. Date:   Thu Sep 13 23:31:15 2018 +0800
    76. 给文件benet.txt增加两行代码
    77. commit 942131a50893184fb627f7d113531602dc42970a
    78. Author: Mr.chen <215379068@qq.com>
    79. Date:   Thu Sep 13 23:29:09 2018 +0800
    80. test
    81. diff --git a/benet.txt b/benet.txt
    82. index d510880..b8cf22e 100644
    83. --- a/benet.txt
    84. +++ b/benet.txt
    85. @@ -2,3 +2,5 @@ www
    86. www
    87. www
    88. www
    89. +www
    90. +www
    91. #通过标签进行历史提交回滚,回滚到v1.0版本
    92. [root@Git01 mycode]# cat benet.txt
    93. www
    94. www
    95. www
    96. www
    97. www
    98. www
    99. [root@Git01 mycode]# git reset --hard v1.0
    100. HEAD 现在位于 def8bf2 test3
    101. [root@Git01 mycode]# cat benet.txt
    102. www
    103. www
    104. www
    105. www

    2.5 gitignore文件

    • 为什么使用.gitignore文件 
      • 大量与项目无关的文件全推到远程仓库上,同步的时候会非常慢,且跟编辑器相关的一些配置推上去之后,别人更新也会受到影响。所以,我们使用该文件,对不必要的文件进行忽略,使其不被git追踪。
      • 一般情况下, .gitignore文件,在项目一开始创建的时候就创建,并推送到远程服务器上。这样大家初次同步项目的时候,就是用到该文件,避免以后,团队成员把与项目无关的文件,传到远程服务器上。
      • gitignore文件匹配规则 
        • *.log 表示忽略项目中所有以.log结尾的文件
        • 123?.log 表示忽略项目中所有以123加任意一个字符的文件
        • /error.log 表示忽略根目录下的error.log文件
        • **/java/ 匹配所有java目录下的所有文件
        • !/error.log 表示在前面的匹配规则中,被忽略了的文件,你不想它被忽略,那么就可以在文件前加叹号
    1. #在Git01上
    2. [root@Git01 mycode]# ls
    3. 10  5  6  7  8  9  access.log  benet.txt  test.txt  x.idea  xx.idea
    4. [root@Git01 mycode]# git status
    5. 位于分支 master
    6. 您的分支领先 'test/master' 共 2 个提交。
    7. (使用 "git push" 来发布您的本地提交)
    8. 未跟踪的文件:
    9. (使用 "git add <文件>..." 以包含要提交的内容)
    10. access.log          #三个文件需要被add
    11. x.idea              #三个文件需要被add
    12. xx.idea             #三个文件需要被add
    13. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)
    14. #添加.gitignore文件
    15. [root@Git01 mycode]# cat .gitignore
    16. target
    17. *.log       #忽略任意开头.log结尾文件
    18. ?.idea      #忽略任意一个字符开头.idea结尾文件
    19. #查看git工作目录文件状态
    20. [root@Git01 mycode]# git status
    21. 位于分支 master
    22. 您的分支领先 'test/master' 共 2 个提交。
    23. (使用 "git push" 来发布您的本地提交)
    24. 未跟踪的文件:
    25. (使用 "git add <文件>..." 以包含要提交的内容)
    26. .gitignore          #刚才被规则覆盖的文件没了
    27. xx.idea             #刚才被规则覆盖的文件没了
    28. 提交为空,但是存在尚未跟踪的文件(使用 "git add" 建立跟踪)

    三,GIT分支管理

    3.1 分支的结构概述

    在实际的项目开发中,尽量保证master分支稳定,仅用于发布新版本,平时不要随便直接修改里面的数据文件。 
    那在哪干活呢?干活都在dev分支上。每个人从dev分支创建自己个人分支,开发完合并到dev分支,最后dev分支合并到master分支。 
    所以,团队的合作分支看起来会像下图那样。

     

    分支管理工作流程: 
    在工作中,为了保证master分支稳定,产品经理通常会从master分支上复制一份代码作为dev分支; 
    然后成员开发A在从dev分支上复制一份代码叫做michael 
    然后成员开发B再从dev分支上复制一份代码叫做bob 
    平时开发A和开发B推送和拉取代码都在自己的个人分支michaelbob上。 
    当一个新功能开发完毕,或者一个bug修改完毕以后。开发人员会先将代码变更推送到自己的个人分支,然后再把个人分支的变更合并到dev分支里; 
    当开发经理和测试人员拉取dev分支的代码进行测试以后,如果没问题,那么开发经理会把dev分支的变更合并进master版本; 
    最后,由于master版本新增了测试过的新功能,那么就需要进行项目发布或者代码上线了。

    3.2 GIT本地分支管理

    3.2.1 本地分支的创建与切换

    • git branch : 查看当前分支情况,当前分支前有*号
    • git branch linux : 创建分支
    • git checkout: 检查本地分支与远程分支的变更差异
    • git checkout linux:切换分支
    1. #检查当前分支情况
    2. [root@Git01 mycode]# git branch
    3. * master                                 #当前所处分支
    4. [root@Git01 mycode]# git branch linux   #创建linux分支
    5. [root@Git01 mycode]# git branch
    6. linux
    7. * master                                #当前所处分支            
    8. [root@Git01 mycode]# git checkout linux #切换到linux分支
    9. 切换到分支 'linux'
    10. [root@Git01 mycode]# git branch
    11. * linux                                  #当前所处分支
    12. master

    3.2.2 尝试在linux本地分支进行代码提交

    1. [root@Git01 mycode]# git branch
    2. * linux
    3. master
    4. [root@Git01 mycode]# cat benet.txt
    5. www
    6. www
    7. www
    8. [root@Git01 mycode]# echo "linux分支新增一行" >> benet.txt
    9. [root@Git01 mycode]# git add *
    10. [root@Git01 mycode]# git status
    11. 位于分支 linux
    12. 要提交的变更:
    13. (使用 "git reset HEAD <文件>..." 以取消暂存)
    14. 修改:     benet.txt
    15. #此时我们先将改变提交到本地linux分支以后在切换分支
    16. #将变更提交到本地linux分支
    17. [root@Git01 mycode]# git commit -m "test"
    18. [linux 2592cd6] test
    19. 1 file changed, 1 insertion(+)
    20. #查看修改过的文件
    21. [root@Git01 mycode]# cat benet.txt
    22. www
    23. www
    24. www
    25. linux分支新增一行           #linux分支比master分支新增一行数据
    26. #切换分支到master分支
    27. [root@Git01 mycode]# git checkout master
    28. 切换到分支 'master'
    29. 您的分支与上游分支 'test/master' 一致。
    30. #查看benet.txt文件
    31. [root@Git01 mycode]# cat benet.txt
    32. www
    33. www
    34. www
    35. 说明:
    36. 我们发现切换分支以后,在linux分支修改过的文件数据,完全还原成了master分支的文件内容
    37. 但是这里存在一个问题,假如我们在linux分支下,修改了文件,但是并未提交到本地仓库linux分支,而只是放到了暂存区就切换到master分支的话,那么会发生什么呢?
    38. #切换到linux分支
    39. [root@Git01 mycode]# git checkout linux
    40. 切换到分支 'linux'
    41. [root@Git01 mycode]# cat benet.txt
    42. www
    43. www
    44. www
    45. linux分支新增一行
    46. #再次新增一行数据
    47. [root@Git01 mycode]# echo "linux分支新增第二行" >> benet.txt
    48. [root@Git01 mycode]# cat benet.txt
    49. www
    50. www
    51. www
    52. linux分支新增一行
    53. linux分支新增第二行
    54. #文件变更添加到暂存区
    55. [root@Git01 mycode]# git add *
    56. [root@Git01 mycode]# git status
    57. 位于分支 linux
    58. 要提交的变更:
    59. (使用 "git reset HEAD <文件>..." 以取消暂存)
    60. 修改:     benet.txt
    61. #切换分支master,但由于没有提交暂存区的改变,因此切换分支失败
    62. [root@Git01 mycode]# git checkout master
    63. error: Your local changes to the following files would be overwritten by checkout:
    64. benet.txt
    65. Please commit your changes or stash them before you switch branches.
    66. 终止中
    67. [root@Git01 mycode]# git branch
    68. * linux             #仍旧在linux分支
    69. master
    70. 说明:
    71. 当linux的本地分支仓库和master本地分支仓库的代码不同时,如果你没把变更从暂存区提交到分支仓库,那么默认是不能切换分支的。
    72. 但是,这里还有一个问题,假如linux分支和master分支的本地仓库的代码相同,但是我仍旧没把linux分支的变更提交到本地linux分支仓库,那么能直接切换master分支吗?
    73. #将本地分支master和linux的代码保存为一致。
    74. [root@Git01 mycode]# git branch
    75. linux
    76. * master
    77. [root@Git01 mycode]# cat benet.txt
    78. www
    79. www
    80. www
    81. [root@Git01 mycode]# git checkout linux
    82. 切换到分支 'linux'
    83. [root@Git01 mycode]# cat benet.txt
    84. www
    85. www
    86. www
    87. #在linux分支修改benet.txt文件
    88. [root@Git01 mycode]# echo "修改第一次" >> benet.txt
    89. [root@Git01 mycode]# cat benet.txt
    90. www
    91. www
    92. www
    93. 修改第一次
    94. [root@Git01 mycode]# git branch
    95. * linux                     #当前在linux分支
    96. master
    97. [root@Git01 mycode]# git add *
    98. #切换到master分支
    99. [root@Git01 mycode]# git checkout master
    100. M   benet.txt
    101. 切换到分支 'master'
    102. 您的分支与上游分支 'test/master' 一致。
    103. [root@Git01 mycode]# cat benet.txt
    104. www
    105. www
    106. www
    107. 修改第一次                      #之前的修改仍旧存在
    108. [root@Git01 mycode]# git status             #切换分支前的暂存区内容还在
    109. 位于分支 master
    110. 您的分支与上游分支 'test/master' 一致。
    111. 要提交的变更:
    112. (使用 "git reset HEAD <文件>..." 以取消暂存)
    113. 修改:     benet.txt
    114. 说明:
    115. 如果本地分支仓库间的代码一致,那么就算不把变更提交到本地分支仓库,那么也可以切换分支,这点同学们要注意。
    116. 在工作中,为了避免意外,在切换分支前,务必要先将暂存区提交或者清空。
    117. #清空暂存区内容
    118. [root@Git01 mycode]# git reset
    119. 重置后取消暂存的变更:
    120. M   benet.txt

    3.3 本地分支的合并与删除

    想把linux的工作成果合并到master分支上; 
    先切换到master分支 
    git merge linux : 合并linux分支到master 
    git branch -d linux :确定合并完成后,可以放心的删除Linux分支。

    3.3.1 自动合并本地分支

    1. [root@Git01 mycode]# git branch
    2. linux
    3. * master
    4. [root@Git01 mycode]# cat benet.txt
    5. www
    6. www
    7. www

    3.3.2 本地分支的删除

    1. [root@Git01 mycode]# git merge linux    #将分支linux的变更合并到当前分支
    2. 更新 4da08fc..23e5b98
    3. Fast-forward
    4. benet.txt | 1 +
    5. 1 file changed, 1 insertion(+)
    6. [root@Git01 mycode]# cat benet.txt
    7. www
    8. www
    9. www
    10. 修改第一次
    11. #将linux分支删除(当前不能在linux分支上)
    12. [root@Git01 mycode]# git branch -d linux
    13. 已删除分支 linux(曾为 23e5b98)。
    14. [root@Git01 mycode]# git branch
    15. * master
    16. #假如linux分支的变更没有合并到当前分支,那么必须用-D参数强制删除分支
    17. [root@Git01 mycode]# git branch
    18. linux
    19. * master
    20. [root@Git01 mycode]# git branch -d linux
    21. error: 分支 'linux' 没有完全合并。
    22. 如果您确认要删除它,执行 'git branch -D linux'。
    23. [root@Git01 mycode]# git branch -D linux
    24. 已删除分支 linux(曾为 7212474)。

    3.3.3 手动合并本地分支===>本地分支代码冲突解决方案

    当本地分支之间的同名目录-同名文件-同一行内容不同时,在进行分支合并时就会产生冲突,故为了解决代码冲突,就需要进行人工手动合并; 
    在工作中,为了尽量避免冲突,一般开发人员之间尽可能不负责相同的功能模块,也就不至于同时修改同一个文件。

    1. #在Git01上进行操作,让linux分支和master分支产生文件代码冲突
    2. [root@Git01 mycode]# git checkout linux
    3. 切换到分支 'linux'
    4. [root@Git01 mycode]# cat benet.txt
    5. www
    6. www
    7. www
    8. test
    9. linux               #本地linux分支benet.txt文件的第五行是linux
    10. [root@Git01 mycode]# git status     #查看文件状态,已经提交暂存区无内容
    11. 位于分支 linux
    12. nothing to commit, working tree clean
    13. #切换到master分支
    14. [root@Git01 mycode]# git checkout master
    15. 切换到分支 'master'
    16. [root@Git01 mycode]# cat benet.txt
    17. www
    18. www
    19. www
    20. test
    21. master              ##本地master分支benet.txt文件的第五行是master
    22. [root@Git01 mycode]# git status
    23. 位于分支 master
    24. nothing to commit, working tree clean
    25. 说明:
    26. 由上可知,本地linux分支的benet.txt文件和本地master分支的benet.txt文件的第五行代码产生冲突。如果此时,我们将本地linux分支的变更合并到本地master分支中,那么就会产生代码冲突。
    27. #合并本地linux分支
    28. [root@Git01 mycode]# git branch
    29. linux
    30. * master
    31. [root@Git01 mycode]# git merge linux
    32. 自动合并 benet.txt
    33. 冲突(内容):合并冲突于 benet.txt      #报错说明,冲突在benet.txt文件
    34. 自动合并失败,修正冲突然后提交修正的结果。
    35. #找出文件冲突内容,并修改。(此时冲突的benet.txt文件里已经被标注了冲突内容)
    36. [root@Git01 mycode]# cat benet.txt
    37. www
    38. www
    39. www
    40. test
    41. <<<<<<< HEAD                #HEAD:表示当前所在的分支
    42. master                      #此行表示当前所在分支本行的master和下边的linux所在分支的linux冲突
    43. =======                     #隔离符号
    44. linux                       #和上边的master内容冲突
    45. >>>>>>> linux               #linux分支
    46. #到了这里我们只能手动排除,选择保留后的内容,假如我们要保留linux分支的内容,然后再将工作目录中的变更提交即可人工解决代码冲突,并完成分支合并
    47. [root@Git01 mycode]# vim benet.txt
    48. [root@Git01 mycode]# cat benet.txt
    49. www
    50. www
    51. www
    52. test
    53. linux
    54. [root@Git01 mycode]# git add *
    55. [root@Git01 mycode]# git commit -m "修改了一个分支冲突"
    56. [master 001ef0b] 修改了一个分支冲突
    57. [root@Git01 mycode]# git status
    58. 位于分支 master
    59. 您的分支领先 'test/master' 共 1 个提交。
    60. (使用 "git push" 来发布您的本地提交)
    61. nothing to commit, working tree clean

    3.4 GIT远程分支管理

    3.4.1 将linux本地分支代码和标签推送到github远程仓库的linux分支

    1. #在Git01上,创建本地分支linux,复制master分支代码。
    2. [root@Git01 mycode]# git branch
    3. * master
    4. [root@Git01 mycode]# git status
    5. 位于分支 master
    6. 您的分支与上游分支 'test/master' 一致。
    7. nothing to commit, working tree clean
    8. #创建本地linux分支
    9. [root@Git01 mycode]# git branch linux
    10. [root@Git01 mycode]# git checkout linux
    11. 切换到分支 'linux'
    12. #将工作目录代码的benet.txt文件修改后提交到本地linux分支
    13. [root@Git01 mycode]# echo "远程分支提交测试" >> benet.txt
    14. [root@Git01 mycode]# cat benet.txt
    15. www
    16. www
    17. www
    18. test
    19. linux
    20. 远程分支提交测试
    21. [root@Git01 mycode]# git add *
    22. [root@Git01 mycode]# git commit -m "test"
    23. 位于分支 linux
    24. nothing to commit, working tree clean
    25. [root@Git01 mycode]# git status
    26. 位于分支 linux
    27. nothing to commit, working tree clean
    28. #将本地linux分支推送到远程github的linux分支
    29. [root@Git01 mycode]# git push test linux
    30. Username for 'https://github.com': 215379068@qq.com
    31. Password for 'https://215379068@qq.com@github.com': 
    32. Total 0 (delta 0), reused 0 (delta 0)
    33. remote: 
    34. remote: Create a pull request for 'linux' on GitHub by visiting:
    35. remote:      https://github.com/yinsendemogui/yunjisuan/pull/new/linux
    36. remote: 
    37. To https://github.com/yinsendemogui/yunjisuan.git
    38. * [new branch]      linux -> linux

    浏览器访问:https://github.com

     

    1. #创建本地标签,并推送到github
    2. [root@Git01 mycode]# git tag v1.0 -m "这就是一个测试"
    3. [root@Git01 mycode]# git tag
    4. v1.0
    5. [root@Git01 mycode]# git push test v1.0
    6. Username for 'https://github.com': 215379068@qq.com
    7. Password for 'https://215379068@qq.com@github.com': 
    8. 对象计数中: 1, 完成.
    9. 写入对象中: 100% (1/1), 183 bytes | 0 bytes/s, 完成.
    10. Total 1 (delta 0), reused 0 (delta 0)
    11. To https://github.com/yinsendemogui/yunjisuan.git
    12. * [new tag]         v1.0 -> v1.0

    浏览器访问:https://github.com

     

    3.4.2 从github远程仓库克隆一个linux分支到本地linux分支

    1)克隆远程仓库master分支后,通过checkout切换成远程linux分支

    1. #在Git02上进行操作
    2. [root@Git02 ~]# mkdir -p /mycode
    3. [root@Git02 ~]# cd /mycode
    4. [root@Git02 mycode]# git init
    5. 初始化空的 Git 版本库于 /mycode/.git/
    6. #克隆远程仓库到本地
    7. [root@Git02 mycode]# git clone https://github.com/yinsendemogui/yunjisuan.git
    8. 正克隆到 'yunjisuan'...
    9. remote: Counting objects: 61, done.
    10. remote: Compressing objects: 100% (33/33), done.
    11. remote: Total 61 (delta 16), reused 53 (delta 8), pack-reused 0
    12. Unpacking objects: 100% (61/61), done.
    13. [root@Git02 mycode]# ls
    14. yunjisuan
    15. [root@Git02 mycode]# cd yunjisuan/
    16. [root@Git02 yunjisuan]# cat benet.txt
    17. www
    18. www
    19. www
    20. test
    21. linux   #不是之前上传的linux分支版本
    22. [root@Git02 yunjisuan]# git branch  #克隆下来的是master分支
    23. * master
    24. 说明:
    25. 如果clone时不用-b linux指定分支进行克隆那么默认克隆的都是master分支;我们可以通过checkout切换远程分支的方式对于已经下载下来的master工作目录进行代码替换。
    26. #查看github远程仓库分支情况
    27. [root@Git02 yunjisuan]# git branch -a
    28. * master
    29. remotes/origin/HEAD -> origin/master      #当前分支为origin/master分支
    30. remotes/origin/linux
    31. remotes/origin/master
    32. #检查origin/linux远程分支
    33. [root@Git02 yunjisuan]# git checkout origin/linux
    34. Note: checking out 'origin/linux'.
    35. You are in 'detached HEAD' state. You can look around, make experimental
    36. changes and commit them, and you can discard any commits you make in this
    37. state without impacting any branches by performing another checkout.
    38. If you want to create a new branch to retain commits you create, you may
    39. do so (now or later) by using -b with the checkout command again. Example:
    40. git checkout -b new_branch_name
    41. HEAD 目前位于 c89123d... test2
    42. [root@Git02 yunjisuan]# cat benet.txt
    43. www
    44. www
    45. www
    46. test
    47. linux
    48. 远程分支提交测试        #文件内容已经变了
    49. [root@Git02 yunjisuan]# git branch
    50. * (分离自 origin/linux)           #当前分支是origin/linux分支
    51. master
    52. [root@Git02 yunjisuan]# git branch linux    #创建本地linux分支
    53. [root@Git02 yunjisuan]# git branch
    54. * (分离自 origin/linux)
    55. linux
    56. master
    57. [root@Git02 yunjisuan]# git checkout linux  #切换到本地linux分支
    58. 切换到分支 'linux'
    59. [root@Git02 yunjisuan]# git branch
    60. * linux                             #当前工作目录代码已经作为本地linux分支代码
    61. master

    2)通过git clone -b linux URL直接指定远程仓库分支进行克隆

    1. #克隆一个远程分支linux到本地工作目录(-b linux指定分支)
    2. [root@Git02 mycode]# git clone -b linux https://github.com/yinsendemogui/yunjisuan.git
    3. 正克隆到 'yunjisuan'...
    4. remote: Counting objects: 61, done.
    5. remote: Compressing objects: 100% (33/33), done.
    6. remote: Total 61 (delta 16), reused 53 (delta 8), pack-reused 0
    7. Unpacking objects: 100% (61/61), done.
    8. [root@Git02 mycode]# ls
    9. yunjisuan
    10. [root@Git02 mycode]# cat yunjisuan/benet.txt
    11. www
    12. www
    13. www
    14. test
    15. linux
    16. 远程分支提交测试
  • 相关阅读:
    Linux下MySQL/MariaDB Galera集群搭建过程
    Linux下Nginx+Tomcat负载均衡和动静分离配置要点
    快速部署tomcat项目的Shell脚本
    利用缓存实现APP端与服务器接口交互的Session控制
    基于xml的Spring多数据源配置和使用
    基于注解的Spring多数据源配置和使用
    MySQL 开启事件 使用定时器调用存储过程
    MyBatis绑定错误--BindingException:Invalid bound statement (not found)
    micropython1.16官方文档转PDF
    dokuwiki使用随笔
  • 原文地址:https://www.cnblogs.com/heroke/p/10262445.html
Copyright © 2020-2023  润新知