Skip to content

安装

官网下载:https://git-scm.com/downloads (opens new window) 下载完成后使用默认进行安装。

安装完成后,在开始菜单里找到 Git -> Git Bash,蹦出一个类似命令行窗口的东西,就说明 Git 安装成功! 还需要最后一步设置,在命令行输入:

    git config --global user.name "Your Name"
    git config --global user.email "email@example.com"
    git config --global user.name "Your Name"
    git config --global user.email "email@example.com"

因为 Git 是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和 Email 地址。

创建版本库(仓库)

    # 创建仓库
    mkdir <仓库name>

    # 进入仓库
    cd <仓库name>

    # 此命令用于显示当前目录
    pwd

    # 把当前目录初始化成Git仓库
    git init
    # 创建仓库
    mkdir <仓库name>

    # 进入仓库
    cd <仓库name>

    # 此命令用于显示当前目录
    pwd

    # 把当前目录初始化成Git仓库
    git init

也不一定必须在空目录下创建 Git 仓库,选择一个已经有东西的目录直接git init也是可以的。

添加文件到仓库

在仓库目录下放入文件,如新建一个test.txt文件,然后使用git add test.txt命令告诉 Git,把文件添加到缓存区,然后使用git commit -m "提交描述"告诉 Git,把文件提交到仓库。

    # 添加指定文件或文件夹到缓存区,文件需添加后缀
    git add <文件或文件夹name>                   # 单个文件
    git add <文件或文件夹name> <文件或文件夹name>  # 多个文件

    # 或 全部文件同时添加到缓存区
    git add .

    # 把文件从缓存区提交至仓库
    git commit -m "提交描述"
    # 添加指定文件或文件夹到缓存区,文件需添加后缀
    git add <文件或文件夹name>                   # 单个文件
    git add <文件或文件夹name> <文件或文件夹name>  # 多个文件

    # 或 全部文件同时添加到缓存区
    git add .

    # 把文件从缓存区提交至仓库
    git commit -m "提交描述"

提交描述规范

在提交代码时输入清晰的说明有利于版本管理,Commit message 包括三个部分:Header,Body 和 Footer。格式如下:

    <type>(<scope>): <subject>
    // 空一行
    <body>
    // 空一行
    <footer>
    <type>(<scope>): <subject>
    // 空一行
    <body>
    // 空一行
    <footer>

其中,Header 是必需的,Body 和 Footer 可以省略。

Message Subject(Header)

Header部分只有一行,包括三个字段:type(必需)、scope(可选)和subject(必需)。

(1)type type用于说明 commit 的类别,只允许使用下面4个标识。

  • 📰 feat: 新特性(新需求)

  • 🚑 fix: 修改问题(缺陷修复)

  • 🎨 pref: 性能提升(优化类,业务逻辑不变)

  • 📚 docs: 文档

(2)scope

scope用于定义type影响的范围,比如数据层控制层视图层等等,视项目不同而不同。

(3)subject

subject是 commit 目的的简短描述,不超过50个字符。

Body

Body 部分是对本次 commit 的详细描述,可以分成多行,每行尽量不超过72个字符。下面是一个范例。

More detailed explanatory text, if necessary.  Wrap it to 
about 72 characters or so. 

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent
More detailed explanatory text, if necessary.  Wrap it to 
about 72 characters or so. 

Further paragraphs come after blank lines.

- Bullet points are okay, too
- Use a hanging indent

Footer 部分只用于两种情况。

(1)不兼容变动

如果当前代码与上一个版本不兼容,则 Footer 部分以BREAKING CHANGE开头,后面是对变动的描述、以及变动理由和迁移方法。

BREAKING CHANGE: isolate scope bindings definition has changed.

 To migrate the code follow the example below:

  Before:

  scope: {
      myAttr: 'attribute',
    }

    After:

    scope: {
      myAttr: '@',
    }

  The removed `inject` wasn't generaly useful for directives so there should be no code using it.
BREAKING CHANGE: isolate scope bindings definition has changed.

 To migrate the code follow the example below:

  Before:

  scope: {
      myAttr: 'attribute',
    }

    After:

    scope: {
      myAttr: '@',
    }

  The removed `inject` wasn't generaly useful for directives so there should be no code using it.

