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 项目用个人邮箱,互不影响。

相关推荐
乐之者v4 小时前
Intellij Idea修改Git远程地址
git
北寻北爱6 小时前
面试题-git+npm
vue.js·git·webpack·echarts
EleganceJiaBao7 小时前
【Git】使用 SSH 进行 Git 操作的完整步骤
git·ssh·github·push·clone
ChoSeitaku7 小时前
Git的安装|创建仓库|配置Git|添加文件|修改文件|版本回退|撤销修改|删除文件
大数据·git·elasticsearch
zoujiahui_20189 小时前
git中的连接测试
git
Real-Staok10 小时前
Git 积累 & 常用命令
git·gitee·github
MonkeyKing_sunyuhua12 小时前
Mac 上从 0 到 1 安装 Git + 配置 GitHub SSH + 拉取 private 仓库 的完整步骤
git·macos·github
Wpa.wk21 小时前
Git日志+分支管理+基础冲突解决
经验分享·git·测试工具
香水5只用六神1 天前
【DMA】存储器到外设模式实验2
c语言·git·stm32·单片机·嵌入式硬件·github·visual studio