zoukankan      html  css  js  c++  java
  • Linux(14):集群架构进阶 --- CentOS 7

    CentOS 7

    CentOS 7 管理软件常用命令:

    vim /etc/selinux/config                         # 修改 SELINUX 开机自启动与否
    setenforce 0                                    # 关闭 SELINUX
    getenforce                                         # 查看 SELINUX 状态
    systemctl stop firewalld.service                # 关闭防火墙
    systemctl status firewalld.service                # 查看防火墙状态
    systemctl enable firewalld.service                # 设置防火墙开机不会自启动
    systemctl disable firewalld.service                # 设置防火墙开机自启动

    CentOS 7 与 6 的区别:

    # 区别1:网卡名称的不一致
    # 想让 CentOS 7 中的网卡名也为 eth 这种形式,可用如下方法:
    # 在安装系统的时候,修改内核参数 net.ifnames=0 biosdevname=0,使网卡名统一  (即在安装系统的时候,输入 net.ifnames=0 biosdevname=0 )
    
    
    # 区别2: CentOS 7 中的网络配置命令:
    ip命令的安装:yum -y install iproute
    # CentOS7主推使用ip、ss命令。
    ifconfig命令的安装: yum -y install net-tools
    
    # 网卡和路由的相关命令如下:
    ip a                  # 查看网卡信息             <--- ip addr 或者 ip address
    ip a s eth0            # 显示某块网卡的信息;  a 是 addr 或者 address 的简写, s 是 show 的简写
    ip r                # 查看路由信息             <--- ip route
    
    # 7 中的 ss 命令可查看监听的端口号和连接状态,类似 6 中的 netstat
    [root@NEO ~]# ss -lntup            # 查看端口
    Netid State      Recv-Q Send-Q     Local Address:Port      Peer Address:Port              
    tcp   LISTEN     0      128                    *:22                   *:*            users:(("sshd",pid=7289,fd=3))
    tcp   LISTEN     0      100            127.0.0.1:25                   *:*            users:(("master",pid=7416,fd=13))
    tcp   LISTEN     0      128                   :::22                  :::*            users:(("sshd",pid=7289,fd=4))
    tcp   LISTEN     0      100                  ::1:25                  :::*            users:(("master",pid=7416,fd=14))
    [root@NEO ~]# ss -ant            # 查看连接状态
    State       Recv-Q Send-Q               Local Address:Port                              Peer Address:Port              
    LISTEN      0      128                              *:22                                           *:*                  
    LISTEN      0      100                      127.0.0.1:25                                           *:*                  
    ESTAB       0      0                       10.0.0.201:22                                  10.0.0.253:6703               
    ESTAB       0      52                      10.0.0.201:22                                  10.0.0.253:9113               
    LISTEN      0      128                             :::22                                          :::*                  
    LISTEN      0      100                            ::1:25                                          :::*   
    
    # setup 命令在 CentOS 7 中不建议使用;setup 在 7 中,一是建议使用 vim 去修改相关配置文件,二是可用 nmtui 命令去替代 setup 命令 
    
    
    # 区别3:主机名等配置文件
    # 修改主机名
    hostname 新的主机名                     ---> 临时生效
    # 编辑 /etc/hostname                    --->   修改主机名的配置文件 (修改主机名永久生效);方法一
    hostnamectl set-hostname 新的主机名     --->   让修改主机名即临时生效同时也永久生效(本质还是改配置文件);方法二
    
    # 修改字符集
    localectl set-locale LANG=zh_CN. UTF-8    --->   方法一
    # 编辑 /etc/locale.conf                    --->   方法二
    
    cat /etc/redhat-release                --->   查看系统版本号
    cat /etc/os-release                    --->   所有支持systemd系统的统一发行版名称和版本号文件
    
    
    # 区别4: /etc/rc.local ---> 开机自启动的软件
    # CentOS 7 中开机自启动的软件放到 /etc/rc.local 中,但 /etc/rc.local 的默认权限是 644,想要放在 /etc/rc.local 中的软件能够开机自启动,需要给 /etc/rc.local 加上执行权限
    [root@neo ~]# ll /etc/rc.local 
    lrwxrwxrwx. 1 root root 13 Jun 17 23:13 /etc/rc.local -> rc.d/rc.local
    [root@neo ~]# ll /etc/rc.d/rc.local 
    -rw-r--r--. 1 root root 473 Oct 31  2018 /etc/rc.d/rc.local                # 默认权限644
    [root@neo ~]# chmod +x /etc/rc.local 
    [root@neo ~]# ll /etc/rc.d/rc.local
    -rwxr-xr-x. 1 root root 473 Oct 31  2018 /etc/rc.d/rc.local                # 加上执行权限
    
    
    # 区别5: 运行级别 ---> Runlevel vs System Target
    # 7 中的运行级别
    [root@neo ~]# ls -lh /usr/lib/systemd/system/runlevel*.target        ---> 查看所有的运行级别(7中的7个运行级别是为了兼容6中的7个运行级别)
    lrwxrwxrwx. 1 root root 15 Jun 17 23:13 /usr/lib/systemd/system/runlevel0.target -> poweroff.target
    lrwxrwxrwx. 1 root root 13 Jun 17 23:13 /usr/lib/systemd/system/runlevel1.target -> rescue.target
    lrwxrwxrwx. 1 root root 17 Jun 17 23:13 /usr/lib/systemd/system/runlevel2.target -> multi-user.target
    lrwxrwxrwx. 1 root root 17 Jun 17 23:13 /usr/lib/systemd/system/runlevel3.target -> multi-user.target
    lrwxrwxrwx. 1 root root 17 Jun 17 23:13 /usr/lib/systemd/system/runlevel4.target -> multi-user.target
    lrwxrwxrwx. 1 root root 16 Jun 17 23:13 /usr/lib/systemd/system/runlevel5.target -> graphical.target
    lrwxrwxrwx. 1 root root 13 Jun 17 23:13 /usr/lib/systemd/system/runlevel6.target -> reboot.target
    
    [root@neo ~]# systemctl get-default                         ---> 查看当前运行级别
    multi-user.target
    
    [root@neo ~]# systemctl set-default multi-user.target         ---> 设置运行级别为 multi-user.target
    Removed symlink /etc/systemd/system/default.target.
    Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.
    [root@neo ~]# 
    
    [root@neo ~]# ll -d /etc/systemd/system/ /usr/lib/systemd/system/
    drwxr-xr-x.  8 root root  4096 Jun 18 23:21 /etc/systemd/system/            # 这个目录放的是系统的配置(相比下面的,优先级更高)
    drwxr-xr-x. 22 root root 12288 Jun 18 01:23 /usr/lib/systemd/system/        # 这个目录放的是用户的配置
    
    
    # 区别6:管理服务
    # CentOS 6 中:
    chkconfig
    service
    /etc/init.d/
    
    # CentOS 7 中:
    # systemctl:融合service和chkconfig的功能于一体,兼容SysV和LSB的启动脚本,而且够在进程启动过程中更有效地引导加载服务
    # 详情见下图
    
    [root@neo ~]# systemctl is-active crond.service         # 定时任务是不是正在运行
    active
    [root@neo ~]# systemctl is-enabled crond.service         # 定时任务是不是开机自启动
    enabled
    
    # systemctl start crond.service和systemctl start crond效果一样。

    systemctl 的用法:

    git

    给git添加全局配置

    [root@web03 ~]# git config --global user.name neo                        # 添加全局配置
    [root@web03 ~]# git config --global user.email andrewzheng@sina.cn        # 添加全局配置
    [root@web03 ~]# ll .gitconfig                         # --global 级别的配置会在用户家目录下生成一个 .gitconfig 文件
    -rw-r--r-- 1 root root 48 Jul  8 16:27 .gitconfig
    [root@web03 ~]# cat .gitconfig 
    [user]
        name = neo
        email = andrewzheng@sina.cn
    [root@web03 ~]# git config --list         # 查看全局配置
    user.name=neo
    user.email=andrewzheng@sina.cn
    [root@web03 ~]# 

    git 初始化仓库

    [root@web03 ~]# mkdir git_test
    [root@web03 ~]# cd git_test/
    [root@web03 git_test]# ll
    total 0
    [root@web03 git_test]# git init            # 初始化仓库
    Initialized empty Git repository in /root/git_test/.git/
    [root@web03 git_test]# ll -a
    total 12
    drwxr-xr-x  3 root root 4096 Jul  8 16:48 .
    dr-xr-x---. 5 root root 4096 Jul  8 16:47 ..
    drwxr-xr-x  7 root root 4096 Jul  8 16:48 .git        # 生成的 git 仓库(本地仓库)
    [root@web03 git_test]# cd .git/
    [root@web03 .git]# ll
    total 32
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 branches
    -rw-r--r-- 1 root root   92 Jul  8 16:48 config
    -rw-r--r-- 1 root root   73 Jul  8 16:48 description
    -rw-r--r-- 1 root root   23 Jul  8 16:48 HEAD
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 hooks
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 info
    drwxr-xr-x 4 root root 4096 Jul  8 16:48 objects        # objects 目录是真正的仓库,仓库的数据都是放到这个目录下
    drwxr-xr-x 4 root root 4096 Jul  8 16:48 refs
    [root@web03 .git]#

    git 基础命令

    [root@web03 git_test]# git status        # 查看仓库状态
    On branch master
    
    Initial commit
    
    nothing to commit (create/copy files and use "git add" to track)
    [root@web03 git_test]# touch a b
    [root@web03 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jul  9 00:46 a
    -rw-r--r-- 1 root root 0 Jul  9 00:46 b
    [root@web03 git_test]# git status
    On branch master
    
    Initial commit
    
    Untracked files:                                                    # 未跟踪文件(只存在于工作目录)
      (use "git add <file>..." to include in what will be committed)
    
        a
        b
    
    nothing added to commit but untracked files present (use "git add" to track)
    [root@web03 git_test]# git add a            # 把 a 文件添加到暂存区(git add 命令 是把文件从工作目录提交到暂存区;从 untracked 状态变成 staged 状态)
    [root@web03 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
    
    Untracked files:                                # 未跟踪文件
      (use "git add <file>..." to include in what will be committed)
    
        b
    [root@web03 git_test]# ll .git/
    total 36
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 branches
    -rw-r--r-- 1 root root   92 Jul  8 16:48 config
    -rw-r--r-- 1 root root   73 Jul  8 16:48 description
    -rw-r--r-- 1 root root   23 Jul  8 16:48 HEAD
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 hooks
    -rw-r--r-- 1 root root   96 Jul  9 00:49 index            # 暂存区的内容保存在这个 index 文件中
    drwxr-xr-x 2 root root 4096 Jul  8 16:48 info
    drwxr-xr-x 5 root root 4096 Jul  9 00:49 objects
    drwxr-xr-x 4 root root 4096 Jul  8 16:48 refs
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
        new file:   b
    [root@web03 git_test]# git rm --cached b            # 把 b 文件从暂存区移到工作目录中(从 staged 状态变成 untracked 状态 )
    rm 'b'
    [root@web03 git_test]# git status
    On branch master
    
    Initial commit
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
    
        new file:   a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        b
    
    [root@web03 git_test]# git rm -f a            # git rm -f 文件  ---> 把文件从暂存区和工作目录中同时删除
    rm 'a'
    [root@web03 git_test]# git status
    On branch master
    
    Initial commit
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        b
    
    nothing added to commit but untracked files present (use "git add" to track)
    [root@web03 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jul  9 00:46 b
    [root@web03 git_test]# git mv b b.txt            # 同时对工作区和暂存区的文件进行重命名
    [root@web03 git_test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        renamed:    b -> b.txt
    
    [root@web03 git_test]#
    
    # 查看文件修改内容
    [root@web03 git_test]# echo "bbb"  >>b.txt 
    [root@web03 git_test]# 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:   b.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@web03 git_test]# git diff b.txt         # git diff b.txt   --->  相对于暂存区,本地工作目录中的 b.txt 的内容有哪些变化
    diff --git a/b.txt b/b.txt
    index e69de29..f761ec1 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -0,0 +1 @@
    +bbb
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        modified:   b.txt
    
    [root@web03 git_test]# git diff b.txt 
    [root@web03 git_test]# git diff --cached b.txt         # 相比于本地仓库,暂存区内容有哪些变化
    diff --git a/b.txt b/b.txt
    index e69de29..f761ec1 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -0,0 +1 @@
    +bbb
    [root@web03 git_test]# git commit -m "modify b"
    [master 6f77fa9] modify b
     1 file changed, 1 insertion(+)
    [root@web03 git_test]# git diff --cached b.txt 
    [root@web03 git_test]# 
    
    # 查看所有的 提交
    [root@web03 git_test]# git log                        # 查看所有的 commit
    commit 6f77fa90ca72ad8d8d49edb708860e82d53e44ba
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 10:44:02 2019 +0800
    
        modify b
    
    commit dc597ee6acf95b49e15feeae6a7815ab4b0b6a17
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 10:20:45 2019 +0800
    
        rename from b to b.txt
    
    commit 69e60bfb3dbd89e411c52d0e7f651f393a3f6aff
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 09:24:25 2019 +0800
    
        commit b
    [root@web03 git_test]# git log --oneline        # 查看所有 commit 的简写
    6f77fa9 modify b
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git log --oneline --decorate        # 查看各个分支当前所指的提交对象(commit object)
    6f77fa9 (HEAD -> master) modify b
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git log -p        # 显示每次提交的完整信息(包括内容的变动)
    ...
    [root@web03 git_test]# git log -1        # 只显示最近的1条提交记录 (-2  就是最近的2条提交记录)
    commit 6f77fa90ca72ad8d8d49edb708860e82d53e44ba
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 10:44:02 2019 +0800
    
        modify b
    [root@web03 git_test]# 
    
    
    # 用暂存区覆盖工作目录中的文件内容
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# cat b.txt 
    bbb
    [root@web03 git_test]# echo "aaa" >>b.txt         # 修改工作区内容
    [root@web03 git_test]# git diff b.txt 
    diff --git a/b.txt b/b.txt
    index f761ec1..b47d0c0 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -1 +1,2 @@
     bbb
    +aaa
    [root@web03 git_test]# 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:   b.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")        
    [root@web03 git_test]# git checkout -- b.txt         # 撤回工作区 b.txt 内容的变化
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# cat b.txt 
    bbb
    [root@web03 git_test]# 
    
    
    # 本地仓库 覆盖 暂存区内容
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# echo "aaa">>b.txt 
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git diff --cached         # 相对于本地仓库,缓存区有哪些变化
    diff --git a/b.txt b/b.txt
    index f761ec1..b47d0c0 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -1 +1,2 @@
     bbb
    +aaa
    [root@web03 git_test]# git status
    On branch master
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)    # 用本地仓库的内容去覆盖缓存区
    
        modified:   b.txt
    
    [root@web03 git_test]# git reset HEAD b.txt     # 用本地仓库的内容去覆盖缓存区
    Unstaged changes after reset:
    M    b.txt
    [root@web03 git_test]# cat b.txt 
    bbb
    aaa
    [root@web03 git_test]# git diff --cached b.txt     # 相对于本地仓库,缓存区没有变化
    [root@web03 git_test]# git diff b.txt             # 相对于缓存区,工作区的内容有变化
    diff --git a/b.txt b/b.txt
    index f761ec1..b47d0c0 100644
    --- a/b.txt
    +++ b/b.txt
    @@ -1 +1,2 @@
     bbb
    +aaa
    [root@web03 git_test]# 
    
    
    # 回退到某一次 commit
    [root@web03 git_test]# cat b.txt 
    bbb
    aaa
    [root@web03 git_test]# git log
    commit 6f77fa90ca72ad8d8d49edb708860e82d53e44ba
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 10:44:02 2019 +0800
    
        modify b
    
    commit dc597ee6acf95b49e15feeae6a7815ab4b0b6a17
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 10:20:45 2019 +0800
    
        rename from b to b.txt
    
    commit 69e60bfb3dbd89e411c52d0e7f651f393a3f6aff
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 09:24:25 2019 +0800
    
        commit b
    [root@web03 git_test]# git reset --hard 69e60bfb3dbd89e        # 回退到某一次 commit (它之后的commit记录在 git log 中会找不到)
    HEAD is now at 69e60bf commit b
    [root@web03 git_test]# cat b 
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# git log                                # 它之后的 commit 通过 git log 命令已经找不到了
    commit 69e60bfb3dbd89e411c52d0e7f651f393a3f6aff
    Author: neo <andrewzheng@sina.cn>
    Date:   Tue Jul 9 09:24:25 2019 +0800
    
        commit b
    [root@web03 git_test]# git reflog                            # 显示仓库中所有的提交记录
    69e60bf HEAD@{0}: reset: moving to 69e60bfb3dbd89e
    6f77fa9 HEAD@{1}: commit: modify b
    dc597ee HEAD@{2}: commit: rename from b to b.txt
    69e60bf HEAD@{3}: commit (initial): commit b
    [root@web03 git_test]# git reset --hard dc597ee
    HEAD is now at dc597ee rename from b to b.txt
    [root@web03 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jul 10 01:06 b.txt
    [root@web03 git_test]# cat b.txt 
    [root@web03 git_test]# 
    
    # git log 会列出当前 commit 之前的所有提交记录; git reflog会列出本地仓库所有的历史提交记录

    git 分支

    # git分支的概念:
    Git的分支,从本质上来讲仅仅是指向提交对象的可变指针(相当于做了一次快照)。在这一点上Git和SVN有着本质区别。SVN的分支实际上就是一个目录;
    Git的默认分支名字是 master。在多次提交操作后,你其实已经有一个指向最后那个提交对象的 master 分支。它会在每次的提交操作中自动向前移动
    
    在实际开发项目中,尽量保证 master 分支稳定,公用于发布新版本,平时不要随便直接修改里面的数据文件;
    日常开发都在 dev 分支上。每个人从dev分支 创建自己的个人分支,开发完合并到dev分支,最后dev分支合并到master分支。

    示例如下:

    [root@web03 git_test]# git log --oneline --decorate
    dc597ee (HEAD -> master) rename from b to b.txt            # HEAD 可理解成 当前分支;HEAD在哪个分支上,哪个分支就是当前分支 
    69e60bf commit b
    [root@web03 git_test]# 
    
    
    # 创建分支
    [root@web03 git_test]# git branch testing        # 创建分支的命令
    [root@web03 git_test]# git branch                # 查看所有分支
    * master                                        # * 表示当前所在分支
      testing
    [root@web03 git_test]# git log --oneline --decorate
    dc597ee (HEAD -> master, testing) rename from b to b.txt    # 此时 master分支和testing分支都指向了 dc597ee 这次 commit;此时 master 和 testing 这两个分支里面的内容是一样的
    69e60bf commit b
    [root@web03 git_test]# git checkout testing        # git checkout 分支名 ---> 切换分支
    Switched to branch 'testing'
    [root@web03 git_test]# git branch
      master
    * testing        # 当前在 testing 分支上
    [root@web03 git_test]# git log --oneline --decorate
    dc597ee (HEAD -> testing, master) rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# touch test        # 在 testing 分支上创建一个test文件
    [root@web03 git_test]# git status
    On branch testing
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
    
        test
    
    nothing added to commit but untracked files present (use "git add" to track)
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git commit -m "commit test on branch testing"    # 此时提交到了 testing 分支上
    [testing 0289b8e] commit test on branch testing
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 test
    [root@web03 git_test]# git status
    On branch testing
    nothing to commit, working tree clean
    [root@web03 git_test]# ll        # 在 testing 分支上查看目录下的文件
    total 0
    -rw-r--r-- 1 root root 0 Jul 10 01:06 b.txt
    -rw-r--r-- 1 root root 0 Jul 11 01:46 test
    [root@web03 git_test]# git log --oneline --decorate
    0289b8e (HEAD -> testing) commit test on branch testing        # testing分支指向了 0289b8e 最新的一次commit;HEAD在 testing分支
    dc597ee (master) rename from b to b.txt                        # master 仍然是指向 dc597ee 这次提交
    69e60bf commit b
    [root@web03 git_test]# git checkout master        # 切换到 master 分支上
    Switched to branch 'master'
    [root@web03 git_test]# git branch
    * master
      testing
    [root@web03 git_test]# ll        # 在 master 分支上查看该目录下的文件:没有刚才创建的 test 文件
    total 0
    -rw-r--r-- 1 root root 0 Jul 10 01:06 b.txt
    
    
    # 分支合并
    [root@web03 git_test]# git log --oneline --decorate
    dc597ee (HEAD -> master) rename from b to b.txt            # 由于 master 分支的最后一次commit是在 testing 分支的最后一次commit之前,所以在 master 分支上看不到 testing 分支 上的最后一次 commit
    69e60bf commit b
    [root@web03 git_test]# git log --oneline --decorate
    0289b8e (HEAD -> testing) commit test on branch testing        # 在 testing 分支上能看到 master 分支上的最后一次 commit
    dc597ee (master) rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git checkout master
    Switched to branch 'master'
    [root@web03 git_test]# touch master
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git commit -m "commit master on branch master"    # 在 master 分支上 commit 一个 master 新文件; 此时 master 分支 和 testing 分支会产生 分叉
    [master 2edda3d] commit master on branch master
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 master
    [root@web03 git_test]# git log --oneline --decorate
    2edda3d (HEAD -> master) commit master on branch master
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# 
    
    # 把 testing 分支合并到 master 分支上,首先要回到 master 分支上
    [root@web03 git_test]# git branch
    * master        # 当前在 master 分支上 
      testing
    [root@web03 git_test]# git merge testing    # 把 testing 分支合并到 master 分支上 
    Merge branch 'testing'
    
    # Please enter a commit message to explain why this merge is necessary,
    # especially if it merges an updated upstream into a topic branch.
    #
    # Lines starting with '#' will be ignored, and an empty message aborts
    # the commit.
    merge testing branch to master branch        # 写 merger 时的 commit 信息
    merge test file to master branch
    ~                                                                                                                        
    ~                                                                                                                        
    ~                                                                                                                        
                                                                                                                         
    ".git/MERGE_MSG" 9L, 321C written
    Merge made by the 'recursive' strategy.
     test | 0
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 test
    [root@web03 git_test]# git branch
    * master
      testing
    [root@web03 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jul 10 01:06 b.txt
    -rw-r--r-- 1 root root 0 Jul 12 20:30 master
    -rw-r--r-- 1 root root 0 Jul 12 22:11 test        # master分支上也多出了 test 文件
    [root@web03 git_test]# git log --oneline --decorate
    d8d1b5e (HEAD -> master) Merge branch 'testing'        # merger 的时候也会伴随着 commit :把 master 和 testing 这两个分支上的内容合并后做了一次 提交
    2edda3d commit master on branch master
    0289b8e (testing) commit test on branch testing
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git checkout testing        # testing 分支上的内容没有任何变化
    Switched to branch 'testing'
    [root@web03 git_test]# ll
    total 0
    -rw-r--r-- 1 root root 0 Jul 10 01:06 b.txt
    -rw-r--r-- 1 root root 0 Jul 12 22:11 test
    [root@web03 git_test]# git log --oneline --decorate
    0289b8e (HEAD -> testing) commit test on branch testing
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# 
    
    # 由于两个分支上分别创建了一个 [不同的] 文件,所以上面的合并过程很顺利,因为此时没有 冲突
    
    # 两个分支对同一个文件(b.txt)做修改: 
    [root@web03 git_test]# git branch
    * master        # 当前在 master 分支上
      testing
    [root@web03 git_test]# cat b.txt     # 此时 b.txt 文件内容为空
    [root@web03 git_test]# git checkout master
    Switched to branch 'master'
    [root@web03 git_test]# echo "master" >> b.txt     # 在 master 分支上,向 b.txt 文件中 追加 "master"
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git commit -m "modify b.txt adding 'master' on master branch"    # 提交
    [master ef60afd] modify b.txt adding 'master' on master branch
     1 file changed, 1 insertion(+)
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# git log --oneline --decorate
    ef60afd (HEAD -> master) modify b.txt adding 'master' on master branch        # 上面 commit 的提交记录
    d8d1b5e Merge branch 'testing'
    2edda3d commit master on branch master
    0289b8e (testing) commit test on branch testing
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git checkout testing        # 切换到 testing 分支
    Switched to branch 'testing'
    [root@web03 git_test]# git branch
      master
    * testing
    [root@web03 git_test]# cat b.txt                     # 此时 b.txt 的内容也是空
    [root@web03 git_test]# echo "testing" >>b.txt         # 在 testing 分支上,向 b.txt 中追加 "testing"
    [root@web03 git_test]# git add .
    [root@web03 git_test]# git commit -m "modify b.txt adding 'testing' on testing branch"
    [testing 8bd36ae] modify b.txt adding 'testing' on testing branch
     1 file changed, 1 insertion(+)
    [root@web03 git_test]# git checkout master
    Switched to branch 'master'
    [root@web03 git_test]# git merge testing         # 把 testing 分支合并到 master 分支上
    Auto-merging b.txt
    CONFLICT (content): Merge conflict in b.txt        # 合并时发生了冲突
    Automatic merge failed; fix conflicts and then commit the result.
    [root@web03 git_test]# git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:   b.txt        # 两个分支都修改了 b.txt 文件;在有冲突的情况下,执行 merge 是不成功的;这种情况下,必须要人工手动解决 冲突
    
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@web03 git_test]# cat b.txt     # 查看此时 b.txt 文件内容如下
    <<<<<<< HEAD        # 当前分支(master)上的b.txt内容改动如下:(冲突的部分都会列出来)
    master
    =======
    testing                # testing 分支上的 b.txt 内容改动
    >>>>>>> testing
    [root@web03 git_test]# vim b.txt         # 手动解决冲突
    ...
    [root@web03 git_test]# cat b.txt        # 手动修改为如下:
    master
    testing
    [root@web03 git_test]# git status
    On branch master
    You have unmerged paths.
      (fix conflicts and run "git commit")
      (use "git merge --abort" to abort the merge)
    
    Unmerged paths:
      (use "git add <file>..." to mark resolution)
    
        both modified:   b.txt
    
    no changes added to commit (use "git add" and/or "git commit -a")
    [root@web03 git_test]# git add .        # 把修改后的文件提交
    [root@web03 git_test]# git commit -m "merge testing to master both modify b.txt"        # 把修改后的文件提交
    [master f169ba5] merge testing to master both modify b.txt
    [root@web03 git_test]# git status
    On branch master
    nothing to commit, working tree clean
    [root@web03 git_test]# git log --oneline --decorate
    f169ba5 (HEAD -> master) merge testing to master both modify b.txt        # 刚才上面merge后做的 commit 
    8bd36ae (testing) modify b.txt adding 'testing' on testing branch        # 在 testing 分支上做的 commit
    ef60afd modify b.txt adding 'master' on master branch                    # 在 master 分支上做的 commit 
    d8d1b5e Merge branch 'testing'
    2edda3d commit master on branch master
    0289b8e commit test on branch testing
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git branch
    * master
      testing
    [root@web03 git_test]# git branch -d testing        # 删除 testing 分支;合并后 testing 分支没用了,删除掉
    Deleted branch testing (was 8bd36ae).
    [root@web03 git_test]# 

    git 标签

    git 的标签 tag 也是指向某一次 commit 的不变指针
    前面回滚使用的是一串字符串,又长又难记。
    git tag v1.0   #→当前提交内容打一个标签(方便快速回滚),每次提交都可以打个tag。
    git tag          #→查看当前所有的标签
    git show v1.0   #→查看当前1.0版本的详细信息
    git tag v1.2 -m "version 1.2 release is test"  #→创建带有说明的标签,-a指定标签名字,-m指定说明文字
    git reset --hard v1.0    #->回到某一个版本(commit)
    git tag -d v1.0   #→我们为同一个提交版本设置了两次标签,删除之前的v1.0

    示例如下:

    [root@web03 git_test]# git log --oneline
    f169ba5 merge testing to master both modify b.txt
    8bd36ae modify b.txt adding 'testing' on testing branch
    ef60afd modify b.txt adding 'master' on master branch
    d8d1b5e Merge branch 'testing'
    2edda3d commit master on branch master
    0289b8e commit test on branch testing
    dc597ee rename from b to b.txt
    69e60bf commit b
    [root@web03 git_test]# git tag -a v1.0 -m "this is v1.0 version"    # 没有指定 commit 时就是给当前(最后一次)commit 打标签
    [root@web03 git_test]# git tag
    v1.0
    [root@web03 git_test]# git tag -a v2.0 d8d1b5e -m "this is v2.0 version"    # 针对某一次 commit 打标签; d8d1b5e 是 commit ID
    [root@web03 git_test]# git tag
    v1.0
    v2.0
    [root@web03 git_test]# git show v1.0    # 查看某一次 commit 的详细内容
    tag v1.0
    Tagger: neo <andrewzheng@sina.cn>
    Date:   Fri Jul 19 02:00:49 2019 +0800
    
    this is v1.0 version
    
    commit f169ba560b6d6908c74395e4b6d59ba14fc908d7
    Merge: ef60afd 8bd36ae
    Author: neo <andrewzheng@sina.cn>
    Date:   Fri Jul 12 23:30:28 2019 +0800
    
        merge testing to master both modify b.txt
    
    diff --cc b.txt
    index 1f7391f,038d718..bde2aff
    --- a/b.txt
    +++ b/b.txt
    @@@ -1,1 -1,1 +1,2 @@@
     +master
    + testing
    [root@web03 git_test]# git tag -d v2.0        # 删除tag标签
    Deleted tag 'v2.0' (was a82593c)
    [root@web03 git_test]# git tag
    v1.0
    [root@web03 git_test]# 
  • 相关阅读:
    elasticsearch安装教程
    mysql设置账号密码及授权
    mongodb设置账号密码授权案例
    新安装的centos 6.5,不能上网,外网ping不通,内网可以ping通解决方法
    docker-compose安装教程
    解决github图片不显示问题
    网站宽带计算方式
    thinkphp 如何实现url的rewrite
    nginx Https配置
    Ext.Net导入Excel
  • 原文地址:https://www.cnblogs.com/neozheng/p/11043081.html
Copyright © 2011-2022 走看看