gitlab之git基础

gitlab之git基础

git基础安装

安装配置

  1. yum安装

    [root@git-client ~]# yum install -y git
       
    [root@git-client ~]# git --version
    git version 1.8.3.1
    
  2. 配置全局git账户

    配置username和email,和将要同步的远端git型server账户一致,(如公网gitlab、github的注册账户,私有gitlab仓库的注册账户等)

    这里采用了公网gitlab上注册的账户和邮箱信息

    [root@git-client ~]# git config --global --list
    fatal: unable to read config file '/root/.gitconfig': No such file or directory
    [root@git-client ~]# git config --global user.name "ice_latte"
    [root@git-client ~]# git config --global user.email "xx@163.com"
    [root@git-client ~]# git config --global --list
    user.name=ice_latte
    user.email=xx@163.com
    
  3. 配置认证方法

    一般采用ssh-key免密认证方式,认证到远程的git服务器,如gitlab

    另一种方式为git基于https的认证,不推荐

    [root@git-client ~]# ssh-keygen -t rsa
    Generating public/private rsa key pair.
       
    采用如上密码,本地开发机,生成密钥对,然后在~/.ssh/,当前用户的.ssh目录下,找到pub结尾的公钥文件,
    登陆gitlab网页,找到ssh设置选择,将公钥字符串贴入其中即可
    
  4. 验证

    验证是否在个人客户端、和gitlab之间建立连接,采用如下命令,若是私有仓库,gitlab.com要替换为适当的私有域名或ip

    [root@git-client ~]# ssh -T git@gitlab.com
    The authenticity of host 'gitlab.com (172.65.251.78)' can't be established.
    ECDSA key fingerprint is SHA256:HbW3g8zUjNSksFbqTiUWPWg2Bq1x8xdGUrliXFzSnUw.
    ECDSA key fingerprint is MD5:f1:d0:fb:46:73:7a:70:92:5a:ab:5d:ef:43:e2:1c:35.
    Are you sure you want to continue connecting (yes/no)? yes
    Warning: Permanently added 'gitlab.com,172.65.251.78' (ECDSA) to the list of known hosts.
    Welcome to GitLab, @ice_latte!
    

git术语

对于gitlab来说,namespace:有2类

repo仓库:

​ 相当于一个目录,里面存放的文本文件,都接受git的追踪、管理;

  • 本地仓库:新建一个目录,然后git init就是;
  • 远程仓库:注册的github、gitee这种账户,点击新建repo就是;
  • 本地仓库和远程仓库可以相关联,本地仓库可以推送一到多个远程仓库做备份,或分享开源之用;

git基础使用

git只能追踪文件文件的内容变化,如code,网页代码,word这种属于二进制文件,无法追踪其内容变化;

创建本地版本库repo

  1. 初始化git目录

    [root@git-client git-demo]# pwd
    /git-demo
       
    [root@git-client git-demo]# git init
    Initialized empty Git repository in /git-demo/.git/
       
    [root@git-client git-demo]# ll -a
    total 0
    drwxr-xr-x   3 root root  18 Dec  2 14:09 .
    dr-xr-xr-x. 18 root root 240 Dec  2 14:09 ..
    drwxr-xr-x   7 root root 119 Dec  2 14:09 .git
       
    创建一个新的空的目录,做本地repo目录,git init即可初始化,之后生成.git目录为仓库追踪元信息;
    
  2. 编辑2个新文件

    1
    2
    3
    4
    5
    6
    7
    8
    
    [root@git-client git-demo]# vim file1.txt
    [root@git-client git-demo]# cat file1.txt 
    hello git
    I am the one!
       
    [root@git-client git-demo]# vim file2.txt
    [root@git-client git-demo]# cat file2.txt 
    linux user
    
  3. git添加

    git add会将指定的文件,添加到git缓存区,需要指定文件名,file2未添加,所以commit时只会有file1

    1
    2
    
       
    [root@git-client git-demo]# git add file1.txt 
    
  4. git提交

    git commit会把上次commit之后add的文件,即在git缓存区的文件提交到本地仓库,

    提示信息:

    为主分支,root用户提交

    添加了一个文件,里面有2行插入

    -m 为注释信息,要意义明确

    1
    2
    3
    4
    5
    6
    7
    8
    
    [root@git-client git-demo]# git commit -m "my first commit,include one file2"
    [master (root-commit) af61fed] my first commit,include two file2
     1 file changed, 2 insertions(+)
     create mode 100644 file1.txt
    [root@git-client git-demo]# ll
    total 8
    -rw-r--r-- 1 root root 24 Dec  2 14:15 file1.txt
    -rw-r--r-- 1 root root 11 Dec  2 14:16 file2.txt
    

