1. 准备工作

(1) 确保你已经安装:

  • Visual Studio (VS)(任何版本,社区版也行)

  • Git(去官网 git-scm.com 下载安装)

(2) 注册 Gitee/GitHub 账号


2. 在 VS 里创建 HTML 文件

(1) 打开 VS,创建新项目

  • 点击 "Create a new project"(新建项目)

  • 选择 "Empty Project"(空项目) 或 "HTML File"(HTML 文件)

  • 设置项目名称(如 mywebsite),选择保存位置(比如 桌面

  • 点击 "Create"(创建)

(2) 添加 HTML 文件

  • 在 Solution Explorer(解决方案资源管理器) 里右键项目 → Add → New Item(添加新项)

  • 选择 "HTML File",命名为 index.html(默认首页名)

  • 写入一些代码(比如):

<!DOCTYPE html>
<html>
<head>
    <title>我的网站</title>
</head>
<body>
    <h1>Hello World!</h1>
</body>
</html>
  • Ctrl + S 保存文件。


3. 初始化 Git 仓库

(1) 打开 VS 终端

  • 在 VS 里按 Ctrl + ~(波浪键,在 Esc 下面)打开终端。

  • 确保终端路径是你的项目文件夹(比如 ~/Desktop/mywebsite)。

(2) 初始化 Git

输入:

git init

预期反应:

Initialized empty Git repository in C:/Users/你的用户名/Desktop/mywebsite/.git/

(表示 Git 仓库初始化成功)


4. 配置 Git(第一次用才需要)

(1) 设置用户名和邮箱

在终端输入:

git config --global user.name "你的Gitee/GitHub用户名"
git config --global user.email "你的注册邮箱"

(比如):

git config --global user.name "Fang-you"
git config --global user.email "fangyou@qq.com"

检查是否设置成功:

git config --global --list

预期输出:

user.name=Fang-you
user.email=fangyou_0842@qq.com
...

5. 把文件添加到 Git

(1) 检查当前文件状态

git status

预期输出:

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        index.html

(表示 index.html 还没被 Git 跟踪)

(2) 添加文件到 Git

git add index.html

(或者添加所有文件):

git add .

再检查状态:

git status

 预期输出:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   index.html

(表示 index.html 已经准备好提交)


6. 提交更改

git commit -m "第一次提交:添加了首页HTML"

预期输出:

[master (root-commit) 1234567] 第一次提交:添加了首页HTML
 1 file changed, 11 insertions(+)
 create mode 100644 index.html

(表示提交成功)


7. 在 Gitee/GitHub 创建远程仓库

(1) 去 Gitee/GitHub 创建新仓库

  • 登录你的账号 → 新建仓库(New Repository)

  • 仓库名填 mywebsite(和本地项目名一致)

  • 不要勾选 "Initialize with README.md"(不要初始化README)(重要!否则会冲突)

  • 其他默认 → 点击 "Create"(创建)

(2) 复制仓库地址

  • 在仓库页面找到 HTTPS/SSH 地址

(比如)

https://gitee.com/你的用户名/mywebsite.git


8. 连接本地仓库和远程仓库

(1) 添加远程仓库

在终端输入:

git remote add origin https://gitee.com/你的用户名/mywebsite.git

 (origin 是远程仓库的别名,可以改成别的,但一般用这个)

(2) 检查是否连接成功

git remote -v

预期输出:

origin  https://gitee.com/你的用户名/mywebsite.git (fetch)
origin  https://gitee.com/你的用户名/mywebsite.git (push)

(表示远程仓库已正确添加)


9. 推送到远程仓库

git push -u origin master

 预期成功输出:

Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 300 bytes | 300.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://gitee.com/你的用户名/mywebsite.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

(表示推送成功)

如果失败

出现

$ git push -u origin main
error: src refspec main does not match any
error: failed to push some refs to 'https://gitee.com/fangyou666/test2.git'

则可能是

  1. 分支名称错误:你尝试推送 main 分支,但本地只有 master 分支(Gitee 默认用 master

  2. 远程仓库有冲突:远程仓库 test2.git 已经存在文件(比如创建时自动生成的 README),但你的本地仓库没有这些文件

完整解决方案

第一步:确保使用正确的分支名(master)
git branch

确认显示的是 * master(而不是 main)。Gitee 默认用 master,所以后续操作都用 master


第二步:强制同步远程仓库(推荐方案)

如果你确定远程仓库的文件不重要(或者这是个新仓库),可以用强制推送:

git push -u origin master --force

⚠️ 注意:这会 完全覆盖远程仓库,适合个人项目或确定远程内容可丢弃的情况。


第三步:如果不想强制推送(保留远程文件)
  1. 先拉取远程内容并合并:

git pull origin master --allow-unrelated-histories
  • --allow-unrelated-histories 允许合并两个独立的仓库历史

  1. 解决可能出现的冲突:

    • 如果提示冲突(CONFLICT),用 VS 打开冲突文件(如 README.md)

    • 删除冲突标记(<<<<<<<=======>>>>>>>),保留你需要的内容

    • 保存文件

  2. 提交合并结果:

git add .
git commit -m "合并远程仓库初始文件"

重新推送:

git push -u origin master

10. 检查 Gitee/GitHub

  • 刷新你的仓库页面,应该能看到 index.html 已经上传成功!

总结流程

  1. VS 创建 HTML 文件 → 保存。

  2. 终端 git init → 初始化仓库。

  3. git add . → 添加文件。

  4. git commit -m "消息" → 提交更改。

  5. Gitee/GitHub 创建空仓库(不初始化 README)。

  6. git remote add origin 仓库地址 → 连接远程。

  7. git push -u origin master → 推送代码。

这样应该能100%成功!如果还有问题,告诉我具体哪一步报错,我再帮你解决。 🚀

文章指导:没事多睡觉666

Logo

全面兼容主流 AI 模型,支持本地及云端双模式

更多推荐