(2)关闭 Issue

如果当前 commit 针对某个issue,那么可以在 Footer 部分关闭这个 issue 。


Closes #234

Closes #234

也可以一次关闭多个 issue 。

Closes #123, #245, #992
Closes #123, #245, #992

3.4 Revert 还有一种特殊情况,如果当前 commit 用于撤销以前的 commit,则必须以revert:开头,后面跟着被撤销 Commit 的 Header。

revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.

Body部分的格式是固定的,必须写成This reverts commit <hash>.,其中的hash是被撤销 commit 的 SHA 标识符。

如果当前 commit 与被撤销的 commit,在同一个发布(release)里面,那么它们都不会出现在 Change log 里面。如果两者在不同的发布,那么当前 commit,会出现在 Change log 的Reverts小标题下面。

版本管理

####提交修改

如修改test.txt的内容后,运行git status命令看看被修改的文件,然后再使用git add test.txtgit commit -m "修改描述"把修改后的文件提交到仓库,提交后可再次使用git status查看当前状态。

    # 显示 新增/删除/被改动等 的文件
    git status
    # 显示 新增/删除/被改动等 的文件
    git status

查看版本记录

    # 查看版本记录
    git log   # 显示版本号、提交时间等信息
    # 查看版本记录
    git log   # 显示版本号、提交时间等信息

也可使用可视化工具查看 Git 版本历史: 在仓库目录右键 > Git BUI Here

回退版本

首先,Git 必须知道当前版本是哪个版本,在 Git 中,用HEAD表示当前版本,也就是最新的提交 1094adb...(注意我的提交 ID 和你的肯定不一样),上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上 100 个版本写 100 个^比较容易数不过来,所以写成HEAD~100

    # 回退到上一个版本
    $ git reset --hard HEAD^
    # 回退到上一个版本
    $ git reset --hard HEAD^

此时查看git log记录发现,原来最新的版本已经没有了,想回到原来最新的版本怎么办?这就需要知道最新的版本的版本号

    # 跳转到指定版本
    git reset --hard <版本号前几位>
    # 跳转到指定版本
    git reset --hard <版本号前几位>

但是不知道版本号怎么办?Git 提供了一个命令git reflog用来记录你的每一次命令

    git reflog
    git reflog

撤销修改

https://www.liaoxuefeng.com/wiki/896043488029600/897889638509536 (opens new window)

删除文件

https://www.liaoxuefeng.com/wiki/896043488029600/900002180232448

远程仓库

SSH 验证

使本机能关联远程仓库,首次需要 SSH 验证

  • 第 1 步:创建SSH Key。在用户主(C:\Users\dell)目录下,看看有没有.ssh 目录, 如果有,再看看这个目录下有没有id_rsaid_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。 如果没有,打开 Shell(Windows 下打开 Git Bash),创建SSH Key
  # 创建 SSH Key

  ssh-keygen -t rsa -C "邮件地址"
  # 创建 SSH Key

  ssh-keygen -t rsa -C "邮件地址"
  • 第 2 步:登陆 GitHub,右上角头像 > settings > SSH and GPG keys >Add SSH Key,在 key 的文本框里粘贴id_rsa.pub文件的内容

关联远程仓库

SSH 验证完成后,在 github 创建仓库,创建仓库时记得取消 Initialize this repository with a README的勾选,然后在本地命令:

    # 关联远程仓库,仓库名一般使用origin
    git remote add <仓库名> <远程仓库地址>

    # 示例
    git remote add origin git@github.com:xugaoyi/test.git
    # 关联远程仓库,仓库名一般使用origin
    git remote add <仓库名> <远程仓库地址>

    # 示例
    git remote add origin git@github.com:xugaoyi/test.git

下一步,就可以把本地库的所有内容推送到远程库上

    # 把文件推送到远程仓库
    git push -u <仓库名> <分支名>

    # 示例
    git push -u origin master
    # 把文件推送到远程仓库
    git push -u <仓库名> <分支名>

    # 示例
    git push -u origin master