修改文件并查看

  1. 修改文件内容

    第一行末尾,添加了cool!

    1
    2
    3
    
    [root@git-client git-demo]# cat file1.txt 
    hello git cool!
    I am the one!
    
  2. git status查看当前工作区状态

    可以看出是否有变化,提示信息为:主分支、file1.txt发生了修改,还没提交

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    [root@git-client git-demo]# 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:   file1.txt
    #
    no changes added to commit (use "git add" and/or "git commit -a")
       
    
  3. git diff查看两次变化信息

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    [root@git-client git-demo]# git diff
    diff --git a/file1.txt b/file1.txt
    index bf68a56..88f7199 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1,2 +1,2 @@
    -hello git
    +hello git cool!
     I am the one!
       
    可以看出,变化为加了一个cool!
    
  4. 确认无误后、提交变化后文件

    确认是自己想要的文件修改后,(不是误修改),git add 、git commit再次提交;

    此时查看git status状态为“干净的”

    1
    2
    3
    4
    5
    6
    7
    
    [root@git-client git-demo]# git add file1.txt 
    [root@git-client git-demo]# git commit -m 'i confirm'
    [master 409066c] i confirm
     1 file changed, 1 insertion(+), 1 deletion(-)
    [root@git-client git-demo]# git status
    # On branch master
    nothing to commit, working directory clean
    

总结:

  1. 修改文件后,
  2. git status可以查看当前工作区状态,查看距离上次commit后,有无变化,有哪些变化;
  3. git diff可以看到文件变化的内容,输出格式为diff格式
  4. 确认修改后,可以再次add,然后commit

版本回退

  1. 再次修改file1.txt,目前共有3个版本;

    除去添加file2的提交,仅仅针对file1而言,共有3次提交;3个文件版本;

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
    [root@git-client git-demo]# git log
    commit 2ba3654be407a797f9267a9bf4f334189338f558
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:49:53 2020 +0800
       
        third modified
       
    commit 409066ced43ef1a71c5c29d8285ff1eec6e2d2b7
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:35:36 2020 +0800
       
        i confirm
       
    commit c5adcc0311e259847ddf4198b0b16626eb5b54c8
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:32:25 2020 +0800
       
        add file2.txt
       
    commit af61fed1ff436427946d59776de248e9eecc4936
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:16:45 2020 +0800
       
        my first commit,include two file2
           
    ##简洁输出参数,第一列为提交的hash码,也是标识id,前7个字符是短格式
    [root@git-client git-demo]# git log --pretty=oneline
    2ba3654be407a797f9267a9bf4f334189338f558 third modified
    409066ced43ef1a71c5c29d8285ff1eec6e2d2b7 i confirm
    c5adcc0311e259847ddf4198b0b16626eb5b54c8 add file2.txt
    af61fed1ff436427946d59776de248e9eecc4936 my first commit,include two file2
    
  2. 回退到第二个版本;

    [root@git-client git-demo]# cat file1.txt 
    hello git cool!
    I am the one!
    third modified,add one line.
       
    # HEAD^为上个版本,HEAD^^为上2个版本,HEAD~100为上一百个版本
    # 也可以根据commit的id回退到指定版本,id用git log或git reflog获得
       
    [root@git-client git-demo]# git reset --hard HEAD^
    HEAD is now at 409066c i confirm
       
    [root@git-client git-demo]# cat file1.txt 
    hello git cool!
    I am the one!
    
  3. 再次回到最新的第三个版本;

    [root@git-client git-demo]# git log
    commit 409066ced43ef1a71c5c29d8285ff1eec6e2d2b7
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:35:36 2020 +0800
       
        i confirm
       
    commit c5adcc0311e259847ddf4198b0b16626eb5b54c8
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:32:25 2020 +0800
       
        add file2.txt
       
    commit af61fed1ff436427946d59776de248e9eecc4936
    Author: ice_latte <boogies@163.com>
    Date:   Wed Dec 2 14:16:45 2020 +0800
       
        my first commit,include two file2
       
    # git log看到的是提交日志,回退后,新版本的id就丢失
    # git reflog看到的是操作日志,可以看到所有的id
       
    [root@git-client git-demo]# git reflog
    409066c HEAD@{0}: reset: moving to HEAD^
    2ba3654 HEAD@{1}: commit: third modified
    409066c HEAD@{2}: commit: i confirm
    c5adcc0 HEAD@{3}: commit: add file2.txt
    af61fed HEAD@{4}: commit (initial): my first commit,include two file2
       
       
    # 再根据id,回到第三个版本即可
    [root@git-client git-demo]# git reset --hard 2ba3654
    HEAD is now at 2ba3654 third modified
    [root@git-client git-demo]# cat file1.txt 
    hello git cool!
    I am the one!
    third modified,add one line.
    

