# Git 的常用指令

命令 说明
git log 查看历史提交的版本
git reset --hard 版本号 将本地代码退回指定版本
git remote remove origin 移除本地
git remote add origin 仓库地址 新增仓库地址
git push -u origin master 提交代码到主线程
git rm -r --cached [文件|文件夹] 删除缓存中的文件
git pull origin master --allow-unrelated-histories fatal: refusing to merge unrelated histories
git checkout 文件路径 撤销提交
git clone 分支名 仓库地址 克隆某个分支代码
git checkout . 丢弃本次所有修改
git reset HEAD . 丢弃本次所有缓存文件 git add .
git reset --hard HEAD^ 丢弃本次 commit

# Git 分支操作

命令 说明
git clone 云仓库地址 克隆最新的线上代码
git switch dev 切换到开发主分支 dev
git checkout -b 分支名 创建并切换分支
日常代码开发中... 开发代码
git branch -a 查看所有分支名
git stash save 缓存名 缓存本地开发代码
git stash list 查看本地缓存代码
git pull origin dev 拉取线上 dev 分支的最新代码
git stash apply stash@{$num} 恢复缓存区代码
git stash drop stash@{$num} 丢弃缓存区代码
git add . 将本地文件加入缓存区
commitizen (opens new window) 通过 commitize 上传说明
commitizen init cz-conventional-changelog -D 初始化上传说明
git cz 开启 committize 上传界面
选择 feat 本次上传是新功能
feat/courseCategory 上传文件说明
"feat #Issue" 关联本次上传 Issue
git push --set-upstream origin 分支名 推送代码到云仓库
等待审核... 等待管理员审核代码
git merge 子分支名 将子分支合并到主分支
git push 将合并后的代码上传
git branch -D 分支名 删除本地分支名
git push origin --delete 分支名 删除线上分支名
  • type 说明
    • feat 新功能
    • fix 修复
    • refactor 重构
    • test 测试

# Git Hook 自动化部署

  1. 服务器的钩子创建
命令 说明
ssh root@服务器IP 链接服务器
cd 项目根目录 进入根目录
git config receive.denyCurrentBranch ignore 创建空仓库并接受代码提交
cd .git/hooks hook 监听代码提交
touch post-receive 创建 post-receive hook 钩子
vim post-receive 编辑 post-receive hook 钩子
post-receive 内容如下 保存内容
chmod +x post-receive 执行权限
  • post-receive 内容
#!/bin/sh
unset GIT_DIR
cd /www/wwwroot/项目名
git checkout -f
1
2
3
4
  1. 公钥的部署
命令 说明
ls -al ~/.ssh 检查本地电脑 SSH keys 是否存在
ssh-keygen -t rsa -C "邮箱地址" 生成新的 ssh key
直接回车键 跳过设置 passphrase
[pbcopy|clip] < ~/.ssh/id_rsa.pub [Mac|Window] 复制公钥
vi ~/.ssh/authorized_keys 将公钥复制到服务器
  1. 在本地项目根目录编写 auto.sh 脚本
set -e

. ~/.nvm/nvm.sh
nvm use 6.10.0
gitbook build

git init
git add -A
git commit -m 'auto upolad'
git push -f ssh://root@服务器IP/www/wwwroot/项目根目录/.git master

git pull origin master
git push -u origin master

. ~/.nvm/nvm.sh
nvm use 10.18.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  1. package.jsonscript 里面添加命令,可用 npm init 生成 package.json
{
	"scripts": {
    "auto": "bash auto.sh"
  }
}
1
2
3
4
5
  1. 编写 .gitignore 文件
node_modules
.DS_store
1
2
  1. 运行 npm run auto 即可上传文件到服务器实现同步更新