由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git 不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

前提是目录已经git init初始化成仓库,并且git status状态是没有改动的,如果有改动则先git add .添加至缓存区,git commit -m '提交描述'提交至仓库,然后执行上面命令。

如创建仓库时勾选了 Initialize this repository with a README 则需先拉取README.md文件到本地仓库git pull

可关联多个远程仓库,注意给不同的远程仓库取不一样的名称,提交是分别按名称提交到不一样的远程仓库。

    # 简化的推送命令
    git push
    # 简化的推送命令
    git push

查看远程仓库

    # 查看远程仓库
    git remote -v
    # 查看远程仓库
    git remote -v

删除远程仓库

    # 删除远程仓库
    git remote rm <仓库名>
    # 删除远程仓库
    git remote rm <仓库名>

从远程库克隆项目

    # 从远程库克隆项目
    git clone <仓库地址>
    # 从远程库克隆项目
    git clone <仓库地址>
克隆指定分支
    # 克隆指定分支
    git clone -b <分支名> <仓库地址>
    # 克隆指定分支
    git clone -b <分支名> <仓库地址>

分支管理

创建分支

    # 创建分支
    git checkout -b <分支名>
    # 创建分支
    git checkout -b <分支名>

查看分支

    # 查看分支
    git branch
    # 查看分支
    git branch

查看分支时,在分支前带 * 号的表示当前的分支

切换分支

    # 切换分支
    git checkout <分支名>
    # 切换分支
    git checkout <分支名>

合并分支

    # 合并本地的分支
    git merge <分支名>

    # 合并远程的分支
    git merge <远程仓库名>/<分支名>
    # 合并本地的分支
    git merge <分支名>

    # 合并远程的分支
    git merge <远程仓库名>/<分支名>

注意,是将指定分支合并到当前分支,并非当前分支合并到指定分支。

一般情况下是把当前分支切换到主分支,然后把子分支合并到主分支

删除分支

    # 删除分支
    git branch -d <分支名>
    # 删除分支
    git branch -d <分支名>

修改分支名

    # 修改分支名
    git branch -m <原分支名> <新分支名>
    # 修改分支名
    git branch -m <原分支名> <新分支名>

帮助命令

如对命令不清楚时,可使用git help命令显示出 git 命令介绍。

    # 帮助命令
    git help

    $ git help
    usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>]

    These are common Git commands used in various situations:

    start a working area (see also: git help tutorial)
       clone      Clone a repository into a new directory
       init       Create an empty Git repository or reinitialize an existing one

    work on the current change (see also: git help everyday)
       add        Add file contents to the index
       mv         Move or rename a file, a directory, or a symlink
       reset      Reset current HEAD to the specified state
       rm         Remove files from the working tree and from the index

    examine the history and state (see also: git help revisions)
       bisect     Use binary search to find the commit that introduced a bug
       grep       Print lines matching a pattern
       log        Show commit logs
       show       Show various types of objects
       status     Show the working tree status

    grow, mark and tweak your common history
       branch     List, create, or delete branches
       checkout   Switch branches or restore working tree files
       commit     Record changes to the repository
       diff       Show changes between commits, commit and working tree, etc
       merge      Join two or more development histories together
       rebase     Reapply commits on top of another base tip
       tag        Create, list, delete or verify a tag object signed with GPG

    collaborate (see also: git help workflows)
       fetch      Download objects and refs from another repository
       pull       Fetch from and integrate with another repository or a local branch
       push       Update remote refs along with associated objects

    'git help -a' and 'git help -g' list available subcommands and some
    concept guides. See 'git help <command>' or 'git help <concept>'
    to read about a specific subcommand or concept.
    # 帮助命令
    git help

    $ git help
    usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>]

    These are common Git commands used in various situations:

    start a working area (see also: git help tutorial)
       clone      Clone a repository into a new directory
       init       Create an empty Git repository or reinitialize an existing one

    work on the current change (see also: git help everyday)
       add        Add file contents to the index
       mv         Move or rename a file, a directory, or a symlink
       reset      Reset current HEAD to the specified state
       rm         Remove files from the working tree and from the index

    examine the history and state (see also: git help revisions)
       bisect     Use binary search to find the commit that introduced a bug
       grep       Print lines matching a pattern
       log        Show commit logs
       show       Show various types of objects
       status     Show the working tree status

    grow, mark and tweak your common history
       branch     List, create, or delete branches
       checkout   Switch branches or restore working tree files
       commit     Record changes to the repository
       diff       Show changes between commits, commit and working tree, etc
       merge      Join two or more development histories together
       rebase     Reapply commits on top of another base tip
       tag        Create, list, delete or verify a tag object signed with GPG

    collaborate (see also: git help workflows)
       fetch      Download objects and refs from another repository
       pull       Fetch from and integrate with another repository or a local branch
       push       Update remote refs along with associated objects

    'git help -a' and 'git help -g' list available subcommands and some
    concept guides. See 'git help <command>' or 'git help <concept>'
    to read about a specific subcommand or concept.

