## Git 是什么 Git是一个开源的分布式版本控制系统,主要用于软件开发中的代码管理。它允许多个开发者在各自的分支上工作,独立提交更改,然后合并这些更改,从而促进团队协作和项目进度的跟踪。Git的设计强调性能、灵活性和安全性,使其成为当今广泛使用的版本控制系统之一。 ## 提交规范 ```Plain <type>(<scope>): <summary> │ │ │ │ │ └─> commit 简短描述 │ │ │ └─> 可选,用于说明 commit 影响的范围,比如数据层、控制层、视图层等等 │ └─> Commit 类别: feat|fix|docs|refactor|perf|test|build|ci ``` `<type>` 是用于说明 commit 的类别,可以参考使用下面这些标示 - **build**: 变更影响的是构建系统或者外部依赖 (如: gulp, npm) - **ci**: 修改了 CI 配置文件或脚本 (如: Github Action, Travis) - **chore**: 【重要】 变更不影响源代码或测试(如更新了辅助工具、库等) - **docs**: 只修改了文档 - **feat**: 【重要】 一个新特性 - **fix**: 【重要】 修复了一个 Bug - **perf**: 增强性能的代码变更 - **refactor**: 并非修复 Bug 或添加新特性的代码变更 - **revert**: 回退代码 - **style**: 变更不影响一些有意义的代码 (如: 删除空格、格式化代码、添加分号等) - **test**: 添加测试代码或修正已有的测试 ## 配置 ```Shell # 查看配置 $ git config --list # 在文本编辑器中编辑全局配置文件 $ git config --global --edit # 设置提交名称和邮箱 $ git config --global user.name <name> $ git config --global user.email <email> # 删除全局设置 $ git config --global --unset <entry-name> ``` ## 创建一个新仓库 ```Shell $ git clone [email protected]:catcto/test.git $ cd test $ git switch -c main $ touch README.md $ git add README.md $ git commit -m "add README" $ git push -u origin main ``` ## 推送现有文件夹 ```Shell $ cd existing_folder $ git init --initial-branch=main $ git remote add origin [email protected]:catcto/test.git $ git add . $ git commit -m "first commit" $ git push -u origin main ``` ## 推送现有的 Git 仓库 ```Shell $ cd existing_repo $ git remote rename origin old-origin $ git remote add origin [email protected]:catcto/test.git $ git push -u origin --all $ git push -u origin --tags ``` ## 重置远程 ```Shell # 查看提交历史 $ git log $ git show <HEAD> # 重置到某个 HEAD 并提交到远程 master 分支 $ git reset --hard <HEAD> $ git push origin HEAD --force ``` ## 配置 http 和 socks 代理 ```Shell # 查看代理 $ git config --global http.proxy $ git config --global https.proxy $ git config --global socks.proxy # 设置代理 # 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口 $ git config --global http.proxy http://127.0.0.1:1080 $ git config --global https.proxy http://127.0.0.1:1080 $ git config --global socks.proxy 127.0.0.1:1080 # 取消代理 $ git config --global --unset http.proxy $ git config --global --unset https.proxy $ git config --global --unset socks.proxy # 只对 github.com 设置代理 $ git config --global http.https://github.com.proxy socks5://127.0.0.1:1080 $ git config --global https.https://github.com.proxy socks5://127.0.0.1:1080 # 取消 github.com 代理 $ git config --global --unset http.https://github.com.proxy $ git config --global --unset https.https://github.com.proxy ``` ## 配置 SSH 协议代理 对于使用 git@ 协议的,可以配置 socks5 代理, macOS 系统编辑 `~/.ssh/config` 文件,添加这几行 ```Plain Host github.com ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p ``` ## 参考 - https://git-scm.com/book/zh/v2 - https://github.com/angular/angular/blob/main/CONTRIBUTING.md