Git安装及使用教程

随笔3周前发布 土豪灯
30 0 0

1、下载

官网地址:https://git-scm.com/download

2、安装

  1. 打开.exe安装程序, 点击next

  2. 选择安装路径(根据个人喜好决定)

  3. 选择组件

    Git安装及使用教程

    • Additional Icons

      • On the Desktop // 添加桌面图标

    • Windows Explorer integration // 右键菜单添加以下两个选项

      • Git Bash Here

      • Git GUI Here

    • Git LFS (Large File Support // 大文件支持

    • Associate .git* configuration files with the default text editor // 关联.git后缀文件

    • Associate .sh files to be run with Bash // 关联.sh文件

    • Check daily for Git for Windows updates // 每天检查版本更新

    • (NEW!) Add a Git Bash Profile to Windows Terminal // 将Git Bash添加到Windows Terminal中

    • (NEW!)Scalar (Git add-on to manage large-scale repositories) Windows新开发的一种大规模仓库管理, 视情况而定

  4. 后续所有步骤无脑下一步

3、配置

常用配置

  1. # 查看所有配置

  2. git config -l

  3. # 系统配置

  4. git config --system --list

  5. # 当前git环境配置

  6. git config --global --list

  7. # 当前仓库配置

  8. git config --local --list

  9. # 配置用户名、邮箱

  10. git config --global user.name "string"

  11. git config --global user.email XXXXX@qq.com

常用命令

1、查看

  1. # 查看 分支

  2. git branch -v # 本地

  3. git branch -a # 远程

  4. # 查看 git远程仓库

  5. git remote -v

  6. # 查看 暂存文件

  7. git status

  8. # 查看 日志

  9. git reflog

  10. git log

  11. # 显示当前仓库占用大小的命令

  12. git count-objects -vH

2、使用

基本操作
  1. # 初始化

  2. git init

  3. # 新增(绑定)远程仓库, 并创建别名

  4. git remote add <远程仓库名称(自定义)> '远程仓库地址'

  5. # 修改远程仓库名称

  6. git remote rename <old-name> <new-name>

  7. # 删除远程仓库

  8. git remote 远程主机名 --delete 远程分支名

  9. # 切换分支

  10. git checkout <分支名>

  11. # 基于当前分支 切换分支

  12. git checkout -b <分支名>

  13. # 修改本地分支名称

  14. git branch -m <old-name> <new-name>

  15. # 删除本地分支

  16. git branch -D 分支名

  17. # 删除远程分支

  18. git push <远程仓库别名> --delete <分支名>

  19. # 当前版本打tag标签

  20. git tag <name>

提交
  1. # 从远程仓库拉取最新代码

  2. git pull <远程仓库名> <远程分支名>:<本地分支名>

  3. # 添加到暂存区

  4. git add <文件名> <文件名>

  5. git add . # 全部添加

  6. # 添加到本地仓库

  7. git commit -m "备注信息" <文件名> <文件名>

  8. git commit -m "备注信息"   # 全部添加

  9. # 合并分支, 将指定分支 合并 到 当前所在 分支

  10. git merge <本地分支名>

  11. # 变基 (没有多余的合并历史的记录。多人共同协作主干分支, 最好不要使用rebase, 多用于项目组长整理commit记录)

  12. git rebase <本地分支名>

  13. git rebase --continue

  14. # 提交到远程仓库

  15. git push <远程仓库名> <本地分支名>

撤销操作
  1. ## 以下的 HEAD^ 可以写成 HEAD~1, 1代表撤回一个commit ##

  2. # 撤回至某个commit版本

  3. git reset -hard HEAD^

  4. # 撤回commit, 且 撤回 add .操作, 但是不撤销修改的代码

  5. git reset --mixed HEAD^

  6. /

  7. git reset HEAD^

  8. # 撤销 commit、不撤销git add (常用于修改commit msg信息)

  9. git reset --soft HEAD^

  10. # 撤销 commit、撤销 git add . 操作、撤销修改代码

  11. git reset --hard HEAD^

  12. # 取消文件修改

  13. git checkout -- <file>

  14. # 取消所有文件修改

  15. git checkout -- .

  16. # 取消 add

  17. git reset HEAD <file>

  18. # 取消所有暂存文件

  19. git reset

3、开发新需求

1、分支名规范
  1. 【release】:生产

  2. 【develop】:开发(西园)

  3. 【test】:测试

  4. 【feature】:按照各个功能点拉取分支,feat分支一般以禅道任务单号为单位从release分支中拉取一个新的分支。

2、新功能开发

要确保当前分支代码的纯粹性,尽量不能有其他功能需求的分支代码

  1. # 切换到 release(生产环境) 分支

  2. git checkout release

  3. # 更新本地 release 分支

  4. git pull origin release

  5. # 以 release 分支切出 feat#00001 分支 #00001为任务单号(没有单号,自己知道是哪个功能的就行)

  6. git checkout -b feat#00001

  7. #将新分支推到远程仓库

  8. git push origin feat#00001

3、提交代码,打包发布
  1. # 发生产则在release, 发测试操作develop分支

  2. # 切到develop || release

  3. git checkout develop

  4. # 拉取最新

  5. git pull origin develop

  6. # 将 功能feat#00001 分支合并到主分支,有冲突解决冲突

  7. git merge feat#00001

  8. # 将最新分支代码推到远程仓库

  9. git push origin develop

  10. # 打包代码

  11. npm run build:develop

4、打包发布
  • 看情况执行这一步, 是否有需要另切一个分支用于打包发布

  • dist没被添加到.gitignore文件中

  1. git add .

  2. git commit -m "feat: 生产环境打包" --no-verify

  3. git push origin feat#package-main --delete

  4. # git push origin ${分支名}:${打包分支}

  5. git push origin main:feat#package-main

  6. # 代码回滚到打包前的一个版本

  7. git reset --hard HEAD^

4、提交规范

1、项目安装git工具

  • husky

  • commitizen

2、规范格式

  1. <type>[optional scope]: <description>

  2. [optional body]

  3. [optional footer(s)]

  • 类型(Type) :表示提交的类型,如feat(新功能)、fix(修复)、docs(文档更新)等。

  • 范围(Scope,可选) :指定提交影响的代码模块或功能区域。

  • 描述(Description) :简短描述提交的内容。

  • 正文(Body,可选) :提供更详细的描述,如变更的原因、影响范围等。

  • 脚注(Footer,可选) :提供额外信息,如关联的issue编号、破壊性变更的标记等。

例如:

  1. git commit -m feat(login): Add login authorization feature

  2. git commit -m docs:文档更新

3、具体属性

属性 描述
feat 新功能
fix 修改bug
docs 文档修改
style 格式修改
refactor 重构
perf 性能提升
test 测试
build 构建系统
ci 对CI配置文件修改
chore 修改构建流程、或者增加依赖库、工具
revert 回滚版本

5、更新

1、在官网下载新版本,双击安装程序,窗口左下角勾选 Only show new options 选项,然后点击 Install

Git安装及使用教程

2、点击 Finish 完成更新

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...