Git

2022-07-13  本文已影响0人  bowen_wu

概述

Git 是分布式版本控制系统

Git 基本单元

仓库

commit

仓库从一个状态转移到另外一个状态是发生的改变 => commit 对象 => commit 对象不可变,其中包含

Branch

远程仓库 本地仓库 fork

Merge

Join two or more development histories together. => 合并 => 将两个或多个状态(代码快照/代码副本)合并,并产生一个合并提交 => Git 逐行检查冲突并尝试自动解决 =>
解决失败,报错

优点

缺点

git merge --squash

rebase

Reapply commits on top of another base tip. => 将某个状态上的变更(commit)挨个重演 => 共享分支不要 force push

      A---B---C master
     /
    D---E---F  feature
    
    // 在 master 分支上
    git rebase feature
    
              A'---B'---C' master
             /
    D---E---F  
    
    // 在 feature 分支上
    git rebase master 
    
      A---B---C 
     /         \
    D           E'---F'  feature
   

优点

缺点

log

checkout

reset

使用场景

cherry-pick

使用场景

revert

使用场景

bisect

Use binary search to find the commit that introduced a bug.

手动 bisect

  1. git bisect start => 开始 bisect
  2. git bisect bad => 告知当前版本是 bad
  3. git checkout <status> => 恢复到 status 状态
  4. git bisect good => 告知当前版本是 good
  5. git bisect skip => 跳过当前版本
  6. ...
  7. git bisect reset => 结束 bisect

使用场景

自动 bisect

  1. touch run.sh
  2. 将手动的部分写入 run.sh script 中 => shell script:
    • exit(0) => good
    • exit(1) => bad
  3. git checkout <good-status> => 恢复到 good status 状态
  4. ./run.sh => 检测脚本是否符合预期
  5. git checkout <bad-status> => 恢复到 bad status 状态
  6. ./run.sh => 检测脚本是否符合预期
  7. git bisect bad => 告知当前版本是 bad => 如果没有运行 git bisect start,当前命令会询问是否开启 bisect
  8. git checkout <good-status> => 恢复到 good status 状态
  9. git bisect good => 告知当前版本是 good
  10. git bisect run <path>/<to>/<run-shell-script.sh> => 告知 bisect 一个范围(good - bad),之后使用脚本在这个范围内进行二分查找

stash

Stash the changes in a dirty working directory away. => 临时性将工作目录的变更存储起来,然后清空工作目录

使用场景

来了一个紧急的 bug。将手头上的工作使用 git stash 存储(暂存区)起来,之后开始修复 bug

tag

Create, list, delete or verify a tag object signed with GPG.

Git 工作流

  1. 一个或两个共享的稳定分支
    • master
    • develop
  2. 临时性的功能分支
    • feature
    • bugfix
    • release

搭建 Git 服务器

gogs => go git service

步骤

Root 用户

  1. 安装 docker => api-get update && apt-get install docker.io
  2. 创建 git 用户 => adduser git
  3. 使 git 用户可以运行 docker => usermod -aG docker git
  4. 创建 gogs 运行目录 => mkdir -p /app/gogs
  5. 改变目录的所有人 => chown git:git /app/gogs

Git 用户

  1. 创建 SSH 目录 => mkdir -p ~/gogs/git/.ssh
  2. 建立软连接 => ln -s ~/gogs/git/.ssh ~/.ssh
  3. 生成 SSH key => ssh-keygen -t rsa -P ''
  4. cat ~/.ssh/id_ras.pub >> ~/.ssh/authorized_keys
  5. 对 SSH key 更严格的权限 => chmod 700 ~/.ssh && chmod 600 ~/.ssh/*
  6. 把服务器的 22 端口请求转发到 docker Git 服务器
    cat >/app/gogs/gogs <<'END'
    #!/bin/sh
    ssh -p 10022 -o StrictHostKeyChecking=no git@127.0.0.1 \
    "SSH_ORIGINAL_COMMAND=\"$SSH_ORIGINAL_COMMAND\" $0 $@"
    END
    
  7. chmod 755 /app/gogs/gogs
  8. 启动 docker => docker run -v ~git/gogs:/data -p 127.0.0.1:10022:22 -p 3000:3000 gogs/gogs
  9. 运行脚本 => /app/gogs/gogs => Gogs - A painless self-hosted Git service
  10. 访问 => <ip>:3000
  11. 数据库类型 => SQLite3
  12. 数据库文件路径 => /data/gogs.db
  13. 域名 => ip
  14. 应用 URL => http://<ip>:3000/

知识点

  1. octopus merge => octopuss 八爪猫 == octopus pussy => octocat(八爪猫) => github logo
  2. stage => 舞台
  3. UNIX 约定 => 程序成功退出 => 返回值0 => 否则非0
  4. git pull == git fetch + git merge
  5. git pull --rebase == git fetch + git rebase
  6. 提交记录是一条线优点
    • 清楚直观
    • 每个提交代表一个单独的完整的功能点,方便回滚
    • 每个提交都是稳定的,方便进行 bisect
上一篇 下一篇

猜你喜欢

热点阅读