文件修改提交的过程

git追踪的不是文件、而是文件的修改!

三个区:

  • 工作区
  • 暂存区
  • master分支
  1. 新加的文件,或已经挂在分支下,但又有新修改的文件,需要git add将其修改添加到暂存区(本质都是新的修改)
  2. 添加到暂存区 staged的修改 ,是准备提交到master分支上的,git commit提交;

image-20201202153545857

测试:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
[root@git-client git-demo]# vim file2.txt 
[root@git-client git-demo]# git add file2.txt
[root@git-client git-demo]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   file2.txt
#
[root@git-client git-demo]# git commit -m "file2 add two-line"
[master 4285c1a] file2 add two-line
 1 file changed, 1 insertion(+)
 
 # 修改file2后,先add,后commit,后即可;



[root@git-client git-demo]# vim file2.txt 
[root@git-client git-demo]# git commit -m "file2 add three-line"
# On branch master
# 提示看出,还没有提交到staged阶段,为commit做准备,
# staged就是暂存区
# 需要先add
# 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:   file2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

# 再次修改file2,跳过add,直接commit不行,因为又是一个新的修改,需要先add!

总结:即每一个修改(修改原有文件、或添加新文件),都要先add到暂存区,再commit到分支上,多次修改,可以最后只一个add,一个commit;

撤销修改

分三种情况

  • 只是工作区修改,修改后并未git add;
  • 修改到了暂存区,修改后git add添加到了暂存区;
  • 修改提交到了版本库中,修改后git add、git commit到了分支上;(但没有推送到远程库)

修改在工作区

[root@git-client git-demo]# 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:   file2.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

# 修改file2,发现是不想要的修改,在未提交到staged时,可以用checkout撤销修改
# git status都有对应提示
# 撤销后,刚刚修改丢失

[root@git-client git-demo]# git checkout -- file2.txt
[root@git-client git-demo]# git status
# On branch master
nothing to commit, working directory clean

[root@git-client git-demo]# cat file2.txt 
linux user
two line

修改到了暂存区

[root@git-client git-demo]# vim file2.txt 

[root@git-client git-demo]# git add file2.txt 
[root@git-client git-demo]# git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   file2.txt

修改后还add了,,根据提示撤销,分2步:

[root@git-client git-demo]# git reset HEAD file2.txt 
Unstaged changes after reset:
M	file2.txt

[root@git-client git-demo]# git checkout -- file2.txt 
[root@git-client git-demo]# git status
# On branch master
nothing to commit, working directory clean

修改到了版本库

[root@git-client git-demo]# vim file2.txt 
[root@git-client git-demo]# git add file2.txt 
[root@git-client git-demo]# git commit -m 'file2 add three-line'
[master cef9515] file2 add three-line
 1 file changed, 1 insertion(+)
[root@git-client git-demo]# cat file2.txt 
linux user
two line
three-line

[root@git-client git-demo]# git log
commit cef95158395c8843bd694ceadf7ef7e78c5c8f7b
Author: ice_latte <boogies@163.com>
Date:   Wed Dec 2 16:07:16 2020 +0800

    file2 add three-line

commit 4285c1a19de268ab63c63200a6990c316fc86580
...

