【无标题】

创建本地Git仓库并推送到远程仓库

  • [1 创建远程仓库](#1 创建远程仓库)
  • [2 创建本地仓库](#2 创建本地仓库)
  • [3 关联本地仓库与远程仓库](#3 关联本地仓库与远程仓库)
  • [4 从远程仓库拉取代码](#4 从远程仓库拉取代码)
    • [4.1 Merge](#4.1 Merge)
    • [4.2 Rebase](#4.2 Rebase)
  • [5 提交本地代码到远程仓库](#5 提交本地代码到远程仓库)

1 创建远程仓库

例如在Gitee上:https://gitee.com/xxx/yyy.git

2 创建本地仓库

在本地源码的根目录下运行如下命令,例如:c:\yyy

git init

git add .

git status

git commit -m "The first commit to remote repo"

git status

3 关联本地仓库与远程仓库

git remote add origin https://gitee.com/xxx/yyy.git

4 从远程仓库拉取代码

git pull origin master

可能会报错:

复制代码
$ git pull origin master
remote: Enumerating objects: 5, done.
remote: Counting objects: 100% (5/5), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 5 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (5/5), 2.84 KiB | 104.00 KiB/s, done.
From https://gitee.com/qp1886358/kafka-springboot
 * branch            master     -> FETCH_HEAD
 * [new branch]      master     -> origin/master
fatal: refusing to merge unrelated histories

有两种选择解决以上问题:

4.1 Merge

运行命令:

git pull origin master --allow-unrelated-histories

如果有冲突,需要手动解决冲突,然后运行命令:

git add <conflicted_files>

git commit -m "Solved all the conflicts"

4.2 Rebase

git pull --rebase origin master

如果有冲突,需要手动解决冲突,然后运行命令:

git add <conflicted_files>

git rebase --continue

如果要退出rebase,可以运行命令:

git rebase --abort

5 提交本地代码到远程仓库

运行命令:

git push --set-upstream origin master

git push -u origin master

相关推荐
~央千澈~10 小时前
实战针对本地项目git如何移除旧仓库关联并且添加关联新仓库-优雅草卓伊凡
git
JH307317 小时前
git常用命令大全
git
ll5776443321 天前
使用PyTorch实现自定义损失函数以FocalLoss为例的详细教程
git
李少兄1 天前
Git 用户名与邮箱配置指南
git
Blue啊1 天前
code Merge(qcc)
git·gitlab
逛逛GitHub1 天前
推荐 3 个 GitHub 开源项目,让你的 Claude Code 与众不同。
github
whysqwhw1 天前
KSP 全面讲解
github
whysqwhw1 天前
KotlinPoet 详解
github
洛小豆1 天前
Git打标签仓库看不到?她说:豆子,你又忘了加 --tags!
git·后端·github
不老刘1 天前
GitHub Spec-Kit:AI 时代的规范驱动开发工具
人工智能·github·spec-kit