美文网首页
极客时间 Git 学习笔记

极客时间 Git 学习笔记

作者: ragpo | 来源:发表于2020-06-08 15:54 被阅读0次

    01 | 课程综述

    1
    2
    3
    4

    02 | 安装Git

    1
    https://git-scm.com/book/zh/v2/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git
     git --version
    

    03 | 使用Git之前需要做的最小配置

    • 配置 user 信息
      主要是为了在commit的时候能够添加上提交者的相关信息,方便在代码审查的时候知道提交人是谁,方便联系。
    git config --global user.name "zhangshaopo"
    git config --global user.email "ragpo_24@163.com"
    
    • 有三种级别的user信息,缺省等同于 local。
    git config --local     # local只对仓库有效
    git config --global   # global对登录⽤户所有仓库有效
    git config --system # system对系统的所有⽤户有效,一般不用
    
    • 显示 config 的配置,加 --list
    git config --list --local
    git config --list --global
    git config --list --system 
    
    • 清除,--unset
    git config --unset --local user.name
    git config --unset --global user.name
    git config --unset --system user.name 
    
    • 优先级
    local > global > system
    

    04 | 创建第一个仓库并配置local用户信息

    1
    • 创建一个库
    git init git-learning-1
    
    • 配置 user 信息
    cd git-learning-1
    git config --local user.email "ragpo_24@163.com"
    git config --local user.name "apo"
    
    • 创建一个README.md文件,内容如下:
    line1
    
    • 查看状态
    $ git status
    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        README.md
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    • 添加到占存区
    git add README.md
    
    • 查看状态
    $ git status
    On branch master
    
    No commits yet
    
    Changes to be committed:
      (use "git rm --cached <file>..." to unstage)
        new file:   README.md
    
    • commit
    $ git commit -m"add README.md"
    [master (root-commit) beab99b] add README.md
     1 file changed, 1 insertion(+)
     create mode 100644 README.md
    
    • 查看状态
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    • 查看日志

    可以看到Author是我们设置的名称和邮件

    $ git log
    commit beab99bb7d4887a05e322483e65db7e569af2bce (HEAD -> master)
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 17:51:13 2020 +0800
    
        add README.md
    
        add README.md
    

    05 | 通过几次commit来认识工作区和暂存区

    1
    • 添加两个文件a和b
    $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        a
        b
    
    $ git add a b
    
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   a
        new file:   b
    
    
    • 用 git restore --staged 将通过add添加到暂存区的a文件进行回退
    git restore --staged a
    
    • 查看状态
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   b
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        a
    
    • 将a和b文件进行commit提交
    $ git add a
    $ git commit -m"add a and b"
    [master 8bfc512] add a and b
     2 files changed, 2 insertions(+)
     create mode 100644 a
     create mode 100644 b
    
    • 修改a和b的内容,通过 git add -u 同时添加多个文件到暂存区
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   a
        modified:   b
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    $ git add -u
    
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        modified:   a
        modified:   b
    
    $ git commit -m"modify a and b"
    [master a7ec957] modify a and b
     2 files changed, 2 insertions(+), 2 deletions(-)
    
    $ git status
    On branch master
    nothing to commit, working tree clean
    

    06 | 给文件重命名的简便方法

    以下使用旧的方法,将a命名为aa

    $ mv a aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:26]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  3 21:37 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 b
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:27]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    a
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:29]
    $ git add aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:39]
    $ git add a
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:43]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        renamed:    a -> aa
    
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:38:47]
    $ git commit -m"rename a to aa"
    [master 19954dc] rename a to aa
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename a => aa (100%)
    

    以下使用简便方法重命名文件b文件为bb

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:39:00]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  3 21:37 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 b
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:44:06]
    $ git mv b bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:44:11]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        renamed:    b -> bb
    
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:44:13]
    $ git commit -m"rename b to bb"
    [master 76e6152] rename b to bb
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename b => bb (100%)
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:44:26]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  3 21:37 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 bb
    

    删除文件

    可以通过使用Linux的rm命令删除,然后再通过git add或者git rm更新到暂存区;也可以直接通过git rm删除文件并更新到暂存区。

    • 通过通过使用Linux的rm命令删除,然后再通过git add或者git rm更新到暂存区
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:58:07]
    $ rm -f aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:15]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:27] C:128
    $ git add aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:31] C:1
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        deleted:    aa
    
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:33]
    $ ll
    total 16
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:37]
    $ git restore --staged aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:48]
    $ ll
    total 16
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:50]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:58:54]
    $ git restore aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:59:05]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  3 21:59 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 bb
    
    
    • 直接通过git rm删除文件并更新到暂存区
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [21:59:07]
    $ git rm aa
    rm 'aa'
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:59:12]
    $ ll
    total 16
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 21:17 bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [21:59:14]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        deleted:    aa
    
    • 通过git reset --hard将工作区和暂存区的删除或者修改的内容回退到上一次commit的的状态,但是新增的文件不会。
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:04:43]
    $ git reset --hard
    HEAD is now at 76e6152 rename b to bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [22:05:49]
    $ vi aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:05:57]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:05:59]
    $ git rm bb
    rm 'bb'
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:06:13]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        deleted:    bb
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
    
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:06:17]
    $ ll
    total 16
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     4B Jun  3 22:05 aa
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [22:06:18]
    $ git reset --hard
    HEAD is now at 76e6152 rename b to bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [22:06:36]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  3 22:06 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  3 22:06 bb
    

    07 | 通过git log查看版本演变历史

    • 查看最近4个commit
    $ git log -n 4
    commit 76e615246f84c4f4d9f1e8142ab4b5899595780f (HEAD -> master)
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:44:26 2020 +0800
    
        rename b to bb
    
    commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:39:00 2020 +0800
    
        rename a to aa
    
    commit f2b8ff652c66b6cce31a95dfa6ded61273867523
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:34:42 2020 +0800
    
        a
    
    commit ab4396fd4c9558a2c822442b8303f94d5e4d3a87
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:31:47 2020 +0800
    
        rm a
    
    • 将commit信息单行输出
    $ git log -n4 --oneline
    76e6152 (HEAD -> master) rename b to bb
    19954dc rename a to aa
    f2b8ff6 a
    ab4396f rm a
    
    • 查看分支并创建分支
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [22:28:59]
    $ git branch -v
    * master 76e6152 rename b to bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [22:29:17]
    $ git checkout -b temp f2b8ff652c66b6cce31a95dfa6ded61273867523
    Switched to a new branch 'temp'
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:temp o [22:35:23]
    $ git branch -v
      master 76e6152 rename b to bb
    * temp   f2b8ff6 a
    
    • 默认使用git log查看的是当前分支的log,命令行图形化查看所有分支情况
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:temp o [22:40:44]
    $ git log --all --graph
    * commit bb4ef28c6e993c11c6265343e03ccb7f9c91785f (HEAD -> temp)
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 22:40:09 2020 +0800
    |
    |     add c
    |
    | * commit 76e615246f84c4f4d9f1e8142ab4b5899595780f (master)
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 21:44:26 2020 +0800
    | |
    | |     rename b to bb
    | |
    | * commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    |/  Author: zhangshaopo <ragpo_24@163.com>
    |   Date:   Wed Jun 3 21:39:00 2020 +0800
    |
    |       rename a to aa
    |
    * commit f2b8ff652c66b6cce31a95dfa6ded61273867523
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 21:34:42 2020 +0800
    |
    |     a
    |
    * commit ab4396fd4c9558a2c822442b8303f94d5e4d3a87
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 21:31:47 2020 +0800
    |
    |     rm a
    |
    * commit a7ec957015686e3de8665ee89cf786c6fcc18078
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 21:17:50 2020 +0800
    |
    |     modify a and b
    |
    * commit 8bfc512425422e0f85a3397a18faf84388522fe1
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 21:16:15 2020 +0800
    
    • 简洁输出
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:temp o [22:48:28] C:130
    $ git log --graph --all --oneline
    * bb4ef28 (HEAD -> temp) add c
    | * 76e6152 (master) rename b to bb
    | * 19954dc rename a to aa
    |/
    * f2b8ff6 a
    * ab4396f rm a
    * a7ec957 modify a and b
    * 8bfc512 add a and b
    * beab99b add README.md
    
    • 查看其他分支log,如指定master
    $ git log --graph  --oneline master
    
    • 查看log命令的web手册,我这里没有加载成功。
    git help --web log
    fatal: HTML documentation is not provided by this distribution of git.
    

    08 | gitk:通过图形界面工具来查看版本历史

    • 在命令行输入gitk,会弹出窗口


      1
    • 修改字体大小


      2
    • 修改展示页内容,我这里选择所有分支


      3
    • 就能看到所有的分支及关系


      4
    • patch是对比前后两次文件区别,在comment下选择本次commit变动的文件,会显示文件不同的地方,绿色是这次,红色是上次


      5
    • tree是显示本次commit所有的文件,包括变动和没有变动的


      6
    • 其他信息


      7

    09 | 探密.git目录

    • 查看HEAD

    HEAD的内容是指向 refs/heads/,指代当前在哪个分支下面

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:temp o [15:14:21]
    $ cd .git
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:14:23]
    $ ll
    total 56
    -rw-r--r--   1 zhangshaopo  staff     2B Jun  3 23:22 COMMIT_EDITMSG
    -rw-r--r--   1 zhangshaopo  staff    21B Jun  3 22:30 HEAD
    -rw-r--r--   1 zhangshaopo  staff    41B Jun  3 22:06 ORIG_HEAD
    -rw-r--r--   1 zhangshaopo  staff   194B Jun  3 22:45 config
    -rw-r--r--   1 zhangshaopo  staff    73B Jun  3 17:50 description
    -rw-r--r--   1 zhangshaopo  staff   789B Jun  3 23:22 gitk.cache
    drwxr-xr-x  14 zhangshaopo  staff   448B Jun  3 17:50 hooks
    -rw-r--r--   1 zhangshaopo  staff   329B Jun  3 23:22 index
    drwxr-xr-x   3 zhangshaopo  staff    96B Jun  3 17:50 info
    drwxr-xr-x   4 zhangshaopo  staff   128B Jun  3 17:51 logs
    drwxr-xr-x  30 zhangshaopo  staff   960B Jun  3 23:22 objects
    drwxr-xr-x   4 zhangshaopo  staff   128B Jun  3 17:50 refs
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:14:24]
    $ cat HEAD
    ref: refs/heads/temp
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:14:28]
    $ ll refs/heads
    total 16
    -rw-r--r--  1 zhangshaopo  staff    41B Jun  3 21:44 master
    -rw-r--r--  1 zhangshaopo  staff    41B Jun  3 23:22 temp
    
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:14:35]
    $ git branch -v
      master 76e6152 rename b to bb
    * temp   e5d61ab a
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:38:52]
    $ git checkout master
    fatal: this operation must be run in a work tree
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:temp o [15:39:01] C:128
    $ cd ..
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:temp o [15:39:04]
    $ git checkout master
    Switched to branch 'master'
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [15:39:12] C:1
    $ cd -
    ~/git-learning-1/.git
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:39:16]
    $ cat HEAD
    ref: refs/heads/master
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:39:20]
    $ git branch -v
    * master 76e6152 rename b to bb
      temp   e5d61ab a
    
    • config

    config文件里面的user字段可以通过命令行来设置,也可以通过命令来获取,如果修改了文件,命令行也是能读取到的。

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:54:52]
    $ git config --local --list
    core.repositoryformatversion=0
    core.filemode=true
    core.bare=false
    core.logallrefupdates=true
    core.ignorecase=true
    core.precomposeunicode=true
    gui.wmstate=normal
    gui.geometry=871x447+5+28 192 201
    user.name=apo
    user.email=xxx@163.com
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:54:47]
    $ cat config
    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
    [gui]
        wmstate = normal
        geometry = 871x447+5+28 192 201
    [user]
        name = apo
        email = xxx@163.com
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:55:03]
    $ git config --local user.name
    apo
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [15:57:12]
    $ git config --local user.email
    xxx@163.com
    
    • refs

    refs里面包含了本repo所包含的分支(head)和tag,例如master分支的内容,就是某次commit,git cat-file -t是查看这个哈希值所代表的类型,-p就展示这个哈希值的内容。

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:03]
    $ ll refs
    total 0
    drwxr-xr-x  4 zhangshaopo  staff   128B Jun  3 23:22 heads
    drwxr-xr-x  2 zhangshaopo  staff    64B Jun  3 17:50 tags
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:09]
    $ ll refs/heads
    total 16
    -rw-r--r--  1 zhangshaopo  staff    41B Jun  3 21:44 master
    -rw-r--r--  1 zhangshaopo  staff    41B Jun  3 23:22 temp
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:17]
    $ cat refs/heads/master
    76e615246f84c4f4d9f1e8142ab4b5899595780f
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:24]
    $ git cat-file -t 76e615246
    commit
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:35]
    $ git cat-file -p 76e615246
    tree cda2191fe0021fce14fca113c92b64551ec96649
    parent 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    author zhangshaopo <ragpo_24@163.com> 1591191866 +0800
    committer zhangshaopo <ragpo_24@163.com> 1591191866 +0800
    
    rename b to bb
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:05:41]
    $ git cat-file -p cda2191fe00
    100644 blob a29bdeb434d874c9b1d8969c40c42161b03fafdc    README.md
    100644 blob 78981922613b2afb6025042ff6bd878ac1994e85    aa
    100644 blob e0b3f1b09bd1819ed1f7ce2e75fc7400809f5350    bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:06:05]
    $ git cat-file -p a29bdeb
    line1
    
    • object

    object里面包含了对象,分为tree、commit、blob、tag,tree可以理解为一个文件夹,里面包含了blob,blob可以理解为是一个具体的文件,通过git cat-file -p可以看到内容,tag类似一个开发阶段成果,基于某次commit。

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:29:09]
    $ ll objects
    total 0
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:39 19
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:34 1c
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:39 21
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:20 31
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:20 48
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:22 57
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:17 5f
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 22:39 6a
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:44 76
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:06 78
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 17:51 81
    drwxr-xr-x  5 zhangshaopo  staff   160B Jun  3 21:31 8b
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 17:50 a2
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:17 a7
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:22 aa
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:31 ab
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 22:40 bb
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 17:51 be
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 22:40 c1
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:16 c8
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:44 cd
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:17 e0
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:22 e5
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:17 e6
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 21:34 f2
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  3 23:20 fe
    drwxr-xr-x  2 zhangshaopo  staff    64B Jun  3 17:50 info
    drwxr-xr-x  2 zhangshaopo  staff    64B Jun  3 17:50 pack
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:29:21]
    $ ll objects/fe
    total 8
    -r--r--r--  1 zhangshaopo  staff   130B Jun  3 23:20 046b7ede257c4825ef25e2845b3d08dbd9104c
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:29:31]
    $ git cat-file -t fe046b7ede257c4825ef25e2845b3d08dbd9104c
    tree
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:29:41]
    $ git cat-file -p fe046b7ede257c4825ef25e2845b3d08dbd9104c
    100644 blob a29bdeb434d874c9b1d8969c40c42161b03fafdc    README.md
    100644 blob 318861bb2b0af2638b377456f52e77c5ff646f2a    a
    100644 blob e0b3f1b09bd1819ed1f7ce2e75fc7400809f5350    b
    100644 blob 6a69f92020f5df77af6e8813ff1232493383b708    c
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:29:50]
    $ git cat-file -t a29bdeb434d874c9b1d8969c40c42161b03fafdc
    blob
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1/.git on git:master o [16:30:08]
    $ git cat-file -p a29bdeb434d874c9b1d8969c40c42161b03fafdc
    line1
    

    10 | commit、tree和blob三个对象之间的关系

    1

    一次commit就对应着对当前时间仓库内的所有文件做了一次快照;一次commit对应着一个tree,tree类似文件夹,一个tree下面又可以包含多个tree,tree下面可以包含文件,也就是blob,但是blob没有具体名字,因为git的存储机制是基于相同文件的内容就认为是同一个blob,在有多个文件时,就能节省实际的存储空间。

    11 | 小练习:数一数tree的个数

    1

    一次commit,至少包含本身一个tree,因为又包含一个目录doc,所以又会有个tree,doc下面包含一个文件,也是就blob,所以一共有2个tree,1个blob。

    12 | 分离头指针情况下的注意事项

    分离头指针说的是当前我们的工作不在具体的某个分支上进行,而是在一个没有分支的状态下工作,这种状态是同样可以commit,一般的用处就是仅仅做个测试验证,可以随时不要,只需要执行git checkout某个分支就可以丢弃了,但是git会提示你,是否需要将在分离头指针情况下的commit生成一个分支。分离头指针一般是基于某个commit生成的。

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:23:27]
    $ git checkout a7ec957015686e3de8
    Note: switching to 'a7ec957015686e3de8'.
    
    You are in 'detached HEAD' state. You can look around, make experimental
    changes and commit them, and you can discard any commits you make in this
    state without impacting any branches by switching back to a branch.
    
    If you want to create a new branch to retain commits you create, you may
    do so (now or later) by using -c with the switch command. Example:
    
      git switch -c <new-branch-name>
    
    Or undo this operation with:
    
      git switch -
    
    Turn off this advice by setting config variable advice.detachedHead to false
    
    HEAD is now at a7ec957 modify a and b
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 o [19:23:32]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  4 19:23 a
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  4 19:23 b
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 o [19:23:46]
    $ touch detached-file
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 x [19:23:59]
    $ vi detached-file
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 x [19:24:12]
    $ git commit -am"detached"
    HEAD detached at a7ec957
    Untracked files:
        detached-file
    
    nothing added to commit but untracked files present
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 x [19:24:22] C:1
    $ git add detached-file
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:a7ec957 x [19:24:33]
    $ git commit -am"detached"
    [detached HEAD 1375d76] detached
     1 file changed, 1 insertion(+)
     create mode 100644 detached-file
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:1375d76 o [19:24:34]
    $ git status
    HEAD detached from a7ec957
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:1375d76 o [19:24:39]
    $ git log
    commit 1375d760e4b4b7173f7b2ffd9ac90e1b33ec431e (HEAD)
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 19:24:34 2020 +0800
    
        detached
    
    commit a7ec957015686e3de8665ee89cf786c6fcc18078
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:17:50 2020 +0800
    
        modify a and b
    
    commit 8bfc512425422e0f85a3397a18faf84388522fe1
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:16:15 2020 +0800
    
        add a and b
    
    $ git checkout master
    Warning: you are leaving 1 commit behind, not connected to
    any of your branches:
    
      1375d76 detached
    
    If you want to keep it by creating a new branch, this may be a good time
    to do so with:
    
     git branch <new-branch-name> 1375d76
    
    Switched to branch 'master'
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:29:28]
    $ git log --graph  --all
    * commit e5d61ab172f1cc308cddfd01b496d6e8a73e7170 (temp)
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 23:22:41 2020 +0800
    |
    |     a
    |
    * commit 480b474878cb9dac78aa4fa00bae6ef0be264701
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 23:20:31 2020 +0800
    |
    |     modify a
    |
    * commit bb4ef28c6e993c11c6265343e03ccb7f9c91785f
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 22:40:09 2020 +0800
    |
    |     add c
    |
    | * commit 76e615246f84c4f4d9f1e8142ab4b5899595780f (HEAD -> master)
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 21:44:26 2020 +0800
    | |
    | |     rename b to bb
    | |
    | * commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    |/  Author: zhangshaopo <ragpo_24@163.com>
    |   Date:   Wed Jun 3 21:39:00 2020 +0800
    |
    |       rename a to aa
    |
    * commit f2b8ff652c66b6cce31a95dfa6ded61273867523
    | Author: zhangshaopo <ragpo_24@163.com>
    | Date:   Wed Jun 3 21:34:42 2020 +0800
    |
    |     a
    |
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:29:41]
    $ git branch detached 1375d76
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:30:43]
    $ git log --graph  --all
    * commit 1375d760e4b4b7173f7b2ffd9ac90e1b33ec431e (detached)
    | Author: apo <xxx@163.com>
    | Date:   Thu Jun 4 19:24:34 2020 +0800
    |
    |     detached
    |
    | * commit e5d61ab172f1cc308cddfd01b496d6e8a73e7170 (temp)
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 23:22:41 2020 +0800
    | |
    | |     a
    | |
    | * commit 480b474878cb9dac78aa4fa00bae6ef0be264701
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 23:20:31 2020 +0800
    | |
    | |     modify a
    | |
    | * commit bb4ef28c6e993c11c6265343e03ccb7f9c91785f
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 22:40:09 2020 +0800
    | |
    | |     add c
    | |
    | | * commit 76e615246f84c4f4d9f1e8142ab4b5899595780f (HEAD -> master)
    | | | Author: zhangshaopo <ragpo_24@163.com>
    | | | Date:   Wed Jun 3 21:44:26 2020 +0800
    | | |
    | | |     rename b to bb
    | | |
    | | * commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    | |/  Author: zhangshaopo <ragpo_24@163.com>
    | |   Date:   Wed Jun 3 21:39:00 2020 +0800
    | |
    | |       rename a to aa
    | |
    | * commit f2b8ff652c66b6cce31a95dfa6ded61273867523
    | | Author: zhangshaopo <ragpo_24@163.com>
    | | Date:   Wed Jun 3 21:34:42 2020 +0800
    

    13 | 进一步理解HEAD和branch

    head可以指向某个branch或者某个tag,其实具体的就是某个commit,因为一个branch的状态会随着commit的提交不断发生变化,所以head可以直接指代某个commit。比如通过git diff来比较不同commit之间的差异时,可以使用commit id,也可以使用head,如果使用head的话,默认是head指代的就是当前分支的最新commit。

    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:47:29]
    $ vi cc
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [19:47:40]
    $ git add cc
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master x [19:47:43]
    $ git commit -m"add cc"
    [master 7d69805] add cc
     1 file changed, 1 insertion(+)
     create mode 100644 cc
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:47:49]
    $ git log
    commit 7d69805ff3340df16389385d86f026c687d9d3cb (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 19:47:49 2020 +0800
    
        add cc
    
    commit 76e615246f84c4f4d9f1e8142ab4b5899595780f
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:44:26 2020 +0800
    
        rename b to bb
    
    # zhangshaopo @ zhangshopodeMBP in ~/git-learning-1 on git:master o [19:48:01]
    $ git diff 7d69805ff 76e61524
    diff --git a/cc b/cc
    deleted file mode 100644
    index 28924d0..0000000
    --- a/cc
    +++ /dev/null
    @@ -1 +0,0 @@
    -cccc
    

    使用HEAD来指代当前分支commit,HEAD1或者HEAD~1指代上一个commit,HEAD^或者HEAD~2指代上上一个commit.

    14 | 怎么删除不需要的分支?

    使用git branch -d或者git branch -D进行删除分支,-D是强制确认删除。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:10:16]
    $ git branch -av
      detached 1375d76 detached
    * master   7d69805 add cc
      temp     e5d61ab a
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:15:19] C:1
    $ git branch -d detached
    error: The branch 'detached' is not fully merged.
    If you are sure you want to delete it, run 'git branch -D detached'.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:15:31] C:1
    $ git branch -D detached
    Deleted branch detached (was 1375d76).
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:15:38]
    $ git branch -av
      * master 7d69805 add cc
      temp   e5d61ab a
    

    15 | 怎么修改最新commit的message?

    通过git commit --amend命令修改当前的commit的message信息。

    16 | 怎么修改老旧commit的message?

    通过使用git rebase -i 指定commit的父commit id,进入一个文件交互框,然后将pick修改为reword/r,然后保存退出,会再进行一个文件交互框,这个时候修改commit信息,再保存退出。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:47:28]
    $ git --no-pager log -n3
    commit 8c45faa517d496d409a37e8a1f004cfe7bb7d333 (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 19:47:49 2020 +0800
    
        zhangshaopo add cc
    
    commit c7f6685ffbcc93bd33a8ac0e5e8a8948a5219128
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:44:26 2020 +0800
    
        rename b to bb
    
    commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:39:00 2020 +0800
    
        rename a to aa
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:47:29]
    $ git rebase -i 19954dc4c1ab9ec
    pick c7f6685 rename b to bb
    pick 8c45faa zhangshaopo add cc
    
    # Rebase 19954dc..8c45faa onto 19954dc (2 commands)
    #
    # Commands:
    # p, pick <commit> = use commit
    # r, reword <commit> = use commit, but edit the commit message
    # e, edit <commit> = use commit, but stop for amending
    # s, squash <commit> = use commit, but meld into previous commit
    # f, fixup <commit> = like "squash", but discard this commit's log message
    # x, exec <command> = run command (the rest of the line) using shell
    # b, break = stop here (continue rebase later with 'git rebase --continue')
    # d, drop <commit> = remove commit
    # l, label <label> = label current HEAD with a name
    # t, reset <label> = reset HEAD to a label
    # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
    # .       create a merge commit using the original merge commit's
    # .       message (or the oneline, if no original merge commit was
    # .       specified). Use -c <commit> to reword the commit message.
    #
    # These lines can be re-ordered; they are executed from top to bottom.
    #
    # If you remove a line here THAT COMMIT WILL BE LOST.
    #
    # However, if you remove everything, the rebase will be aborted.
    #
    # Note that empty commits are commented out
    ------>>>
    zhangshaopo rename b to bb
    
    # Please enter the commit message for your changes. Lines starting
    # with '#' will be ignored, and an empty message aborts the commit.
    #
    # Author:    zhangshaopo <ragpo_24@163.com>
    # Date:      Wed Jun 3 21:44:26 2020 +0800
    #
    # interactive rebase in progress; onto 19954dc
    # Last command done (1 command done):
    #    reword c7f6685 rename b to bb
    # Next command to do (1 remaining command):
    #    pick 8c45faa zhangshaopo add cc
    # You are currently editing a commit while rebasing branch 'master' on '19954dc'.
    #
    # Changes to be committed:
    #       renamed:    b -> bb
    #
    
    $ git rebase -i 19954dc4c1ab9ec
    [detached HEAD 0c10858] zhangshaopo rename b to bb
     Author: zhangshaopo <ragpo_24@163.com>
     Date: Wed Jun 3 21:44:26 2020 +0800
     1 file changed, 0 insertions(+), 0 deletions(-)
     rename b => bb (100%)
    Successfully rebased and updated refs/heads/master.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [20:51:35]
    $ git --no-pager log -n3
    commit 44eefccfa4f54895a9cc2a96a054a445dbb77912 (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 19:47:49 2020 +0800
    
        zhangshaopo add cc
    
    commit 0c108589da2e105019ede7efa13a101b1504f80d
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:44:26 2020 +0800
    
        zhangshaopo rename b to bb
    
    commit 19954dc4c1ab9ec9c49c969a5f19476abd7e34c7
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:39:00 2020 +0800
    
        rename a to aa
    

    17 | 怎样把连续的多个commit整理成1个?

    $ git --no-pager log --oneline
    44eefcc (HEAD -> master) zhangshaopo add cc
    0c10858 zhangshaopo rename b to bb
    19954dc rename a to aa
    f2b8ff6 a
    ab4396f rm a
    a7ec957 modify a and b
    8bfc512 add a and b
    beab99b add README.md
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [21:04:09]
    $ git rebase -i beab99bb7d
    [detached HEAD a1cc254] add a and b
     Author: zhangshaopo <ragpo_24@163.com>
     Date: Wed Jun 3 21:16:15 2020 +0800
     3 files changed, 3 insertions(+)
     create mode 100644 aa
     create mode 100644 bb
     create mode 100644 cc
    Successfully rebased and updated refs/heads/master.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [21:06:42]
    $ git --no-pager log
    commit a1cc25402f6fd31ae654baa76e5b91ce3169fc08 (HEAD -> master)
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:16:15 2020 +0800
    
        add a and b
    
        modify a and b
    
        rm a
    
        a
    
        rename a to aa
    
        zhangshaopo rename b to bb
    
        zhangshaopo add cc
    
    commit beab99bb7d4887a05e322483e65db7e569af2bce
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 17:51:13 2020 +0800
    
        add README.md
    

    18 | 怎样把间隔的几个commit整理成1个?

    不想写了,有点报错。

    19 | 怎么比较暂存区和HEAD所含文件的差异?

    在修改完文件,add后添加到暂存区,想比较暂存区与HEAD区别,可以通过git diff --cached进行比较

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [22:09:38]
    $ vi cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [22:10:06]
    $ git add cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [22:10:08]
    $ git --no-pager diff --cached
    diff --git a/cc b/cc
    index 566c958..654a75a 100644
    --- a/cc
    +++ b/cc
    @@ -1 +1 @@
    -c-22
    +cx-22
    

    20 | 怎么比较工作区和暂存区所含文件的差异?

    git --no-pager diff,另外可以通过在后面加上文件名指定显示,否则默认是展示所有文件的不同点

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [22:21:24]
    $ git --no-pager diff
    diff --git a/aa b/aa
    index 7898192..717f8ed 100644
    --- a/aa
    +++ b/aa
    @@ -1 +1 @@
    -a
    +ninina
    diff --git a/cc b/cc
    index 566c958..6129777 100644
    --- a/cc
    +++ b/cc
    @@ -1 +1 @@
    -c-22
    +xxxc-22
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [22:21:26]
    $ git --no-pager diff aa
    diff --git a/aa b/aa
    index 7898192..717f8ed 100644
    --- a/aa
    +++ b/aa
    @@ -1 +1 @@
    -a
    +ninina
    

    21 | 如何让暂存区恢复成和HEAD的一样?

    可以通过git restore --staged指定文件恢复,或者git reset HEAD

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [8:29:05]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
        modified:   cc
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [8:53:49]
    $ git add aa cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [8:58:57]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        modified:   aa
        modified:   cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:12:01]
    $ git --no-pager diff --cached
    diff --git a/aa b/aa
    index 7898192..717f8ed 100644
    --- a/aa
    +++ b/aa
    @@ -1 +1 @@
    -a
    +ninina
    diff --git a/cc b/cc
    index 566c958..6129777 100644
    --- a/cc
    +++ b/cc
    @@ -1 +1 @@
    -c-22
    +xxxc-22
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:12:12]
    $ git restore --staged
    fatal: you must specify path(s) to restore
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:13:50] C:128
    $ git restore --staged aa cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:14:00]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
        modified:   cc
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:14:05]
    $ git add aa cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:14:54]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        modified:   aa
        modified:   cc
    
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:14:56]
    $ git reset HEAD
    Unstaged changes after reset:
    M   aa
    M   cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:15:05]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
        modified:   cc
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    22 | 如何让工作区的文件恢复为和暂存区一样?

    git restore 文件名

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:45:52]
    $ cat cc
    xxxc-22
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:46:04]
    $ git --no-pager diff cc
    diff --git a/cc b/cc
    index 566c958..6129777 100644
    --- a/cc
    +++ b/cc
    @@ -1 +1 @@
    -c-22
    +xxxc-22
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:46:12]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   cc
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [10:46:25]
    $ git restore cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [10:46:31]
    $ cat cc
    c-22
    

    23 | 怎样取消暂存区部分文件的更改?

    git restore --staged 文件名

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:14:07] C:1
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:14:10]
    $ git add aa
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:14:28]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        modified:   aa
    
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:14:30]
    $ git restore --staged aa
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:14:42]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   aa
    
    no changes added to commit (use "git add" and/or "git commit -a")
    

    24 | 消除最近的几次提交

    恢复到某次commit,git reset --hard 51e9dc395,工作区和暂存区都会变成对应commit文件状态

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:31:21]
    $ git log
    commit e15c04bfbd79796b56b241bf47e1e9daf2101071 (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Fri Jun 5 11:07:19 2020 +0800
    
        jj2
    
    commit f12bd4b520023986aa17ec4a397c74c03ea32399
    Author: apo <xxx@163.com>
    Date:   Fri Jun 5 11:06:54 2020 +0800
    
        jj
    
    commit 51e9dc3955215120a4094e18d6c691e76c6324de
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 22:00:09 2020 +0800
    
        combine add and 2
    
    commit 38457f22dec999493e4bab764eedb785140c450d
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:16:15 2020 +0800
    
        zhangshaopo add file
    
    commit beab99bb7d4887a05e322483e65db7e569af2bce
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 17:51:13 2020 +0800
    
        add README.md
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:31:23]
    $ git reset --help
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:31:59]
    $ git reset --hard 51e9dc395
    HEAD is now at 51e9dc3 combine add and 2
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:32:09]
    $ git log
    commit 51e9dc3955215120a4094e18d6c691e76c6324de (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 22:00:09 2020 +0800
    
        combine add and 2
    
    commit 38457f22dec999493e4bab764eedb785140c450d
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:16:15 2020 +0800
    
        zhangshaopo add file
    
    commit beab99bb7d4887a05e322483e65db7e569af2bce
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 17:51:13 2020 +0800
    
        add README.md
    

    25 | 看看不同提交的指定文件的差异

    通过git diff指定分支或者commit,分支其实也是一个具体的commit,还可以加上比对的文件,否则是比对所有文件。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:40:10]
    $ git diff beab99b 51e9dc395
    diff --git a/aa b/aa
    new file mode 100644
    index 0000000..7898192
    --- /dev/null
    +++ b/aa
    @@ -0,0 +1 @@
    +a
    diff --git a/bb b/bb
    new file mode 100644
    index 0000000..e0b3f1b
    --- /dev/null
    +++ b/bb
    @@ -0,0 +1 @@
    +bb
    diff --git a/cc b/cc
    new file mode 100644
    index 0000000..566c958
    --- /dev/null
    +++ b/cc
    @@ -0,0 +1 @@
    +c-22
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:40:20]
    $ git diff beab99b 51e9dc395 aa
    diff --git a/aa b/aa
    new file mode 100644
    index 0000000..7898192
    --- /dev/null
    +++ b/aa
    @@ -0,0 +1 @@
    +a
    

    26 | 正确删除文件的方法

    可以通过使用Linux的rm命令删除,然后再通过git add或者git rm更新到暂存区;也可以直接通过git rm删除文件并更新到暂存区。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:54:48]
    $ ll
    total 32
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 11:32 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  4 21:22 bb
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 11:32 cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:54:50]
    $ rm -f cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:54:55]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add/rm <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        deleted:    cc
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:54:57]
    $ git add cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:55:01]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        deleted:    cc
    
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:55:04]
    $ git commit -m"delete cc"
    [master 4045e7a] delete cc
     1 file changed, 1 deletion(-)
     delete mode 100644 cc
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:55:18]
    $ ll
    total 24
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 11:32 aa
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  4 21:22 bb
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:55:21]
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:55:25]
    $ git log
    commit 4045e7a1d96b9fb050a49ed06203ee343488c5b5 (HEAD -> master)
    Author: apo <xxx@163.com>
    Date:   Fri Jun 5 11:55:18 2020 +0800
    
        delete cc
    
    commit 51e9dc3955215120a4094e18d6c691e76c6324de
    Author: apo <xxx@163.com>
    Date:   Thu Jun 4 22:00:09 2020 +0800
    
        combine add and 2
    
    commit 38457f22dec999493e4bab764eedb785140c450d
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 21:16:15 2020 +0800
    
        zhangshaopo add file
    
    commit beab99bb7d4887a05e322483e65db7e569af2bce
    Author: zhangshaopo <ragpo_24@163.com>
    Date:   Wed Jun 3 17:51:13 2020 +0800
    
        add README.md
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:55:26]
    $ git rm aa
    rm 'aa'
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:56:02]
    $ git status
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        deleted:    aa
    
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [11:56:04]
    $ git commit -m"delete aa"
    [master 48ee187] delete aa
     1 file changed, 1 deletion(-)
     delete mode 100644 aa
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:56:12]
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:56:15]
    $ ll
    total 16
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  4 21:22 bb
    

    27 | 开发中临时加塞了紧急任务怎么处理?

    通过stash能够将工作目录中和暂存区的内容暂时保存;在添加了a和c文件,并且修改了bb文件,stash后不会保存新增的a和c,需要通过add a和c,再stash才会将其进行保存;恢复的时候,pop会删除 stash历史记录,apply不会

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:19:17]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   bb
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        a
        c
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:19:19]
    $ git stash
    Saved working directory and index state WIP on master: 48ee187 delete aa
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:19:35]
    $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        a
        c
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:19:38]
    $ git commit -am"a and c"
    On branch master
    Untracked files:
        a
        c
    
    nothing added to commit but untracked files present
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:20:03] C:1
    $ git commit -a -m"a and c"
    On branch master
    Untracked files:
        a
        c
    
    nothing added to commit but untracked files present
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:20:14] C:1
    $ git add a c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:20:28]
    $ git commit -m"add a and c"
    [master 2aea138] add a and c
     2 files changed, 2 insertions(+)
     create mode 100644 a
     create mode 100644 c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:20:39]
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:20:41]
    $ ll
    total 32
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:19 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:20:43]
    $ git stash pop
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   bb
    
    no changes added to commit (use "git add" and/or "git commit -a")
    Dropped refs/stash@{0} (c14399230601f0f07efc2a7e946cfa387d0ca427)
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:20:49]
    $ ll
    total 32
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 13:20 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:28:16]
    $ vi d
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:28:32]
    $ git status
    On branch master
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   bb
    
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        d
    
    no changes added to commit (use "git add" and/or "git commit -a")
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:28:39]
    $ git add d
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:30:55]
    $ git stash
    Saved working directory and index state WIP on master: 2aea138 add a and c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:31:09]
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:31:14]
    $ ll
    total 32
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:31 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:31:37] C:129
    $ git stash list
    stash@{0}: WIP on master: 2aea138 add a and c
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [13:31:39]
    $ git stash apply
    On branch master
    Changes to be committed:
      (use "git restore --staged <file>..." to unstage)
        new file:   d
    
    Changes not staged for commit:
      (use "git add <file>..." to update what will be committed)
      (use "git restore <file>..." to discard changes in working directory)
        modified:   bb
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [13:31:45]
    $ git stash list
    stash@{0}: WIP on master: 2aea138 add a and c
    

    28 | 如何指定不需要Git管理的文件?

    通过创建.gitignore,没有 / 则能匹配所有文件和目录,以 / 结尾则匹配目录,*则代表匹配所有。
    https://github.com/github/gitignore

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:57:48]
    $ ll
    total 48
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 13:31 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 14:43 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 14:43 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 14:45 f
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:57:50]
    $ cat .gitignore
    d
    f
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:58:03]
    $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitignore
        e
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:58:09]
    $ vi .gitignore
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:58:38]
    $ cat .gitignore
    d/
    f
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master x [14:58:43]
    $ git status
    On branch master
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        .gitignore
        d
        e
    
    nothing added to commit but untracked files present (use "git add" to track)
    

    29 | 如何将Git仓库备份到本地?

    1
    2
    3
    • 本地哑协议(下载)

    --bare参数指定不需要clone代码文件,只需要.git文件内容。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:13:05]
    $ git clone --bare /Users/zhangshaopo/git-learning-1/.git ya
    Cloning into bare repository 'ya'...
    done.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:13:12]
    $ ll ya
    total 32
    -rw-r--r--    1 zhangshaopo  staff    23B Jun  5 17:13 HEAD
    drwxr-xr-x    2 zhangshaopo  staff    64B Jun  5 17:13 branches
    -rw-r--r--    1 zhangshaopo  staff   175B Jun  5 17:13 config
    -rw-r--r--    1 zhangshaopo  staff    73B Jun  5 17:13 description
    drwxr-xr-x   13 zhangshaopo  staff   416B Jun  5 17:13 hooks
    drwxr-xr-x    3 zhangshaopo  staff    96B Jun  5 17:13 info
    drwxr-xr-x  105 zhangshaopo  staff   3.3K Jun  5 17:13 objects
    -rw-r--r--    1 zhangshaopo  staff   162B Jun  5 17:13 packed-refs
    drwxr-xr-x    4 zhangshaopo  staff   128B Jun  5 17:13 refs
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:13:15]
    $ git branch -av
    fatal: not a git repository (or any of the parent directories): .git
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:13:23] C:128
    $ ll
    total 0
    drwxr-xr-x  11 zhangshaopo  staff   352B Jun  5 17:13 ya
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:14:52]
    $ cd ya
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/ya on git:master o [17:14:56]
    $ git branch -av
    * master 5dd4342 null-1
      temp   e5d61ab a
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/ya on git:master o [17:15:01]
    $ cd ..
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:15:07]
    $ ll
    total 0
    drwxr-xr-x  11 zhangshaopo  staff   352B Jun  5 17:13 ya
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:15:08]
    $ rm -fr ya
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:15:11]
    $ git clone  /Users/zhangshaopo/git-learning-1/.git ya
    Cloning into 'ya'...
    done.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:15:21]
    $ ll ya
    total 48
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 17:15 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 17:15 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 17:15 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 17:15 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 17:15 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 17:15 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 17:15 f
    
    • 智能协议(下载)
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:26:35]
    $ git clone  file:///Users/zhangshaopo/git-learning-1/ zhineng
    Cloning into 'zhineng'...
    remote: Enumerating objects: 48, done.
    remote: Counting objects: 100% (48/48), done.
    remote: Compressing objects: 100% (29/29), done.
    remote: Total 48 (delta 7), reused 0 (delta 0)
    Receiving objects: 100% (48/48), done.
    Resolving deltas: 100% (7/7), done.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo [17:27:08]
    $ ll zhineng
    total 48
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 17:27 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 17:27 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 17:27 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 17:27 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 17:27 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 17:27 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 17:27 f
    
    • push

    在push之后,进入repo,通过git相关命令就能看见新建的分支以及文件了。

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [17:37:11]
    $ git remote add zhineng file:///Users/zhangshaopo/local-repo/zhineng
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [17:37:46]
    $ git branch -av
    * master 5dd4342 null-1
      temp   e5d61ab a
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [17:38:26]
    $ git checkout -b test1
    Switched to a new branch 'test1'
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:02:55]
    $ ll
    total 48
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 13:31 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 14:43 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 14:43 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 14:45 f
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:02:59]
    $ git branch -av
      master 5dd4342 null-1
      temp   e5d61ab a
    * test1  5dd4342 null-1
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:03:06]
    $ vi g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 x [19:03:18]
    $ git status
    On branch test1
    Untracked files:
      (use "git add <file>..." to include in what will be committed)
        g
    
    nothing added to commit but untracked files present (use "git add" to track)
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 x [19:03:29]
    $ git add g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 x [19:03:41]
    $ git commit -m"add g"
    [test1 264eeb6] add g
     1 file changed, 1 insertion(+)
     create mode 100644 g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:03:53]
    $ git push zhineng
    fatal: The current branch test1 has no upstream branch.
    To push the current branch and set the remote as upstream, use
    
        git push --set-upstream zhineng test1
    
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:04:05] C:128
    $ git push --set-upstream zhineng test1
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (3/3), 249 bytes | 249.00 KiB/s, done.
    Total 3 (delta 1), reused 0 (delta 0)
    To file:///Users/zhangshaopo/local-repo/zhineng
     * [new branch]      test1 -> test1
    Branch 'test1' set up to track remote branch 'test1' from 'zhineng'.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:04:12]
    $ git branch -av
      master                5dd4342 null-1
      temp                  e5d61ab a
    * test1                 264eeb6 add g
      remotes/zhineng/test1 264eeb6 add g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [19:06:35]
    $ git status
    On branch test1
    Your branch is up to date with 'zhineng/test1'.
    
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/zhineng on git:test1 o [19:16:39]
    $ pwd
    /Users/zhangshaopo/local-repo/zhineng
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/zhineng on git:test1 o [19:15:05]
    $ git checkout test1
    Already on 'test1'
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/zhineng on git:test1 o [19:16:32]
    $ ll
    total 56
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 17:27 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 19:11 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 19:11 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 19:11 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 19:11 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 19:11 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 19:11 f
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 19:14 g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/local-repo/zhineng on git:test1 o [19:16:33]
    $ git branch -av
      master                5dd4342 null-1
    * test1                 264eeb6 add g
      remotes/origin/HEAD   -> origin/master
      remotes/origin/master 5dd4342 null-1
      remotes/origin/temp   e5d61ab a
    

    30 | 注册一个GitHub账号

    31 | 配置公私钥

    在通过ssh进行仓库连接,需要使用密钥对,就可以不需要http/https那样需要用户名和密码了。

    https://help.github.com/en/github/authenticating-to-github/connecting-to-github-with-ssh

    32 | 在GitHub上创建个人仓库

    1

    33 | 把本地仓库同步到GitHub

    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:27:48]
    $ git remote add github git@github.com:ragpo/git-learning.git
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:35:54] C:129
    $ git remote -v
    github  git@github.com:ragpo/git-learning.git (fetch)
    github  git@github.com:ragpo/git-learning.git (push)
    zhineng file:///Users/zhangshaopo/local-repo/zhineng (fetch)
    zhineng file:///Users/zhangshaopo/local-repo/zhineng (push)
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:35:56]
    $ git branch -av
      master                5dd4342 null-1
      temp                  e5d61ab a
    * test1                 264eeb6 add g
      remotes/zhineng/test1 264eeb6 add g
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:38:47] C:130
    $ git push github
    Enumerating objects: 32, done.
    Counting objects: 100% (32/32), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (17/17), done.
    Writing objects: 100% (32/32), 2.09 KiB | 713.00 KiB/s, done.
    Total 32 (delta 5), reused 0 (delta 0)
    remote: Resolving deltas: 100% (5/5), done.
    remote:
    remote: Create a pull request for 'test1' on GitHub by visiting:
    remote:      https://github.com/ragpo/git-learning/pull/new/test1
    remote:
    To github.com:ragpo/git-learning.git
     * [new branch]      test1 -> test1
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:38:59]
    $ git push github --all
    Enumerating objects: 22, done.
    Counting objects: 100% (22/22), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (14/14), done.
    Writing objects: 100% (21/21), 1.72 KiB | 883.00 KiB/s, done.
    Total 21 (delta 2), reused 0 (delta 0)
    remote: Resolving deltas: 100% (2/2), done.
    remote:
    remote: Create a pull request for 'temp' on GitHub by visiting:
    remote:      https://github.com/ragpo/git-learning/pull/new/temp
    remote:
    To github.com:ragpo/git-learning.git
     * [new branch]      temp -> temp
     ! [rejected]        master -> master (fetch first)
    error: failed to push some refs to 'git@github.com:ragpo/git-learning.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 integrate the remote changes
    hint: (e.g., 'git pull ...') before pushing again.
    hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:test1 o [11:59:04]
    $ git checkout master
    Switched to branch 'master'
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:59:13]
    $ git status
    On branch master
    nothing to commit, working tree clean
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [11:59:20]
    $ git merge github/master
    fatal: refusing to merge unrelated histories
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [12:06:21] C:129
    $ git merge github/master --allow-unrelated-histories
    Merge made by the 'recursive' strategy.
     LICENSE | 201 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     1 file changed, 201 insertions(+)
     create mode 100644 LICENSE
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [12:06:29]
    $ ll
    total 72
    -rw-r--r--  1 zhangshaopo  staff    11K Jun  6 12:06 LICENSE
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  3 17:50 README.md
    -rw-r--r--  1 zhangshaopo  staff     2B Jun  5 13:17 a
    -rw-r--r--  1 zhangshaopo  staff     9B Jun  5 13:31 bb
    -rw-r--r--  1 zhangshaopo  staff     3B Jun  5 13:17 c
    -rw-r--r--  1 zhangshaopo  staff     6B Jun  5 14:43 d
    -rw-r--r--  1 zhangshaopo  staff     5B Jun  5 14:43 e
    drwxr-xr-x  3 zhangshaopo  staff    96B Jun  5 14:45 f
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [12:17:26]
    $ git log --graph
    *   commit 49f2b29637df38aa5ef027cf4222c538216b0d80 (HEAD -> master)
    |\  Merge: 5dd4342 8a87c0f
    | | Author: apo <xxx@163.com>
    | | Date:   Sat Jun 6 12:06:26 2020 +0800
    | |
    | |     Merge remote-tracking branch 'github/master'
    | |
    | * commit 8a87c0f9df7af760d2d4e4a090947d6fa77e86d9 (github/master)
    |   Author: ragpo <31686068+ragpo@users.noreply.github.com>
    |   Date:   Sat Jun 6 00:25:01 2020 +0800
    |
    |       Initial commit
    |
    * commit 5dd43425e001faaf107c9d6408539e642089708e
    | Author: apo <xxx@163.com>
    | Date:   Fri Jun 5 17:12:47 2020 +0800
    |
    |     null-1
    |
    
    # zhangshaopo @ zhangshaopodeMacBook-Pro in ~/git-learning-1 on git:master o [12:17:34]
    $ git push github --all
    Enumerating objects: 4, done.
    Counting objects: 100% (4/4), done.
    Delta compression using up to 8 threads
    Compressing objects: 100% (2/2), done.
    Writing objects: 100% (2/2), 297 bytes | 297.00 KiB/s, done.
    Total 2 (delta 1), reused 0 (delta 0)
    remote: Resolving deltas: 100% (1/1), completed with 1 local object.
    To github.com:ragpo/git-learning.git
       8a87c0f..49f2b29  master -> master
    
    $ git log --graph
    *   commit 49f2b29637df38aa5ef027cf4222c538216b0d80 (HEAD -> master, github/master)
    |\  Merge: 5dd4342 8a87c0f
    | | Author: apo <xxx@163.com>
    | | Date:   Sat Jun 6 12:06:26 2020 +0800
    | |
    | |     Merge remote-tracking branch 'github/master'
    | |
    | * commit 8a87c0f9df7af760d2d4e4a090947d6fa77e86d9
    |   Author: ragpo <31686068+ragpo@users.noreply.github.com>
    |   Date:   Sat Jun 6 00:25:01 2020 +0800
    |
    |       Initial commit
    |
    * commit 5dd43425e001faaf107c9d6408539e642089708e
    | Author: apo <xxx@163.com>
    | Date:   Fri Jun 5 17:12:47 2020 +0800
    |
    |     null-1
    
    1
    2

    34 | 不同人修改了不同文件如何处理?

    我们在别人修改并且push之前,已经pull/fetch到本地后,别人在修改了一个文件A,push到GitHub,此时我们在本地修改文件B,在push的时候会报错,那么就需要通过使用git fetch,然后再git merge,最后git push。git pull其实等于git fetch和git merge两个操作,完成本地与远端的同步,如果相同文件

    35 | 不同人修改了同文件的不同区域如何处理?

    这个其实和上个没有太大区别,因为修改的地方不一样,所以会在merge的时候自动合并。

    36 | 不同人修改了同文件的同一区域如何处理?

    首先还是得先pull在GitHub上最新的代码,然后会提示merge失败的地方,修改对应的文件,然后再add commit,然后push。

    37 | 同时变更了文件名和文件内容如何处理?

    在别人修改了文件名以及内容,并且push到GitHub后,我们在本地修改了原始文件,push的时候会报错,通过使用git pull就能将文件名同步并且merge文件内容。

    38 | 把同一文件改成了不同的文件名如何处理?

    同理,先git pull下来,然后会提示文件冲突,本地会出现别人修改后的文件名文件,再通过git status根据提示add和rm文件,最后push。

    39 | 禁止向集成分支执行push -f操作

    悠着点用。GitHub和gitlab提供禁止push -f的操作配置。

    40 | 禁止向集成分支执行变更历史的操作

    改了之后其他人会很麻烦,

    41 | GitHub为什么会火?

    1
    2
    3
    4
    5

    42 | GitHub都有哪些核心功能?

    https://github.com/
    https://github.com/features

    43 | 怎么快速淘到感兴趣的开源项目?

    可以通过输入关键字匹配。
    https://github.com/xirong/my-git

    1

    44 | 怎样在GitHub上搭建个人博客

    https://github.com/barryclark/jekyll-now

    45 | 开源项目怎么保证代码质量?

    46 | 为何需要组织类型的仓库?

    47 | 创建团队的项目

    48 | 怎样选择适合自己团队的工作流?

    1
    2
    3
    4
    5
    6
    7

    49 | 如何挑选合适的分支集成策略?

    将Beijing的分支merge到master,以下是默认的分支结构。并有以下三种分支集成策略。

    1 2
    1. create a merge commit: Beijing的分支会连接到master那里。
    2
    1. squash and merge: 将北京的合并到master,会将三个commit合并成一个merge到master上,master会多了一个commit,北京本身没有变化。
    3
    1. rebase and merge: 将北京的三个commit合并到master上,北京本身没有变化。
    4

    50 | 启用issue跟踪需求和任务

    提issue,然后还可以配置issue模板。

    51 | 如何用project管理issue?

    其实就是看板功能

    52 | 项目内部怎么实施code review?

    53 | 团队协作时如何做多分支的集成?

    54 | 怎样保证集成的质量?

    55 | 怎样把产品包发布到GitHub上?

    releases

    56 | 怎么给项目增加详细的指导文档?

    wiki

    57 | 国内互联网企业为什么喜欢GitLab?

    略略略

    58 | GitLab有哪些核心的功能?

    略略略

    59 | GitLab上怎么做项目管理?

    略略略

    60 | GitLab上怎么做code review?

    略略略

    61 | GitLab上怎么保证集成的质量?

    略略略

    62 | 怎么把应用部署到AWS上?

    略略略

    补充

    直接在全局设置
    git config --global http.sslVerify "false"
    当然是一个方法,但此方法的不足在于会玷污全局配置。下面介绍一个即方便又不玷污全局配置的方法。
    
    推荐的方法
    Windows
    打开 PowerShell
    
    $Env:GIT_SSL_NO_VERIFY="true"
    git clone https://abc.dev/user/repo.git
    然后 cd 进去
    
    cd .\repo\
    设置不验证证书
    
    git config http.sslVerify "false"
    Linux
    打开终端
    
    export GIT_SSL_NO_VERIFY="true"
    git clone https://abc.dev/user/repo.git
    然后 cd 进去
    
    cd ./repo/
    设置不验证证书
    
    git config http.sslVerify "false"
    

    相关文章

      网友评论

          本文标题:极客时间 Git 学习笔记

          本文链接:https://www.haomeiwen.com/subject/hllvzhtx.html