git环境配置参考:https://blog.csdn.net/taotiezhengfeng/article/details/150072162
1. 项目上传github
上传本机 gomoku 项目到 Git 仓库的完整步骤。
step1. 初始化 Git 仓库
#进入项目文件夹 cd /Users/userName/dev/gomoku # git初始化(使用全局的git config配置初始化) git initstep2. 添加文件到暂存区
git add .step3. 提交文件
git commit -m "初始化五子棋项目"step4. 关联远程仓库
#选项 A:如果你要推送到 GitHub(需要先创建空仓库) git remote add origin https://github.com/你的用户名/gomoku.git#选项 B:如果你要推送到已有的远程仓库 git remote add origin <远程仓库地址>step5. 推送到远程
git branch -M main git push -u origin main常用检查命令:
| 命令 | 作用 |
|---|---|
git status | 查看当前状态 |
git log --oneline | 查看提交历史 |
git remote -v | 查看已关联的远程仓库 |
2. 有多个git目标库
场景描述:本机git库全局配置是公司git库,想将个人项目推送到github的个人库去,怎么搞?
# 查看全局配置(公司配置) git config --global user.name git config --global user.email # 查看当前仓库配置(个人配置) git config user.name git config user.email有两个方案可以实现上述目标。
方案1:为 GitHub 单独配置(推荐)
step1. 在 gomoku 目录下单独配置 Git 用户信息
cd /Users/userName/dev/gomoku # 设置个人 GitHub 用户名和邮箱(仅当前仓库生效) git config user.name "你的GitHub用户名" git config user.email "你的GitHub邮箱"step2. 初始化并提交
git init git add . git commit -m "初始化五子棋项目"step3. 关联 GitHub 远程仓库
git remote add origin https://github.com/你的用户名/gomoku.git git branch -M main git push -u origin main方案 2:使用 SSH 密钥区分
为 GitHub 单独配置 SSH 密钥,避免与公司仓库冲突:
step1. 生成 GitHub 专用 SSH 密钥
ssh-keygen -t ed25519 -C "你的GitHub邮箱" -f ~/.ssh/id_ed25519_githubstep2. 添加 SSH 配置
编辑~/.ssh/config文件,添加:
# 公司 Git Host company HostName 公司git服务器地址 User git IdentityFile ~/.ssh/id_rsa_company # GitHub Host github HostName github.com User git IdentityFile ~/.ssh/id_ed25519_githubstep3. 添加公钥到 GitHub
cat ~/.ssh/id_ed25519_github.pub # 复制输出内容,粘贴到 GitHub Settings -> SSH Keysstpe4. 使用 SSH 地址推送
git remote add origin git@github:你的用户名/gomoku.git3. 修改git推送目标库
场景描述:当前项目已用git init初始化完成,git config信息默认是公司的git库配置,我希望改为github的个人库配置,向github个人库推送。
只需要修改当前仓库的 Git 配置,覆盖设置个人信息即可。
step1. 在当前仓库设置个人 Git 信息(覆盖公司全局配置)
cd /Users/userName/dev/gomoku # 设置个人 GitHub 用户名和邮箱(仅当前仓库生效,不加 --global) git config user.name "你的GitHub用户名" git config user.email "你的GitHub邮箱"step2. 验证配置是否生效
# 查看当前仓库配置(应该显示你的个人配置) git config user.name git config user.email # 对比全局配置(应该还是公司配置) git config --global user.name git config --global user.emailstep3.重新提交(用个人信息)
# 修改最后一次提交的作者信息 git commit --amend --reset-author -m "初始化五子棋项目"step4. 推送到 GitHub
# 添加 GitHub 远程仓库 git remote add origin https://github.com/你的用户名/gomoku.git # 推送 git branch -M main git push -u origin main关键命令:
| 命令 | 作用范围 |
|---|---|
git config --global xxx | 全局(所有仓库) |
git config xxx | 仅当前仓库 |
这样你的公司项目继续用公司邮箱,gomoku 项目用个人邮箱,互不影响。