# 修改后,add,还commit了,此时就属于版本回退了,
# git log找到上一个commit的id,然后reset到那个版本即可;

[root@git-client git-demo]# git reset --hard 4285c1a19de268ab63c63200a6990c316fc86580
HEAD is now at 4285c1a file2 add two-line
[root@git-client git-demo]# cat file2.txt 
linux user
two line

删除文件

  1. 工作区、先直接rm删除需要的文件
    1. 若确认删除,版本库中文件也继续删除
    2. 若为误删除,需要从版本库中恢复该文件到工作区
  2. ps:
    1. 只有之前提交过到版本库(分支)后的可以恢复
    2. 恢复后,最近一次commit后的修改会丢失

实验:

1,添加新文件f3,并加到版本库中

​ 添加、删除都是修改

[root@git-client git-demo]# touch f3

[root@git-client git-demo]# git add f3

[root@git-client git-demo]# git commit -m "add f3"
[master 9357e75] add f3
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 f3
[root@git-client git-demo]# git status
# On branch master
nothing to commit, working directory clean

2,删除f3后,并删除版本库中f3

[root@git-client git-demo]# rm -rf f3 

[root@git-client git-demo]# git rm f3
rm 'f3'

[root@git-client git-demo]# git commit -m 'delete f3'

3,删除f3后,发现误删除,要恢复

​ 多git status查看状态,都有对应提示;

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

[root@git-client git-demo]# ll
total 8
-rw-r--r-- 1 root root  0 Dec  2 16:24 f3

远程仓库

本地上传到github

  1. 创建github仓库
  2. 添加本地机器生成的公钥到github的ssh-key
  3. github创建一个仓库
  4. 上三步略...

将本地仓库关联到新建立的github仓库;下为新建仓库后,给出的提示命令

git remote add origin git@github.com:ice-latte/git-demo.git
添加远程库信息
git branch -M main
创建main分支
git push -u origin main
第一次-u参数,将main分支关联到本地的master分支,origin为远程仓库的名字
先验证github联通性
[root@git-client git-demo]# ssh -T git@github.com
Hi ice-latte! You've successfully authenticated, but GitHub does not provide shell access.

根据提示命令执行即可;
[root@git-client git-demo]# git remote add origin git@github.com:ice-latte/git-demo.git
[root@git-client git-demo]# git branch -M main
[root@git-client git-demo]# git push -u origin main
Counting objects: 26, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (20/20), done.
Writing objects: 100% (26/26), 2.15 KiB | 0 bytes/s, done.
Total 26 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), done.
To git@github.com:ice-latte/git-demo.git
 * [new branch]      main -> main
Branch main set up to track remote branch main from origin.

推送后,github可以看到相应文件,之后再提交到本地的修改,直接git push orgin main即可;不再需要u参数

image-20201202173741140

github仓库克隆到本地

github创建好仓库后,直接git clone即可

[root@git-client /]# git clone git@github.com:ice-latte/git-demo2.git
Cloning into 'git-demo2'...
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (4/4), 4.49 KiB | 0 bytes/s, done.
[root@git-client /]# ll /git-demo2/
total 16
-rw-r--r-- 1 root root 11357 Dec  2 17:47 LICENSE
-rw-r--r-- 1 root root    11 Dec  2 17:47 README.md

分支管理

分支的管理,本质就是指针的切换,创建与删除;参考图示

