Git 管理常用命令

图片来自官网
由来: 公司最近要进行招人,所以整理了份 Git 的常用命令,以做参考。同时让自己也进行学习一波。

<center> Git 管理

一、Git 常用操作

  • 提交修改到本地分支

    1.git add .
    2.git commit -m "Add:1.登录模块已完成"
    
  • 查看修改

    1.git diff // 提交至暂存库后的修改记录
    2.git diff dc519c7 // 当前与某次提交暂存库后的修改记录
    3.git diff dev // 当前与 dev 分支比较的修改记录
    
  • 新建并切换分支

    1.git checkout -b debug
    
  • 合并代码

    1.git merge xxbranch
    
  • 关联远程分支

    1.git branch --set-upstream-to=origin/xxbranch xxbranch
    
  • 拉取 / 推送代码到远端

    1.git fetch // 拉取远程对应分支代码
    2.git diff // 查看修改
    3.git merge // 合并
    4.git push origin xxbranch
    
    1.git pull // 拉取远程对应分支更新并合并
    2.git push origin xxbranch
    
  • 修改上次 commit 信息

    1.git commit --amend
    
  • 删除本地分支

    1.git branch -d xxbranch
    
  • 删除远程分支

    1.git push origin --delete xxbranch
    
  • 标记 tag

    1.git tag v0.0.1 / git tag -a v0.0.1 -m "v0.0.1版本"
    2.git push origin v0.0.1 // 将本地 v0.0.1 的 tag 推送到远端服务器
    3.git push --tags / git push origin --tags // 推送所有 tag 到远端
    4.git tag // 查看 tag 列表
    5.git show v0.0.1 // 查看 tag 修改信息
    
  • 将当前修改存储至缓存区

    1.git stash // 将当前的修改放到缓存区
    2.git stash list // 查看 stash 列表
    3.git stash pop // 恢复至最近一次 stash, 同时删除该条记录
    4.git stash apply // 恢复至最近一次 stash, 记录还存在 list 中
    5.git stash show stash@{1} // 查看指定 stash 修改
    6.git stash apply stash@{1} // 恢复至指定 stash
    7.git stash drop stash@{1} // 删除指定 stash
    8.git stash clear // 删除所有 stash
    
  • 版本回退 / 回滚

    1.git reset --hard 123456
    2.git revert 123456
    

    针对远程

    1.自己的分支回滚可直接用 reset
    2.公共分支回滚要用 revert
    

二、线上 bug fix 流程

  • 假定 master 分支为当前线上版本分支, 此时线上分支出现了一个 bug 需要紧急修复, 而你此时正在 dev 下的 xxx 分支开发, 该如何操作?

    • 1.如果当前开发已告一段落:
    1.git add .
    2.git commit -m "当前功能已完成" 
    3.git checkout master 
    4.git checkout -b debug // 在 master 分支上新建 debug 分支用作修改
    5.git add .
    6.git commit -m "bug 已修改完成" 
    7.git checkout master
    8.git merge debug / git merge --no-ff -m "bug 已修改完成" debug // 合并 debug 分支
    9.git add .
    10.git commit -m "merge debug branch and fixed bug" 
    11.git pull
    12.git push origin master
    13.git branch -d debug // 删除 debug 分支
    
    • 2.如果当前分支功能开发尚未完成:
    git stash // 保存现场
    顺次执行上述 1 ~ 13 步
    git checkout xxbranch // 切回修复 bug 之前分支
    git stash pop // 恢复现场继续未完成开发
    

三、自己的远程分支版本回退

1.git reflog 
2.git reset --hard 123456
3.git push -f origin xxbranch // 本地分支回滚后版本将落后远程分支, 必须使用强制推送覆盖远程分支

附录

一、命令

    1. git add . 将代码提交到暂存区
    1. git commit -m "balabala" 将代码提交到工作区(本地库)
    1. git diff 查看提交至暂存库后的修改记录
    1. git checkout xxbranch 切换到某个分支
    1. git checkout -b xxbranch 创建并切换到某个分支
    1. git merge xxbranch 合并某个分支
    1. git merge --no-ff -m "balabala" 合并某个分支(临时分支用完后一般会删除, 则无法通过分支查询历史记录, 所以使用临时分支时需要使用 --no-ff 的方式,同时写上 -m 备注信息)
    1. git fetch 拉取远程对应分支
    1. git pull 拉取远程对应分支更新并合并
  • 10.git push origin xxbranch 推送当前分支到远程
  • 11.git push -f origin xxbranch 强制推送当前分支到远程
  • 12.git branch --set-upstream-to=origin/xxbranch xxbranch 本地分支与远程分支建立关联
  • 13.git stash 将当前的修改放到缓存区
  • 14.git stash list 查看 stash 列表
  • 15.git stash pop 恢复至最近一次 stash, 同时删除该记录
  • 16.git stash apply 恢复至最近一次 stash, 记录还存在 list 中
  • 17.git stash show stash@{1} 查看指定 stash 修改
  • 18.git stash apply stash@{1} 恢复至指定 stash
  • 19.git stash drop stash@{1} 删除指定 stash
  • 20.git stash clear 删除所有 stash
  • 21.git branch 查看本地分支列表
  • 22.git branch -a 查看远程分支列表
  • 23.git tag v0.0.1 标记 tag
  • 24.git tag -a v0.0.1 -m "v0.0.1版本" 标记 tag 并附带信息
  • 25.git push origin v0.0.1 将本地 v0.0.1 的 tag 推送到远端服务器
  • 26.git push --tags / git push origin --tags 推送所有 tag 到远端
  • 27.git tag 查看 tag 列表
  • 28.git show v0.0.1 查看 tag 修改信息
  • 29.git reset --hard 123456 版本回退
  • 30.git revert 123456 版本回滚, 会在当前基础上新增 commit 记录
  • 31.git log 显示所有提交过的版本信息
  • 32.git reflog 显示所有提交过的版本信息, 包括已经被删除的 commit 记录

二、git commit -m "xxx"

  • 提交 log: Action + Message
    • Action: Add / Mod(ified) / Del(ete) / Rem(ove) / Fix / Ref(actor) / Rea(dability)
      • Add 修改
      • Modified 修改
      • Delete Remove 删除
      • Fix 修复 bug
      • Refactor 重构
      • Readability 增加可读性
    • Message:对应的描述信息
  • 示例:git commit -m "Add:发言模块完成."

参考:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1. 安装 Github 查看是否安装git: $ git config --global user.name "...
    Albert_Sun阅读 13,737评论 9 163
  • 文/叶丹颖 沿着山坡 我们像孩子一样奔跑 仰起高高的头 把童年再过一遍 把春天再画一遍 忘记了疲倦 只记得歌唱——...
    树姑娘阅读 263评论 0 3
  • 山清水秀太阳高 秋高气爽清风飘 民众聚于村道口 等待铲车到跟前 礼炮点燃气氛烈 祠堂上香敬先祖 老屋庇护多代人 断...
    边边缘阅读 242评论 10 4
  • MBTI测试下来,我是ISFP型的,这是“专家型”的人格特质。所谓“专家型”,就是在某领域可以扎的很深,但广度不够...
    HelenHuang阅读 440评论 0 0
  • A果然到了最后一刻,我还是没有完成任务M明知道是如此,我还是没有吸取教训,没有做计划是最大的问题B以后一定要倒计时...
    陈珠芳2018阅读 186评论 0 1