[工具]关于Git

Git

Steven-Zhl 头像
[工具]关于Git

Git是一个分布式版本控制系统,也是目前世界上最常用的该类软件

值得一提的是,它的作者是Linus Torvalds(Linux和NVIDIA F**K You创始人),他在2005年为了管理Linux内核开发而开发了Git

不管怎么说,在现代任何和计算机相关的应用场景往往都要用到git,所以学习git是非常有必要的

目录

1 安装与初始化

  • Windows:官网下载安装即可,推荐添加到环境变量中:

    Path环境变量下添加<Git安装路径>\cmd<Git安装路径>\bin两个路径即可

  • Linux/MacOS:建议全程用包管理器即可

    例如Ubuntu下:sudo apt install git

    MacOS下:brew install git

  • 安装好后可以在终端输入git -v查看版本号,如果有输出则说明安装成功

    说起来大多数命令行软件都有这个功能

2 配置(git config)

  • 配置文件分为系统级用户级,分别对应/etc/gitconfig~/.gitconfig

    通常来说,系统级配置文件中可以配置git的安装路径,用户级配置文件中可以配置用户名和邮箱等。所以大多数时候我们只需要配置用户级配置文件即可

  • 常用示例:

    git config --global user.name "Steven-Zhl" # 全局配置,对所有仓库都生效
    git config --global user.email StevenZhl1001@gmail.com # 系统配置,对当前系统所有用户都生效
    git config credential.helper store # 记住密码
    git config --global --list # 查看配置(全局配置)
    
  • 总结起来,git config的命令格式如下:
  git config <作用域> <配置项> <值>
graph LR A[git config] --> B[作用域] A --> C[配置项] A --> D[值] --> 若值包含空格则需要用英文引号括起来 B --> E[--system] --> 系统配置,对当前系统所有用户都生效 --> 存放在/etc/gitconfig文件中 B --> F[--global] --> 全局配置,对所有仓库都生效 --> 存放在username/.gitconfig文件中 B --> G[--local] --> 仓库配置,仅对当前仓库生效,使用的很少 --> 存放在.git/config文件中 C --> H[user.name] C --> I[user.email]

完整参考:E2. 配置

3 创建仓库

创建仓库其实分两种情况:新建仓库克隆仓库

3.1 新建仓库(git init)

新建仓库指在本地新建一个仓库,从0开始

  • 常用示例:

    git init <项目名> # 在 ./<项目名> 下创建仓库
    git init # 在./下创建仓库,也就是让当前所在的目录成为仓库
    git init <路径> # 在指定路径下创建仓库
    

3.2 克隆仓库(git clone)

克隆仓库指从远程仓库(通常从代码托管平台)克隆一份到本地

  • 常用示例:

    git clone <仓库地址> # 克隆仓库到./<仓库名>下
    git clone <仓库地址> <路径> # 克隆仓库到<路径>/<仓库名>下
    

完整参考:E.3 创建仓库

4. 工作区域和文件状态

本节主要介绍Git最精妙的设计,也是它和其他版本控制系统最大的区别

虽然和使用没太大关系,但懂这个目的是知其所以然

  • Git的工作区域分为工作区暂存区本地仓库

Extra. 指令汇总

按照功能分类

E1. 杂项

  • git -v: 查看当前git版本
  • git <子功能(可选)> -h: 查看对于子功能的帮助

E2. 配置

usage: git config [<options>]

Config file location
    --global              use global config file
    --system              use system config file
    --local               use repository config file
    --worktree            use per-worktree config file
    -f, --file <file>     use given config file
    --blob <blob-id>      read config from given blob object