翻译工具翻译中文

    $ git help
    使用:git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>]

    这些是在各种情况下使用的通用Git命令:

    start a working area (参见命令: git help tutorial)
       clone      将存储库克隆到新目录中
       init       创建一个空的Git存储库或重新初始化一个现有的存储库

    work on the current change (参见命令: git help everyday)
       add        将文件内容添加到索引中
       mv         移动或重命名文件、目录或符号链接
       reset      将当前磁头重置为指定状态
       rm         从工作树和索引中删除文件

    examine the history and state (参见命令: git help revisions)
       bisect     使用二分查找查找引入错误的提交
       grep       打印与模式匹配的行
       log        显示提交日志
       show       显示各种类型的对象
       status     显示工作树状态

    grow, mark and tweak your common history
       branch     列出、创建或删除分支
       checkout   切换分支或还原工作树文件
       commit     记录对存储库的更改
       diff       显示提交、提交和工作树等之间的更改
       merge      将两个或多个开发历史连接在一起
       rebase     在另一个基本提示之上重新应用提交
       tag        创建、列表、删除或验证用GPG签名的标记对象

    collaborate (参见命令: git help workflows)
       fetch      从另一个存储库下载对象和引用
       pull       从另一个存储库或本地分支获取并与之集成
       push       更新远程引用和相关对象

    'git help -a' 和 'git help -g' 列出可用的子命令和一些概念指导。
    命令'git help <command>' 或 'git help <concept>' 查看特定子命令或概念.
    $ git help
    使用:git [--version] [--help] [-C <path>] [-c <name>=<value>]
               [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
               [-p | --paginate | -P | --no-pager] [--no-replace-objects] [--bare]
               [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
               <command> [<args>]

    这些是在各种情况下使用的通用Git命令:

    start a working area (参见命令: git help tutorial)
       clone      将存储库克隆到新目录中
       init       创建一个空的Git存储库或重新初始化一个现有的存储库

    work on the current change (参见命令: git help everyday)
       add        将文件内容添加到索引中
       mv         移动或重命名文件、目录或符号链接
       reset      将当前磁头重置为指定状态
       rm         从工作树和索引中删除文件

    examine the history and state (参见命令: git help revisions)
       bisect     使用二分查找查找引入错误的提交
       grep       打印与模式匹配的行
       log        显示提交日志
       show       显示各种类型的对象
       status     显示工作树状态

    grow, mark and tweak your common history
       branch     列出、创建或删除分支
       checkout   切换分支或还原工作树文件
       commit     记录对存储库的更改
       diff       显示提交、提交和工作树等之间的更改
       merge      将两个或多个开发历史连接在一起
       rebase     在另一个基本提示之上重新应用提交
       tag        创建、列表、删除或验证用GPG签名的标记对象

    collaborate (参见命令: git help workflows)
       fetch      从另一个存储库下载对象和引用
       pull       从另一个存储库或本地分支获取并与之集成
       push       更新远程引用和相关对象

    'git help -a' 和 'git help -g' 列出可用的子命令和一些概念指导。
    命令'git help <command>' 或 'git help <concept>' 查看特定子命令或概念.

参考文档

https://www.liaoxuefeng.com/wiki/896043488029600 (opens new window)