Git

常用 Git 命令

提示

[xxx] 均为可选参数

# 拷贝一个 Git 仓库到本地
git clone 仓库地址
git clone 仓库地址 --depth 1 # 只克隆最近一次的 commit
1
2
3
# 查看当前的 Git 配置
git config --list
# 设置使用 Git 时的用户名称
git config [--global] user.name "名称"
# 设置使用 Git 时的邮箱地址
git config [--global] user.email "邮箱"
1
2
3
4
5
6
# 添加所有文件到暂存区
git add .
1
2
# 提交暂存区到仓库区
git commit -m "提交信息"
git commit --amend # 增补提交,使用上次的 commit 信息,不添加新的 commit 记录
1
2
3
# 显示变更的文件
git status
        -s # 精简输出
1
2
3
# 列出所有本地分支
git branch
        分支名 # 新建一个分支(停留在当前分支)
        -r # 列出所有远程分支
        -a # 列出所有本地分支和远程分支
        -d 分支名 # 删除分支
        -r # 列出所有远程分支
# 新建一个空白分支
git checkout --orphan 分支名
1
2
3
4
5
6
7
8
9
# 合并指定分支到当前分支
git merge 分支名
1
2
# 显示所有远程仓库
git remote -v
1
2
# 取回远程仓库的变化,并与本地分支合并
git pull [remote][branch]
# 上传本地指定分支到远程仓库
git push [remote][branch]
# 强行推送当前分支到远程仓库,忽略冲突
git push [remote] --force
1
2
3
4
5
6
# 只暂存被追踪的文件
git stash
        save '说明信息' # 添加说明信息
        -u # 暂存所有文件
# 查看 stash 列表
git stash list
# 取出最近一次的 stash
git stash apply
# 取出并删除最近一次的 stash
git stash pop
# 清空所有 stash
git stash clear
1
2
3
4
5
6
7
8
9
10
11
12
# 查看提交过的完整日志
git log
        --oneline # 查看精简日志(精简版本号和提交信息)
        --pretty=oneline # 查看精简日志(完整版本号和提交信息)
# 查看所有分支的所有操作记录(包括被删除的 commit 记录和 reset 操作)
git reflog
1
2
3
4
5
6
# 撤销 commit 操作
git reset --soft HEAD~1 # git reset --soft commit_id
# 撤销 commit 和 add 操作
git reset --mixed HEAD~1 # git reset --mixed commit_id
# 撤销 commit 和 add 操作同时撤销本地已追踪内容的修改
git reset --hard HEAD~1 # git reset --hard commit_id
1
2
3
4
5
6

查看完整版 Git 命令

三年 Git 使用心得 & 常见问题整理open in new window

git 命令大全 githubopen in new window

commit 常用 type

type含义
feat新功能
fix修复 bug
docs修改文档
style代码格式修改
refactor重构(即不是新增功能,也不是修复 bug)
perf更改代码以提高性能
test增加测试
build构建过程或辅助工具的变动
ci修改项目持续集成流程
chore其他类型的提交
revert恢复上一次提交

删除 Git 中的所有提交历史记录

提示

master 分支为例

# 创建 orphan 分支
git checkout --orphan [分支名]

# 添加需要上传文件
git add .

# 提交更改
git commit -m "Initial"

# 删除需要清空提交记录的分支
git branch -D master

# 将当前分支重命名为需要清空提交记录的分支名
git branch -m master

# 强制更新存储库
git push -f origin master
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

同步 github fork 项目上游更新

# 1. 添加上游仓库
git remote add upstream https://github.com/项目地址

# 2. 拉取上游变动
git fetch upstream

# 3. 合并(以 master 位置为例)
git rebase upstream/master
# OR
git merge upstream/master

# 4. 更新远程 fork 仓库分支(以 master 位置为例)
git push origin master
1
2
3
4
5
6
7
8
9
10
11
12
13

将代码提交到 github 的 gh-pages 分支

  1. 安装 gh-pages
yarn add -D gh-pages
# OR npm install -D gh-pages
1
2
  1. package.json 中添加如下脚本
"deploy": "gh-pages -d dist -m deploy",
"deploy:build": "npm run build && npm run deploy"
1
2
  1. 运行 deploy 脚本
yarn deploy
# OR npm run deploy
1
2

使用 GitHub Actions 自动部署

GitHub Actionsopen in new window 是 GitHub 的持续集成服务

配置 Secrets

Action 需要有操作仓库的权限

GitHub 官方的帮助文档:创建用于命令行的个人访问令牌open in new window

打开需要配置 Actions 的仓库,进入 Settings/Secrets 页面,配置 ACCESS_TOKEN 变量,储存内容为刚刚创建的个人访问令牌

编写 workflow 文件

  1. 点击仓库的 Actions 按钮
  2. 点击 Set up a workflow yourself 按钮
  3. 复制如下内容
name: GitHub Actions Build and Deploy

# 触发条件: push 到 master 分支后
on:
  push:
    branches:
      - master

# 设置上海时区
env:
  TZ: Asia/Shanghai

# 任务
jobs:
  build-and-deploy:
    # 服务器环境:最新版 ubuntu
    runs-on: ubuntu-latest
    steps:
      # 拉取代码
      - name: Checkout
        uses: actions/checkout@v2
        with:
          persist-credentials: false

      # 打包静态文件
      - name: Build
        run: npm install && npm run build

      # 部署
      - name: Deploy
        uses: JamesIves/github-pages-deploy-action@releases/v3
        with:
          # GitHub 密钥
          ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
          # GitHub Pages 读取的分支
          BRANCH: gh-pages
          # 静态文件所在目录
          FOLDER: dist
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

详细教程可以参考阮一峰老师的GitHub Actions 入门教程open in new window

GitHub Actions 中文文档open in new window

上次更新:
(adsbygoogle = window.adsbygoogle || []).push({});