Action
    --get                 get value: name [value-pattern]
    --get-all             get all values: key [value-pattern]
    --get-regexp          get values for regexp: name-regex [value-pattern]
    --get-urlmatch        get value specific for the URL: section[.var] URL
    --replace-all         replace all matching variables: name value [value-pattern]
    --add                 add a new variable: name value
    --unset               remove a variable: name [value-pattern]
    --unset-all           remove all matches: name [value-pattern]
    --rename-section      rename section: old-name new-name
    --remove-section      remove a section: name
    -l, --list            list all
    --fixed-value         use string equality when comparing values to 'value-pattern'
    -e, --edit            open an editor
    --get-color           find the color configured: slot [default]
    --get-colorbool       find the color setting: slot [stdout-is-tty]

Type
    -t, --type <type>     value is given this type
    --bool                value is "true" or "false"
    --int                 value is decimal number
    --bool-or-int         value is --bool or --int
    --bool-or-str         value is --bool or string
    --path                value is a path (file or directory name)
    --expiry-date         value is an expiry date

Other
    -z, --null            terminate values with NUL byte
    --name-only           show variable names only
    --includes            respect include directives on lookup
    --show-origin         show origin of config (file, standard input, blob, command line)
    --show-scope          show scope of config (worktree, local, global, system, command)
    --default <value>     with --get, use default value when missing entry

E3. 创建仓库

  • git init
usage: git init [-q | --quiet] [--bare] [--template=<template-directory>]
                [--separate-git-dir <git-dir>] [--object-format=<format>]
                [-b <branch-name> | --initial-branch=<branch-name>]
                [--shared[=<permissions>]] [<directory>]

    --template <template-directory>
                          directory from which templates will be used
    --bare                create a bare repository
    --shared[=<permissions>]
                          specify that the git repository is to be shared amongst several users
    -q, --quiet           be quiet
    --separate-git-dir <gitdir>
                          separate git dir from working tree
    -b, --initial-branch <name>
                          override the name of the initial branch
    --object-format <hash>
                          specify the hash algorithm to use
  • git clone
usage: git clone [<options>] [--] <repo> [<dir>]

    -v, --verbose         be more verbose
    -q, --quiet           be more quiet
    --progress            force progress reporting
    --reject-shallow      don't clone shallow repository
    -n, --no-checkout     don't create a checkout
    --bare                create a bare repository
    --mirror              create a mirror repository (implies bare)
    -l, --local           to clone from a local repository
    --no-hardlinks        don't use local hardlinks, always copy
    -s, --shared          setup as shared repository
    --recurse-submodules[=<pathspec>]
                          initialize submodules in the clone
    --recursive[=<pathspec>]
                          alias of --recurse-submodules
    -j, --jobs <n>        number of submodules cloned in parallel
    --template <template-directory>
                          directory from which templates will be used
    --reference <repo>    reference repository
    --reference-if-able <repo>
                          reference repository
    --dissociate          use --reference only while cloning
    -o, --origin <name>   use <name> instead of 'origin' to track upstream
    -b, --branch <branch>
                          checkout <branch> instead of the remote's HEAD
    -u, --upload-pack <path>
                          path to git-upload-pack on the remote
    --depth <depth>       create a shallow clone of that depth
    --shallow-since <time>
                          create a shallow clone since a specific time
    --shallow-exclude <revision>
                          deepen history of shallow clone, excluding rev
    --single-branch       clone only one branch, HEAD or --branch
    --no-tags             don't clone any tags, and make later fetches not to follow them
    --shallow-submodules  any cloned submodules will be shallow
    --separate-git-dir <gitdir>
                          separate git dir from working tree
    -c, --config <key=value>
                          set config inside the new repository
    --server-option <server-specific>
                          option to transmit
    -4, --ipv4            use IPv4 addresses only
    -6, --ipv6            use IPv6 addresses only
    --filter <args>       object filtering
    --also-filter-submodules
                          apply partial clone filters to submodules
    --remote-submodules   any cloned submodules will use their remote-tracking branch
    --sparse              initialize sparse-checkout file to include only files at root
    --bundle-uri <uri>    a URI for downloading bundles before fetching from origin remote