git常见场景对应的命令

git环境配置参考:https://blog.csdn.net/taotiezhengfeng/article/details/150072162

1. 项目上传github

上传本机 gomoku 项目到 Git 仓库的完整步骤。

step1. 初始化 Git 仓库

bash 复制代码
#进入项目文件夹
cd /Users/userName/dev/gomoku

# git初始化(使用全局的git config配置初始化)
git init

step2. 添加文件到暂存区

bash 复制代码
git add .

step3. 提交文件

复制代码
git commit -m "初始化五子棋项目"

step4. 关联远程仓库

bash 复制代码
#选项 A:如果你要推送到 GitHub(需要先创建空仓库)
git remote add origin https://github.com/你的用户名/gomoku.git
bash 复制代码
#选项 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的个人库去,怎么搞?

bash 复制代码
# 查看全局配置(公司配置)
git config --global user.name
git config --global user.email

# 查看当前仓库配置(个人配置)
git config user.name
git config user.email

有两个方案可以实现上述目标。

方案1:为 GitHub 单独配置(推荐)

step1. 在 gomoku 目录下单独配置 Git 用户信息

bash 复制代码
cd /Users/userName/dev/gomoku

# 设置个人 GitHub 用户名和邮箱(仅当前仓库生效)
git config user.name "你的GitHub用户名"
git config user.email "你的GitHub邮箱"

step2. 初始化并提交

bash 复制代码
git init
git add .
git commit -m "初始化五子棋项目"

step3. 关联 GitHub 远程仓库

bash 复制代码
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 密钥

bash 复制代码
ssh-keygen -t ed25519 -C "你的GitHub邮箱" -f ~/.ssh/id_ed25519_github

step2. 添加 SSH 配置

编辑 ~/.ssh/config 文件,添加:

bash 复制代码
# 公司 Git
Host company
    HostName 公司git服务器地址
    User git
    IdentityFile ~/.ssh/id_rsa_company

# GitHub
Host github
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_github

step3. 添加公钥到 GitHub

bash 复制代码
cat ~/.ssh/id_ed25519_github.pub
# 复制输出内容,粘贴到 GitHub Settings -> SSH Keys

stpe4. 使用 SSH 地址推送

bash 复制代码
git remote add origin git@github:你的用户名/gomoku.git

3. 修改git推送目标库

**场景描述:**当前项目已用git init初始化完成,git config信息默认是公司的git库配置,我希望改为github的个人库配置,向github个人库推送。

只需要修改当前仓库的 Git 配置,覆盖设置个人信息即可。

step1. 在当前仓库设置个人 Git 信息(覆盖公司全局配置)

bash 复制代码
cd /Users/userName/dev/gomoku

# 设置个人 GitHub 用户名和邮箱(仅当前仓库生效,不加 --global)
git config user.name "你的GitHub用户名"
git config user.email "你的GitHub邮箱"

step2. 验证配置是否生效

bash 复制代码
# 查看当前仓库配置(应该显示你的个人配置)
git config user.name
git config user.email

# 对比全局配置(应该还是公司配置)
git config --global user.name
git config --global user.email

step3.重新提交(用个人信息)

bash 复制代码
# 修改最后一次提交的作者信息
git commit --amend --reset-author -m "初始化五子棋项目"

step4. 推送到 GitHub

bash 复制代码
# 添加 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 项目用个人邮箱,互不影响。

相关推荐
牢七1 天前
无境靶场练习一(失败)
git
HuaCode2 天前
Openclaw一键安装部署(2026年4月最新)
git·python·nodejs·openclaw·api token
小比特_蓝光2 天前
版本控制器Git/调试器gdb/cgdb使用
git
海参崴-2 天前
Git使用完全指南
git
Jp7gnUWcI2 天前
AI Compose Commit:用 AI 智能重构 Git 提交工作流
人工智能·git·重构
小柯博客2 天前
从零开始打造 OpenSTLinux 6.6 Yocto 系统 - STM32MP2(基于STM32CubeMX)(八)
c语言·git·stm32·单片机·嵌入式硬件·嵌入式·yocto
eastyuxiao2 天前
如何在不同的机器上运行多个OpenClaw实例?
人工智能·git·架构·github·php
bu_shuo2 天前
git练习学习网站【中文网站】
git·学习
秃秃然然2 天前
Git指北
git
适应规律2 天前
Git笔记
笔记·git