创建、合并分支

  1. 查看当前分支,并创建新分支并同时切换,*表示的是当前分支位置

    [root@git-client git-demo2]# git branch
    * main
    [root@git-client git-demo2]# git checkout -b dev
    Switched to a new branch 'dev'
    [root@git-client git-demo2]# git branch
    * dev
      main
    
  2. 先创建分支,再切换

    [root@git-client git-demo2]# git branch test
    [root@git-client git-demo2]# git checkout test
    Switched to branch 'test'
    [root@git-client git-demo2]# git branch
      dev
      main
    * test
    [root@git-client git-demo2]# git checkout dev
    Switched to branch 'dev'
    
  3. 在dev分支开发继续

    在dev分支,开发的code1,提交后,再切换为main,看不到code1

    [root@git-client git-demo2]# touch code1
    [root@git-client git-demo2]# git add code1
    [root@git-client git-demo2]# git commit -m 'dev code1'
    [dev 155bb55] dev code1
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 code1
    [root@git-client git-demo2]# ll
    total 16
    -rw-r--r-- 1 root root     0 Dec  2 18:08 code1
    -rw-r--r-- 1 root root 11357 Dec  2 17:47 LICENSE
    -rw-r--r-- 1 root root    11 Dec  2 17:47 README.md
    [root@git-client git-demo2]# git checkout main
    Switched to branch 'main'
    [root@git-client git-demo2]# ll
    total 16
    -rw-r--r-- 1 root root 11357 Dec  2 17:47 LICENSE
    -rw-r--r-- 1 root root    11 Dec  2 17:47 README.md
       
    
  4. 合并dev分支的内容到main主分支,

    其实就是将main分支的指针指向了dev,fast-forward为一种合并模式,就是只切换了指针指向,

    合并后,看到了code1文件

    先要切换到main分支,且merge其他分支

    [root@git-client git-demo2]# git merge dev
    Updating 012d8ac..155bb55
    Fast-forward
     code1 | 0
     1 file changed, 0 insertions(+), 0 deletions(-)
     create mode 100644 code1
    [root@git-client git-demo2]# ll
    total 16
    -rw-r--r-- 1 root root     0 Dec  2 18:12 code1
    -rw-r--r-- 1 root root 11357 Dec  2 17:47 LICENSE
    -rw-r--r-- 1 root root    11 Dec  2 17:47 README.md
       
    
  5. 删除dev分支

    [root@git-client git-demo2]# git branch -d dev
    Deleted branch dev (was 155bb55).
    [root@git-client git-demo2]# git branch
    * main
      test
       
    
  6. 新的分支命令

    git switch -c dev2 创建并切换到dev2分支
    git switch master 切换到master分支
    

git推荐,拉取新分支进行开发功能,对分支代码确认后,再merge到主分支,效果一致,且更安全,

解决冲突

​ 一般情况下,多人协同开发时,都是各自开发不同的模块,操作不同的文本文件、各自拉取的分支在合并后,只是不同模块代码文件的合并;fast-forward合并模式基本可以应付;

​ 但当有多个分支的开发人员操作了同一个文件,且修改了同一行的,且修改不同,此时自动合并就会报出冲突,需要合并时 ,手动解决冲突:就是在多份修改不同时,敲定一个版本,然后提交

