Git + GitHub 私人仓库完整使用指南

适用于 Windows 环境,配合 Visual Studio 2022 和 Visual Studio Code 使用。


一、安装 Git

1. 下载

前往官网下载安装包:

https://git-scm.com/download/win

选择:

64-bit Git for Windows Setup


2. 安装要点

安装过程中保持默认选项,注意以下两步:

  • Adjusting your PATH environment
    选择:
    Git from the command line and also from 3rd-party software

  • Choosing the default editor
    可以选择:
    Use Visual Studio Code as Git's default editor


3. 验证安装

安装完成后,关闭并重新打开终端(PowerShell / CMD / VS Code Terminal),执行:

git --version

看到类似:

git version 2.47.0.windows.1

即表示安装成功。


二、首次配置(只需执行一次)

# 设置用户名(填你的 GitHub 用户名)git config --global user.name "你的GitHub用户名"# 设置邮箱(填你的 GitHub 注册邮箱)git config --global user.email "你的GitHub邮箱"# 可选:设置默认分支名为 main(GitHub 默认)git config --global init.defaultBranch main# 可选:解决中文文件名显示乱码git config --global core.quotepath false

验证配置:

git config --global --list

三、在 GitHub 创建私人仓库

  1. 登录:https://github.com

  2. 点击右上角 + → New repository

  3. 填写信息:

  • Repository name:
    my-project

  • Description:
    可选填写项目描述

  • Visibility:
    选择 Private

  • 不要勾选:

    • Add a README file

    • .gitignore

    • Choose a license

  1. 点击 Create repository

  2. 复制仓库 HTTPS 地址,例如:

https://github.com/你的用户名/my-project.git

四、本地项目首次推送到 GitHub

在项目根目录打开终端,依次执行:

# 1. 初始化 Git 仓库git init# 2. 添加所有文件(.gitignore 中的文件会自动排除)git add .# 3. 检查即将提交的文件列表git status# 4. 首次提交git commit -m "初始提交"# 5. 关联远程仓库git remote add origin https://github.com/你的用户名/my-project.git# 6. 推送到 GitHubgit push -u origin main

首次推送时会弹出 GitHub 登录授权窗口,授权后后续一般无需重复登录。


五、日常开发工作流

命令行方式

# 查看当前改动git status# 查看具体改动内容git diff# 添加所有改动git add .# 或添加指定文件git add 路径/文件名# 提交git commit -m "修复了xxx问题"# 推送git push

拉取远程更新

如果另一台电脑也修改了代码:

git pull

六、在 VS Code 中使用 Git

VS Code 已内置 Git 支持。

基本操作

操作 方法
打开 Git 面板 左侧「源代码管理」或 Ctrl+Shift+G
暂存文件 文件旁点击 +
暂存全部 「更改」标题旁点击 +
提交 输入提交信息 → 点击 ✓
推送 底部状态栏同步图标
拉取 命令面板执行 Git: Pull
查看改动 点击文件进入 Diff 视图

推荐扩展

推荐安装:

  • Git Graph
    可视化查看分支和提交历史

  • GitLens
    查看作者、历史、行级追踪


七、在 Visual Studio 2022 中使用 Git

Visual Studio 2022 同样内置完整 Git 支持。


打开项目

方式一:

文件 → 打开 → 文件夹

方式二:

直接打开:

backend/ActivationApi/ActivationApi.sln

基本操作

操作 方法
Git Changes 窗口 Git → 提交或暂存
暂存文件 右键 → 暂存
暂存全部 点击“全部暂存”
提交 输入提交信息 → “全部提交”
推送 顶部 ↑ 按钮
拉取 Git → 拉取
查看历史 Git → 查看分支历史记录
切换分支 右下角分支名

注意事项

  • VS 2022 打开 .sln 后,Git 操作针对整个仓库

  • 前后端共用同一个 .git 仓库


八、两个 IDE 的协作方式

前端开发 → VS Code 打开 my-project 根目录后端开发 → VS 2022 打开 backend/ActivationApi/ActivationApi.slnGit 提交 → 任意 IDE 都可操作

建议:

  • VS Code 负责 Git 操作

  • VS 2022 专注后端开发与调试


九、常用 Git 命令速查表

基础命令

git statusgit add .git commit -m "提交信息"git pushgit pullgit log --oneline -10

撤销与回退

git checkout -- 文件名git reset HEAD 文件名git reset --soft HEAD~1git reset --hard HEAD~1

分支操作

git branchgit branch 分支名git checkout 分支名git checkout -b 分支名git merge 分支名git branch -d 分支名

远程操作

git remote -vgit remote set-url origin 新地址git push -u origin 分支名

查看信息

git diffgit diff --stagedgit log --oneline --graphgit blame 文件名

十、在新电脑上克隆项目

# 1. 克隆仓库git clone https://github.com/你的用户名/my-project.git# 2. 进入项目目录cd my-project# 3. 创建 .env 文件copy .env.example .env# 4. 安装前端依赖cd frontend/soybean-admin-mainpnpm install# 5. 恢复后端依赖cd ../../backend/ActivationApidotnet restore

十一、安全提醒

文件 是否入库 说明
.env 包含数据库密码等敏感信息
.env.example 配置模板
appsettings.Development.json 本地开发配置
node_modules/ 可重新安装
bin/ obj/ 编译产物
backend/publish/ 发布文件

敏感文件误上传处理

# 从 Git 追踪中移除git rm --cached .env# 提交git commit -m "移除敏感文件"# 推送git push

注意:

即使删除,Git 历史中仍可能存在旧内容,建议立即更换所有泄露的密码和密钥。


十二、常见问题

Q:推送时提示 rejected - non-fast-forward

git pull --rebasegit push

Q:拉取时出现合并冲突

冲突示例:

<<<<<<< HEAD你的改动=======远程改动>>>>>>> origin/main

处理步骤:

git add .git commit -m "解决合并冲突"git push

Q:忽略已被追踪的文件

git rm --cached 文件路径git commit -m "停止追踪文件"

Q:查看文件历史

git log --oneline 文件路径git show 提交哈希:文件路径

Q:VS Code 不显示 Git 信息

确认当前打开的是包含 .git 文件夹的根目录。

 

标签: none

添加新评论