git工作中常用命令

167 阅读3分钟

git工作中常用命令

  • git init (在当前目录下初始化一个git代码库)
  • git clone [url.git] (从远程仓库克隆一个项目)
  • git config --list (查看当前git配置)
  • git config --golbal user.name [name] (设置提交代码时的用户名称)
  • git config --global user.email 1397424@qq.com (设置用户地址)
  • git add [file1] [file2] 添加指定文件到暂存区
  • git add [dir] 添加指定目录到暂存区,包括子目录
  • git add . 添加当前目录所有文件到暂存区
  • git commit -m [message] 提交暂存区代码到本地仓库
  • git branch 查看所有本地分支
  • git branch -r 查看所有远程分支
  • git branch -a 查看所有本地分支和远程分支
  • git branch [branch-name] 新建一个本地分支,但依然停留在当前分支
  • git branch -d [branch-name] 删除本地分支
  • git branch -dr [remote-branch-name] 删除远程分支
  • git checkout [branch-name] 切换本地分支
  • git merge [branch-name] 合并指定分支到当前分支
  • git reset --hard 重置暂存区与工作区,与上一次commit保持一致
  • git log 查看当前Head(commit hash)
  • git checkout --track remotes/origin/dev 在本地创建dev分支并关联remotes/origin/dev远程仓库(自动创建本地仓库名和远程一样的仓库名)
  • git log [branch-name] 查看指定分支的提交记录

1.git config --list 查看当前git用户的配置信息

core.symlinks=false
core.autocrlf=true
core.fscache=true
color.diff=auto
color.status=auto
color.branch=auto
color.interactive=true
help.format=html
rebase.autosquash=true
http.sslbackend=openssl
http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt
credential.helper=manager
user.name=konglingzhan_zt
user.email=1397424822@qq.com

2.git迁移a平台(github)仓库到b平台,保持commi记录

3.初始化一个新的git项目,并推送到新建的远程仓库

本地电脑项目根目录下执行,如下命令

  • git init (初始化一个Git项目)
  • git add . (将项目的所有文件添加到本地暂存区)
  • git commit -m "message" (添加暂存区文件到git本地仓库)
  • git remote add origin url.com.git(关联远程仓库)
  • git push -u origin master(推送本地仓库代码到远程仓库)

4.推送已有项目新开发模块到远程仓库

本地电脑项目根目录下执行,如下命令

  • git add .
  • git commit -m 'message'
  • git pull(这一步完成之后有冲突要解决冲突然后再把冲突代码git add . ; git commit -m 'message')
  • git push

5.将a分支代码合并到b分支

  • git checkout a 切换到a分支
  • git pull 拉取a分支最新代码
  • git checkout b 切换到b分支
  • git pull 拉取b分支最新代码
  • git merge a 将a分支代码合并到b分支

有冲突的情况下解决冲突后。git add . ;git commit -m 'message' ;git push 提交到远程仓库

6.git结合eslint在提交代码前做eslint校验或者修复

原理:git提交前预检查主要是在项目目录下的.git/hooks文件下注入生命钩子

  • 手动注入:自己编写git生命钩子文件如:pre-commit
#!/bin/sh
# husky

# Hook created by Husky
#   Version: 1.3.1
#   At: 2021-3-16 11:14:09
#   See: https://github.com/typicode/husky#readme

# From npm package
#   Name: husky
#   Directory: undefined
#   Homepage: https://github.com/typicode/husky#readme

scriptPath="node_modules/husky/run.js"
hookName=`basename "$0"`
gitParams="$*"

debug() {
  [ "${HUSKY_DEBUG}" = "true" ] && echo "husky:debug $1"
}

debug "$hookName hook started..."

if [ -f "$scriptPath" ]; then
  # if [ -t 1 ]; then
  #   exec < /dev/tty
  # fi
  if [ -f ~/.huskyrc ]; then
    debug "source ~/.huskyrc"
    source ~/.huskyrc
  fi
  node "$scriptPath" $hookName "$gitParams"
else
  echo "Can't find Husky, skipping $hookName hook"
  echo "You can reinstall it using 'npm install husky --save-dev' or delete this hook"
fi
  • 通过husky自动注入git生命钩子 使用npm安装husky这个包后,husky会自动在项目根目录的.git/hooks 注入生命钩子文件。 配置项目的package.json文件去完成eslint检查
{
  "scripts": {
    "lint": "eslint --ext .js,.vue src",
    "fix": "eslint --ignore-pattern 'node_modules/*'  --fix **/*.js **/*.vue"
  },
  "husky": {
    "hooks": {
      "pre-commit": "yarn lint"
    }
  } 
}
 
  • git结合lint-staged(npm安装)执行eslint检查后的后续操作

当代码都可以使用eslint --fix 修复的时候执行完pre-commit这个钩子后,commit这步也会执行成功,否则失败。

{
    "scripts": {
        "lint": "eslint --ext .js,.vue src",
        "fix": "eslint --ignore-pattern 'node_modules/*'  --fix **/*.js **/*.vue"
     },
    "husky": {
        "hooks": {
          "pre-commit": "lint-staged"
        }
     },
    "lint-staged": {
        "src/**/*.{js,vue}": [
          "eslint --fix",
          "git add" // 修复后提交代码到暂存区
        ]
     },
}