Git 代码备忘录

本文档详细说明如何初始化本地 Git 仓库、配置 SSH 密钥、关联远程仓库(以 GitLab 为例),以及日常代码推送和分支管理流程。适用于团队协作开发场景。


1. 初始化本地 Git 仓库

# 初始化仓库
git init

# 全局禁用 SSL 验证(避免证书问题,按需使用)
git config --global http.sslverify false

2. 配置 SSH 密钥

2.1 单账户使用

生成密钥对:

ssh-keygen -t rsa -b 4096 -C "qinghao@procoding.cn"
  • 连续按两次回车(跳过密码设置,默认生成路径 ~/.ssh/
  • 获取公钥内容:
cat ~/.ssh/id_rsa.pub

2.2 多账户使用

生成密钥对:

ssh-keygen -t rsa -b 4096 -C "qinghao@google.com" -f "~\.ssh\id_rsa_google"
ssh-keygen -t rsa -b 4096 -C "qinghao@procoding.cn" -f "~\.ssh\id_rsa_procoding"

在项目登陆时不要使用 –global 参数

git config user.name "qinghao"
git config user.email "qinghao@procoding.cn"

配置~\.ssh\config文件

# procoding.cn 账户配置
Host git-procoding
  HostName git.procoding.cn
  User git
  IdentityFile ~/.ssh/id_rsa_procoding

# google.com 账户配置
Host git-google
  HostName google
  User git
  IdentityFile ~/.ssh/id_rsa_google

添加密钥到 GitLab

  1. 登录 GitLab → 设置(Settings)→ SSH Keys
  2. 粘贴复制的公钥内容

3. 关联远程仓库

# 添加远程仓库地址(单用户示例)
git remote add origin ssh://git@git.procoding.cn/xxxx.git

# 添加远程仓库地址(多用户示例)
git remote add origin git@git-google:xxxx.git
git remote add origin git@git-procoding:xxxx.git

# 验证远程仓库信息
git remote show origin

4. 分支操作

# 查看全部分支(本地+远程)
git branch -a

# 首次创建并切换到 dev 分支
git checkout -b dev

# 后续切换分支(省略 -b 参数)
git checkout dev

5. 代码同步流程

拉取远程代码(更新本地)

git pull origin dev

推送本地代码到远程

# 添加所有修改文件
git add .

# 提交变更(附说明)
git commit -m "fix: 修复用户登录逻辑"

# 推送到远程 dev 分支
git push origin dev

6. 分支合并

# 切换到目标分支(如 release)
git checkout release

# 合并 dev 分支到当前分支
git merge dev

7. 命名规范

  • dev:日常开发分支
  • release:预发布/部署分支
  • main/master:生产稳定分支(勿直接修改)

8. 其他常用命令

# 查看远程仓库 URL
git remote -v

# 强制推送(覆盖远程,慎用!)
git push -f origin dev

9.常用gitignore文件

Python

# 忽略 Python 编译文件和缓存目录
__pycache__/
*.py[cod]
*.so
*.pyc

# 忽略 PyArmor 相关目录和文件
.pyarmor/
*.reg
*.zip
*.enc

# 忽略 IDE 生成的文件
.idea/
.vscode/
*.iml
*.sublime*
*.swp

# 忽略构建产物和临时文件
bin/
obj/
Build/
dist/
*.exe
*.dll
*.pyd
*.pdb
*.log
*.tmp
*.cache
*.DS_Store
Thumbs.db
Temporary/

# 忽略测试和项目特定文件
TestResults/
*.zip
*.config
*.ps1
*.psd1

# 忽略环境和全局配置
venv/
/Global/
*.env

# 其他忽略规则
/.vs/
*.suo
*.ntvs*
*.njsproj
*.sln
*.xcworkspace
*.xcproject
使用Nginx搭建DockerHub中转节点 使用TC(Traffic Control)对内外网分流限速 在 Linux 中挂载新虚拟化硬盘的完整指南
View Comments
There are currently no comments.