一、概念

  • 裸仓库:不包含工作区,通常作为远端的中心仓库而存在
    • 克隆的裸仓库可以本地 push,普通仓库无法进行本地 push
    • 裸仓库不含工作区,不会存在在裸仓库上直接提交变更的情况
  • 区域划分:工作区,暂存区,本地库
  • untracked:未被追踪,没有 add 的文件

    二、操作

    1. 配置

  • 全局配置用户名与邮箱

    • git config --global user.name "your name"
    • git config --global user.email "youremail@github.com"
  • 当前仓库配置用户名与邮箱
    • git config --local user.name "your name"
    • git config --local user.email "youremail@github.com"

2. 分支管理

  • 创建分支
    • git checkout -b 新分支名称 基于当前分支创建新分支,并切换到新分支
  • 切换分支
    • git checkout <分支名称>
  • 查看分支
    • git branch 查看本地分支
    • git branch -r 查看远程分支
    • git branch -a 查看本地和远程分支
  • 删除分支
    • git branch -D <分支名称> 删除本地分支
    • git branch origin -d <远程分支名称> 删除远程分支
  • 合并分支

    • git merge origin/远程分支名称 将远程分支与本地当前分支合并
    • git merge 其他分支名称 本地其他分支与本地当前分支合并

      3. 状态查询

  • 查看状态

    • git status
  • 查看历史操作记录
    • git reflog
  • 查看日志

    • git log --oneline 当前分支各个 commit 用一行显示
    • git log -n 显示就近的 n 个 commit
    • git log --oneline --graph --all 用图示显示所有分支的历史

      4. 文件暂存

  • 添加改动到stash

    • git stash push -m "message"
  • 查看stash列表
    • git stash list
  • 恢复改动
    • git stash pop <stash@{ID}> pop: 取会删除对应的保存记录
    • git stash apply <stash@{ID}> apply: 取但保留记录
  • 删除全部缓存

    • git stash clear

      5. 撤销

  • 撤销工作区修改

    • git checkout .
  • 版本回退

    • git reset --hard commit_sha 硬性回滚,简单粗暴,直接抛弃回滚之后改动(log 还是有保留,内容不要而已)
    • git reset --soft commit_sha1 软性回滚, 跟 rebase 常规用法差不多的效果,可以把提交的东西丢回暂存区和工作区,HEAD 的指向改变会对应的 commit,之后再考虑怎么 commit
    • git reset --soft HEAD~1 软回滚一个版本,可以理解为撤销最近一次的


      参考链接

  • https://juejin.cn/post/6844903971069362190#heading-16

  • https://juejin.cn/post/6844904149465710599#heading-46
  • https://www.processon.com/view/link/5fd224bfe0b34d06f4ec645a