测试:

  1. 先拉取一个新特性分支feature1

    1. 新分支上修改file1,并在第五行添加 is cool

      [root@git-client git-demo]# git branch
      * main
      [root@git-client git-demo]# git checkout -b feature1
      Switched to a new branch 'feature1'
      [root@git-client git-demo]# git branch
      * feature1
        main
      [root@git-client git-demo]# ll
      total 8
      -rw-r--r-- 1 root root  0 Dec  2 16:24 f3
      -rw-r--r-- 1 root root  0 Dec  2 17:41 f4
      -rw-r--r-- 1 root root 79 Dec  2 15:20 file1.txt
      -rw-r--r-- 1 root root 20 Dec  2 16:08 file2.txt
      [root@git-client git-demo]# vim file1.txt 
      [root@git-client git-demo]# cat file1.txt 
      hello git cool!
      I am the one!
      third modified,add one line.
      four line
      five line is cool.
      
    2. git add

    3. git commit提交

      [root@git-client git-demo]# git add file1.txt 
      [root@git-client git-demo]# git commit -m "file1 add is cool"
      [feature1 043bf76] file1 add is cool
       1 file changed, 1 insertion(+), 1 deletion(-)
      [root@git-client git-demo]# git status
      # On branch feature1
      nothing to commit, working directory clean
      
  2. 切换回main主分支

    1. 此时查看file1,第五行未添加任何内容

      [root@git-client git-demo]# git checkout main
      Switched to branch 'main'
      [root@git-client git-demo]# git status
      # On branch main
      nothing to commit, working directory clean
      [root@git-client git-demo]# cat file1.txt 
      hello git cool!
      I am the one!
      third modified,add one line.
      four line
      five line
      
    2. 修改file1,并在第五行添加 is not cool

    3. git add

    4. git commit提交

      [root@git-client git-demo]# vim file1.txt 
      [root@git-client git-demo]# git status
      # On branch main
      # 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:   file1.txt
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      [root@git-client git-demo]# git add file1.txt
      [root@git-client git-demo]# git commit -m 'file1 add is not cool'
      [main c246768] file1 add is not cool
       1 file changed, 1 insertion(+), 1 deletion(-)
      
  3. 开始合并

    1. 在main主分支上,合并feature1上的修改

    2. git branch merge feature1时报错,报合并冲突,因为2个分支都修改了同一个文件,同一行;

      [root@git-client git-demo]# git merge feature1
      Auto-merging file1.txt
      CONFLICT (content): Merge conflict in file1.txt
      Automatic merge failed; fix conflicts and then commit the result.
      [root@git-client git-demo]# git status
      # On branch main
      # Your branch is ahead of 'origin/main' by 1 commit.
      #   (use "git push" to publish your local commits)
      #
      # You have unmerged paths.
      #   (fix conflicts and run "git commit")
      #
      # Unmerged paths:
      #   (use "git add <file>..." to mark resolution)
      #
      #	both modified:      file1.txt
      #
      no changes added to commit (use "git add" and/or "git commit -a")
      
  4. 解决冲突

    1. 在main主分支上,查看file1内容,会有冲突行的提示

      [root@git-client git-demo]# cat file1.txt 
      hello git cool!
      I am the one!
      third modified,add one line.
      four line
      <<<<<<< HEAD
      five line is not cool.
      =======
      five line is cool.
      >>>>>>> feature1
      
    2. 在main主分支上,编辑file1,敲定冲突行到底选定那个分支的修改,或综合一下

      [root@git-client git-demo]# vim file1.txt 
            
      
  5. 解决冲突后提交

    1. git add

    2. git commit提交即可

      [root@git-client git-demo]# git add file1.txt 
      [root@git-client git-demo]# git commit -m "conflict fixed,real cool"
      [main 10fc0d3] conflict fixed,real cool
      [root@git-client git-demo]# git status
      # On branch main
      # Your branch is ahead of 'origin/main' by 3 commits.
      #   (use "git push" to publish your local commits)
      #
      nothing to commit, working directory clean
      
  6. 查看提交日志

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    [root@git-client git-demo]# git log --graph --pretty=oneline --abbrev-commit
    *   10fc0d3 conflict fixed,real cool
    |\  
    | * 043bf76 file1 add is cool
    * | c246768 file1 add is not cool
    |/  # 这块儿看到,2个分支,冲突,最后解决了冲突再merge
    * d90cc73 add f4
    * cf3d7d1 add f3
    * 3837d88 delete f3
    * 9357e75 add f3
    * 4285c1a file2 add two-line
    * 74a3395 yes
    * b9c43df add four line
    * 2ba3654 third modified
    * 409066c i confirm
    * c5adcc0 add file2.txt
    * af61fed my first commit,include two file2
    
  7. ​ 删除feature1分支

    [root@git-client git-demo]# git branch
      feature1
    * main
    [root@git-client git-demo]# git branch -d feature1
    Deleted branch feature1 (was 043bf76).
    [root@git-client git-demo]# git branch
    * main
    

分支管理策略

​ 默认分支合并策略为fast-forwarding,也可以用--no-ff参数禁用fast-forwarding合并策略;采用普通合并模式;

  1. 拉取新分支dev

    1. 修改f3文件

      [root@git-client git-demo]# git checkout -b dev
      Switched to a new branch 'dev'
      [root@git-client git-demo]# git branch
      * dev
        main
      [root@git-client git-demo]# vim f3 
      
    2. add、commit2步提交

      [root@git-client git-demo]# git add f3
            
      [root@git-client git-demo]# git commit -m 'f3 add '
      [dev 6dd5183] f3 add
       1 file changed, 1 insertion(+)
            
      
  2. 切换回main分支

    [root@git-client git-demo]# git status
    # On branch dev
    nothing to commit, working directory clean
    [root@git-client git-demo]# git checkout main
    Switched to branch 'main'
    Your branch is ahead of 'origin/main' by 3 commits.
      (use "git push" to publish your local commits)
    
    1. 用no-ff模式合并dev

      [root@git-client git-demo]# git merge --no-ff -m 'test no-ff merge' dev
      Merge made by the 'recursive' strategy.
       f3 | 1 +
       1 file changed, 1 insertion(+)
      
    2. 查看git log图形

      [root@git-client git-demo]# git log --graph --pretty=oneline --abbrev-commit
      *   e9a12ff test no-ff merge
      |\  
      | * 6dd5183 f3 add
      |/  
      *   10fc0d3 conflict fixed,real cool
      |\  
      | * 043bf76 file1 add is cool
      * | c246768 file1 add is not cool
      |/  
      * d9
      

