Git是什么?

Git是目前世界上最先进的分布式版本控制系统。
git教程

Git命令要多加练习 在线练习网站

一般流程

git工作区-暂存区-仓储-远程.png

  1. git clone 一个仓库或子仓库(git/http(s))建议采用ssh(使用git协议) 配置多个SSH的key 建议参考多【ssh的配置
  2. git checkout -b xxx origin/master 基于master分支新建分支 todo Something in workspace(工作区)
  3. git add . 添加到Index(暂存区) git commit -m ‘msg’添加到Repository(本地仓库) git push origin xxx 推送本地仓库到Remote(远程仓库)

    commit msg 规范可以参考 image.png 多分支管理合并策略 参考 image.png

  4. 这样远程就有一个xxx的分支 可以配合多分支管理策略 管理部署和发布

Git别名设置

建议如下配置 参考【廖雪峰-git别名配置

  1. git config --global alias.st status
  2. git config --global alias.co
  3. git config --global alias.br branch
  4. git config --global alias.last 'log -1'
  5. git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

Git基本操作

常用的git命令

  • git add . git add * git add src/* 添加到index中
  • git commit 提交到本地仓库中
  • git push origin master 推送到远程仓库中 git push origin dev 推送到远程仓库 dev分支中

    git 解决冲突

  • 下载冲突到缓存区 git pull不安全

  • git fetch  远程到 本地仓库
  • git rebase 本地仓库 和本地合并

    git 合并分支

  • git checkout xxx 切换到 xxx分支上

  • git merge —no-ff develop 合并分支到develop上 —no-ff 快速合并

    创建分支

  • git checkout -b feature-x develop 基于develop分支创建feature-x 分支 开发完成后,将功能分支合并到develop分支: * git checkout develop 切换分支

  • git merge --no-ff feature-x 合并分支 删除feature分支:
  • git branch -d feature-x 删除分支

查找head 手动 合并 代码
开始

  • git add add文件
  • git commit 添加
  • git pull 更新
  • git push 提交

处理流程 小乌龟

  • commit
    • fetch
    • rebase

git log统计

统计代码量可以采用cloc

  1. 统计个人代码量

    1. git log --author="jartto" --pretty=tformat: --numstat | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' -
  2. 贡献值统计

    1. git log --pretty='%aN' | sort -u | wc -l

    3 .查看排名前 5 的贡献者

    1. git log --pretty='%aN' | sort | uniq -c | sort -k1 -n -r | head -n 5

git cherry-pick

git cherry-pick命令的作用,就是将指定的提交(commit)应用于其他分支。
具体参看阮大师的
git cherry-pick hashA hashB

  1. git cherry-pick <HashA> <HashB>

git 拾取 包含A 。。B的hash

  1. git cherry-pick A^..B