GIT笔记

188 阅读4分钟

一、底层命令

  • git对象

    // 生成一个key(hash值):value(压缩后的文件内容)键值对存于.git/objects
    git hash-object -w fileUrl
    
  • tree对象

    // 往暂存区添加一条记录存于.git/index
    git update-index --add --cacheinfo 100644 hash test.js
    // 生成树对象存到.git/objects
    git write-tree
    
  • commit对象

    // 生成一个提交对象到.git/objects
    echo 'one commit' | git commit-tree treehash
    
  • 对三个对象的查询

    // 查看对象的内容
    git cat-file -p hash
    // 查看对象的类型
    git cat-file -t hash
    
  • 查看暂存区git ls-files -s

二、上层命令

  • 查询版本git --version

  • 初始化配置

  1. 查看配置:git config --list

  2. 配置用户信息

    // 系统级,优先级最低
    git config --system user.name "lxrlg"
    git config --system user.email "lxrlg@163.com"
    // 全局,优先级次之
    git config --global user.name "damu"
    git config --global user.email damu@example.com
    // 项目级,优先级最高
    git config user.name "xxx"
    git config user.email "xxx@xxx.com"
    
  • 初始化仓库git init

  • 新增或修改

    git status
    git add ./
    git commit -m 'description...'
    git pull
    git push
    
  • 删除

    git rm dirName
    git status
    git commit -m 'description...'
    git pull
    git push
    
  • 重命名

    git mv oldDirName newDirName
    git status
    git commit -m 'description...'
    git pull
    git push
    
  • 查询

    // 查看工作目录中文件的状态【已跟踪(已提交 已暂存 已修改) 未跟踪】
    git status
    // 查看未暂存的修改
    git diff
    // 查看未提交的暂存
    git diff --cache
    // 查看提交记录
    git log --oneline
    
  • 分支

    /**
     * 分支的本质是一个提交对象
     * HEAD:是一个指针,默认指向master分支,切换分支时就是让HEAD指向不同的分支,每次有新的提
     * 交时,HEAD都会带着当前指向的分支一起往前移动
     */
    // 查看整个项目的分支图
    git log --oneline --decorate --graph --all
    // 查看分支列表
    git branch
    // 查看分支指向的最新的提交分支
    git branch -v
    // 在当前提交对象上创建新的分支
    git branch name
    // 在指定的提交对象上创建新的分支
    git branch name commithash
    // 切换分支
    git checkout name
    // 删除空的分支,删除已被合并的分支
    git branch -d name
    // 强制删除分支
    git branch -D name
    // 合并分支
    git merge branchName
    

三、eslint

// eslint:js代码检查工具
// 下载
npm i eslint -D
// 生成配置文件
npx eslint --init
// 检查js文件
npx eslint dirName
/**
 * eslint结合git,在package.json中配置
 * husky: 哈士奇, 为Git仓库设置钩子程序
 * 在git commit之前一定要通过npm run lint的检查,否则提交不了
 */
"husky": {
    "hooks": {
        "pre-commit": "npm run lint"  
    }
}

四、总结

  • 关于分支合并时产生冲突

    // 1.打开冲突的文件进行修改
    // 2.修改完成之后走正常的修改提交流程
    git status
    git add ./
    git commit -m '描述信息'
    
  • 切换分支的注意事项

    // 1.在切换分支的时候,一定要保证当前分支是干净的,即分支上所有的内容处于已提交状态
    // 2.分支上的内容是初始化创建,处于未跟踪状态时,不允许切换分支
    // 3.分支上的内容是初始化创建,第一次处于未暂存状态时,不允许切换分支
    // 4.分支上的所有内容处于已修改状态或第二次以后的已暂存状态时,不允许切换分支
    /**
     * 工作进行到一半,如果有切换分支的需求,应该将现有的工作存储起来:
     * 1.git stash:将当前分支上的工作推到一个栈中
     * 2.切换分支,进行其它工作,完成后,切回原分支
     * 3.git stash apply:将栈顶的工作内容还原,但不让任何内容出栈
     * 4.git stash drop:取出栈顶的工作内容后,就应该将其删除(出栈)
     * 5.git stash pop:git stash apply + git stash drop
     * 6.git stash list:查看存储
     */
    
  • 后悔药

    // 撤销工作目录的修改
    git checkout -- filename
    // 例如:
    git checkout -- src/pages/document.ejs
    // 撤销暂存区的修改
    git reset HEAD filename
    // 撤销提交
    git commit --amend
    
  • reset三部曲

    // 用commithash的内容重置HEAD内容
    git reset --soft commithash
    // 用commithash的内容重置HEAD内容 重置暂存区
    git reset [--mixed] commithash
    // 用commithash的内容重置HEAD内容 重置暂存区 重置工作目录
    git reset --hard commithash
    
  • 路径reset

    /**
     * 所有的路径reset都要省略第一步
     * 第一步是重置HEAD内容  我们知道HEAD本质指向一个分支 分支的本质是一个提交对象
     * 提交对象 指向一个树对象 树对象又很有可能指向多个git对象 一个git对象代表一个文件
     * HEAD可以代表一系列文件的状态
     */
    // 用commithash中filename的内容重置暂存区
    git reset [--mixed] commithash filename
    
  • checkout理解

    /**
     * git checkout branchname VS git reset --hard commithash
     * 共同点:都需要重置 HEAD 暂存区 工作目录
     * 区别:
     *    1. checkout 对工作目录是安全的,reset --hard 是强制覆盖
     *    2. checkout 动HEAD时不会带着分支走而是切换分支
     *    3. reset --hard 时是带着分支走
     */
    // checkout + path
    // 1.重置暂存区、重置工作目录
    git checkout commithash filename
    // 2.重置工作目录
    git checkout --filename