开发时分支策略:

  • master应保持稳定,只有在发布新版本时使用,将dev分支的代码合并到master然后发布;
  • 在master初始化后拉取一个dev分支,作为各个开发人员开发后汇总的分支;
  • 在dev拉取各个开发人员自己的分支,根据需要合并,

图示:

​ 各个开发人员开发分支示意,dev分支汇总各开发的代码,测试通过后,再merge到master上发版;

image-20201203112610865

bug分支

​ 假定,正在dev分支进行开发,开发到中途,上一版master的发版有bug,需要立刻修复,方法:保存dev分支现场,切换到master,拉取bug修复分支,然后切换到master合并bug分支,最后切换为dev,还原现场,继续开发;

  1. 保存dev现场

    [root@git-client git-demo2]# touch code3
    [root@git-client git-demo2]# git add code3 
    [root@git-client git-demo2]# git status
    # On branch dev
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	new file:   code3
    #
    此时code3还未提交,未完成也不能提交,只能先保存现场;
    [root@git-client git-demo2]# git stash
    Saved working directory and index state WIP on dev: df5c057 add code2
    HEAD is now at df5c057 add code2
    [root@git-client git-demo2]# git status
    # On branch dev
    nothing to commit, working directory clean
       
    git stash保存现场,保存后,工作目录是干净的
    
  2. 切换到master

    [root@git-client git-demo2]# git checkout main
    Switched to branch 'main'
    Your branch is ahead of 'origin/main' by 2 commits.
      (use "git push" to publish your local commits)
    [root@git-client git-demo2]# git status
    # On branch main
    # Your branch is ahead of 'origin/main' by 2 commits.
    #   (use "git push" to publish your local commits)
    #
    nothing to commit, working directory clean
    
  3. 拉取bug修复分支,并修复

    [root@git-client git-demo2]# git checkout -b bugfix
    Switched to a new branch 'bugfix'
    [root@git-client git-demo2]# ll
    total 20
    -rw-r--r-- 1 root root     4 Dec  3 15:02 code1
    -rw-r--r-- 1 root root 11357 Dec  2 17:47 LICENSE
    -rw-r--r-- 1 root root    11 Dec  2 17:47 README.md
    [root@git-client git-demo2]# cat code1 
    bug
       
    # 假定code1中的bug删除,就为bug修复操作;
       
    [root@git-client git-demo2]# > code1 
    [root@git-client git-demo2]# cat code1 
    [root@git-client git-demo2]# git add code1 
    [root@git-client git-demo2]# git commit -m 'fixed bug'
    [bugfix ba43126] fixed bug
     1 file changed, 1 deletion(-)
    [root@git-client git-demo2]# git status
    # On branch bugfix
    nothing to commit, working directory clean
    
  4. 切换到master,合并bug修复分支

    [root@git-client git-demo2]# git checkout main
    Switched to branch 'main'
    Your branch is ahead of 'origin/main' by 2 commits.
      (use "git push" to publish your local commits)
    [root@git-client git-demo2]# git merge --no-ff -m 'merge bug fixed' bugfix
    Merge made by the 'recursive' strategy.
     code1 | 1 -
     1 file changed, 1 deletion(-)
       
    
  5. 切换为dev,恢复现场

    [root@git-client git-demo2]# git checkout dev
    Switched to branch 'dev'
    [root@git-client git-demo2]# git status
    # On branch dev
    nothing to commit, working directory clean
       
    # 列出之前保存的现场
    [root@git-client git-demo2]# git stash list
    stash@{0}: WIP on dev: df5c057 add code2
       
    # 用git stash pop恢复
    [root@git-client git-demo2]# git stash pop
    # On branch dev
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	new file:   code3
    #
    Dropped refs/stash@{0} (985929981fb0112131abe194d466ae595d166c66)
    [root@git-client git-demo2]# git stash list
    [root@git-client git-demo2]# git status
    # On branch dev
    # Changes to be committed:
    #   (use "git reset HEAD <file>..." to unstage)
    #
    #	new file:   code3
    
  6. (重放,修复dev中隐含的相同的bug)

    dev是从master拉取的分支,master的bug在dev分支同样存在,可以采用重放的方式,快速修复dev中的bug;

    [root@git-client git-demo2]# git cherry-pick ba43126
    [dev 8f58ed9] fixed bug
     1 file changed, 1 deletion(-)
    [root@git-client git-demo2]# cat code1 
       
    找到修改bug的那个commit的id,git cherry-pick ID即可,前提,当前git status要是干净的;
    

feature分支

1、拉取用于新的特性开发的分支,并做了commit

[root@git-client git-demo2]# git checkout -b feature
Switched to a new branch 'feature'

[root@git-client git-demo2]# touch func1
[root@git-client git-demo2]# git add func1 
[root@git-client git-demo2]# git commit -m 'add func1'
[feature f40650c] add func1
 2 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 func1

2、切换回dev上,若要删除刚刚的分支的话,对于已经commit但未合并的分支,删除需要D参数,

[root@git-client git-demo2]# git status
# On branch feature
nothing to commit, working directory clean
[root@git-client git-demo2]# git checkout dev
Switched to branch 'dev'
[root@git-client git-demo2]# git branch -d feature
error: The branch 'feature' is not fully merged.
If you are sure you want to delete it, run 'git branch -D feature'.
[root@git-client git-demo2]# git branch -D feature
Deleted branch feature (was f40650c).
[root@git-client git-demo2]# git branch
* dev
  main

多人协作

推送分支:除主分支外,其余分支可以根据需要推送;

​ 多人协助时,一般都有一个远程仓库,做汇总,和远程仓库同步时,需要指定远程的仓库名,(是仓库在本地的标识名,不一定是创建仓库时的名字)

​ git remote add origin git@github.com:ice-latte/git-demo.git,origin就是远程仓库在本地的标识名,默认是origin,当有多个远程库要同步时,要定义不同的名字区分开,

# 查看远程仓库的信息

[root@git-client git-demo2]# git remote
origin
[root@git-client git-demo2]# git remote -v
origin	git@github.com:ice-latte/git-demo2.git (fetch)
origin	git@github.com:ice-latte/git-demo2.git (push)

# 将本地的main分支,对应远程的主分支master,推送上去;

[root@git-client git-demo2]# git push origin main
Counting objects: 13, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (10/10), done.
Writing objects: 100% (12/12), 1003 bytes | 0 bytes/s, done.
Total 12 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To git@github.com:ice-latte/git-demo2.git
   012d8ac..1dc27c7  main -> main

# 将本地的dev分支,推送上去,远程也将创建一个dev分支;
[root@git-client git-demo2]# git push origin dev
Total 0 (delta 0), reused 0 (delta 0)
remote: 
remote: Create a pull request for 'dev' on GitHub by visiting:
remote:      https://github.com/ice-latte/git-demo2/pull/new/dev
remote: 
To git@github.com:ice-latte/git-demo2.git
 * [new branch]      dev -> dev

推送分支:

  1. 推送自己本地分支提交的更新时,
  2. 若推送失败,提示远程库较新,需要先把远程库pull下来合并,然后再push
  3. 若合并有冲突,就手动解决冲突,然后提交,然后再push
# 下为提示,push被拒绝,因为,远程库较新,有别人先push了,提示要先pull下来跟自己合并,
# 若修改了冲突的行,还需解决冲突,然后再push

[root@git-client git-demo2]# git push origin dev
Warning: Permanently added the RSA host key for IP address '192.30.255.113' to the list of known hosts.
To git@github.com:ice-latte/git-demo2.git
 ! [rejected]        dev -> dev (fetch first)
error: failed to push some refs to 'git@github.com:ice-latte/git-demo2.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first merge the remote changes (e.g.,
hint: 'git pull') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
updatedupdated2020-12-072020-12-07
加载评论