本地Demo并且链接远程git

本文详细介绍了如何使用React官网的Demo创建本地项目,并通过Git进行版本控制和远程仓库的链接。从初始化Git仓库,设置提交身份,到处理各种Git推送时可能遇到的问题,如HTTPS和SSH链接方式,分支名修改,SSL验证,SSH密钥设置等,提供了全面的解决方案和步骤。此外,还涵盖了如何解决推送冲突和强制推送的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用react官网demo,建立一个小demo

官网Demo:2、搭建本地开发环境
如果需要了解react的demo逻辑,可以参考这篇文章(略显粗糙,笑纳)
不想搭建可以直接从主页 git clone https://github.com/indredK/RDA.git

提交三部曲

语句作用
git add .一键添加所有文件到暂存区
git commit -m “提交备注”把工作区刚添加的东西保存到本地仓库
git push (远程仓库名字) (远程仓库分支)把本地仓库的文件改动推送到远程仓库

① git add .

1、GitHub新建仓库,拷贝仓库地址,本地也新建目录,放两个不相关文件。
2、自建项目用VScode打开,然后进入终端控制面板(ctrl+~)

1、初始化git

PS H:\ApiDemo\my-app> git add .				发现不行,需要初始化git
fatal: not a git repository (or any of the parent directories): .git

解决方法:初始化git

PS H:\ApiDemo\my-app> git init
Initialized empty Git repository in H:/ApiDemo/my-app/.git/

② git commit -m “提交记录”

2、设置提交身份

PS H:\ApiDemo\my-app> git commit -m "官网React的初始代码"  		没有设置提交身份
Author identity unknown

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got '钟@DESKTOP-1FI81QO.(none)')

解决方法:设置提交身份

git config user.email "邮箱@qq.com"
git config user.email
git config user.name '我的名字'
git config user.name  

③ git push origin master = git push (远程仓库名) (分支名)

3、链接远程

PS H:\ApiDemo\my-app> git push					因为没有链接远程
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

    git remote add <name> <url>

and then push using the remote name

    git push <name>
    
PS H:\ApiDemo\my-app>  git push origin CYYY			因为没有链接远程
error: src refspec CYYY does not match any
error: failed to push some refs to 'origin'

所以要设置这个远程仓库名称为origin: 这里有可能因为你的复制出问题,

1、HTTPS链接方式

PS H:\ApiDemo\my-app> git remote add origin https://github.com/indredK/SYYY.git

2、SSH链接方式

PS H:\ApiDemo\my-app> git remote add origin git@github.com:indredK/SYYY.git
`fatal: protocol 'git@https' is not supported`

这里尽量手打,(复制问题)报错说没有这个https,可以通过两种方法(一般就算复制都不会报错了)

git remote set-url origin git@github.com:indredK/SY.git

git remote rm origin 
git remote add origin git@github.com:indredK/SY.git

3、git 方式,未尝试

4、修改分支名,要统一

PS H:\ApiDemo\my-app> git push origin master			本地一般是master,远程默认是main
error: src refspec master does not match any
error: failed to push some refs to 'git@https://github.com/indredK/RDA.git'

1、在github网页上改,请自助

2、改本地分支名

PS H:\ApiDemo\my-app> git branch -m master main
改了分支名字以后重新git add 和 commit  连接远程

5、推送报错

PS C:\Users\apple\Desktop\99> git push origin main  			因为新本地和新仓库没有统一起源
To https://github.com/indredK/SYYY.git
 ! [rejected]        main -> main (fetch first)
error: failed to push some refs to 'https://github.com/indredK/SYYY.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

强制推送

PS C:\Users\apple\Desktop\99> git push origin main --force
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 245 bytes | 245.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/indredK/SYYY.git
 + 0ac3a00...0195160 main -> main (forced update)

其他可能会遇到的问题~

随时查看配置 查看配置的一些说明

git config --list

1、SSL验证

PS C:\Users\apple\Desktop\99> git push origin main
fatal: unable to access 'https://github.com/indredK/SYYY.git/': OpenSSL SSL_read: Connection was aborted, errno 10053  

解决方法:可以取消SSL验证(或者有其他方法,自行百度)

PS C:\Users\apple\Desktop\99> git config --global http.sslVerify "false"
PS C:\Users\apple\Desktop\99> git push origin main
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 12 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 252 bytes | 252.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/indredK/SYYY.git
   0195160..488af76  main -> main

其他方法

PS H:\React官网实例-只有官网资源\react> git config --global http.proxy 127.0.0.1:1080

2、fatal: protocol ‘git@https’ is not supported

PS H:\ApiDemo\my-app> git push origin main
fatal: protocol 'git@https' is not supported
PS H:\ApiDemo\my-app> git remote add origin git@github.com:indredK/RDA.git

解决方法:链接远程的时候,复制有问题,尽量手打,或者如下解决

Linux系统中ctrl+v操作会给系统中输入特殊字符^?,偏偏这样的字符在git-bash中又看不到,所以克隆错误的链接就出现了这样报错信息。

两种方法,一般就算直接复制都没问题了
1git remote set-url origin git@github.com:indredK/SY.git
2git remote rm origin 
git remote add origin git@github.com:indredK/SY.git

3、error: failed to push some refs to ‘origin或者是url’

PS C:\Users\apple\Desktop\99> git push origin CYYY
error: src refspec CYYY does not match any
error: failed to push some refs to 'origin'

解决方法:需要设置地址和远程仓库名字

PS C:\Users\apple\Desktop\99> git push origin CYYY
error: src refspec CYYY does not match any
error: failed to push some refs to 'https://github.com/indredK/SYYY.git'

解决方法:这是因为远程仓库没有CYYY,傻了吧,主分支是main

4、本地的SSH设置key

问题描述:当新申请的git账号建立仓库,并且使用ssh进行拉取、链接本地与远程,就可能会出现。

PS H:\ApiDemo\my-app> git push origin main
The authenticity of host 'github.com (20.205.243.166)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).

Please make sure you have the correct access rights
and the repository exists.								(这里是叫你生成key)

PS H:\ApiDemo\my-app> git push origin main
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

PS H:\ApiDemo\my-app> git push origin main
To github.com:indredK/RDA.git
error: failed to push some refs to 'github.com:indredK/RDA.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法:设置key

在要上传的文件目录(有.git的文件夹),进行git bash
在这里插入图片描述
如果直接复制进去,Git设置页会弹窗提示,设置key失败

Key is invalid. You must supply a key in OpenSSH public key format

要复制key,必须使用clip < ~/.ssh/id_rsa.pub
在这里插入图片描述
在Git的网站设置好
在这里插入图片描述

5、如果你想在这个编辑器,推送当前版本给其他仓库

1、这是官网推荐

git branch -m main master
git fetch origin
git branch -u origin/master master
git remote set-head origin -a

2、我的做法:换远程链接

git remote add origin https://github.com/indredK/其他仓库名字.git

3、然后强制推送

PS H:\DvaDemo\ant-design-pro> git push origin master -force
<think>好的,用户问的是Git是否可以创建本地仓库而不连接远程仓库。我需要先确认这个问题的答案。根据我的知识,Git当然支持本地仓库而不需要远程仓库。用户可能刚开始接触Git,或者不太清楚本地远程仓库的区别。接下来我要考虑用户的潜在需求:他们可能想单独在本地管理项目,或者担心必须使用远程服务如GitHub。我需要明确说明Git本地特性,并提供创建本地仓库的具体步骤,可能还要解释为什么不需要远程仓库。另外,用户可能对如何操作感兴趣,所以需要分步骤说明,比如初始化仓库、添加文件、提交等。同时,可能存在的疑问是,如果不连接远程仓库,会不会有什么限制,这部分需要解释清楚,比如所有操作都在本地,不影响版本控制功能。还要注意使用中文,结构清晰,符合之前的系统级指令,比如行内公式用$...$,但这里可能不需要数学公式。需要检查是否有必要提及子模块或者其他高级功能,但用户的问题比较基础,可能保持简单更好。最后,可能需要补充常见问题,比如以后如何添加远程仓库,或者本地仓库的备份方法,这样用户能得到更全面的信息。</think>#### 一、核心结论 **完全可以**。Git 的本质是**分布式版本控制系统**,所有操作默认在本地完成,远程仓库(如 GitHub、GitLab)是可选项而非必选项。本地仓库独立运作时: - ✅ 完整支持版本记录、分支管理、代码回退等核心功能 - ❌ 无需联网、无需账号、无需服务器 - ⚠️ 数据仅存储在当前设备(需自行备份) --- #### 二、本地仓库创建步骤(含SourceTree图形化操作) **方式1:纯命令行创建** ```bash # 初始化新仓库 mkdir my-local-project && cd my-local-project git init # 生成.git隐藏目录 # 添加文件并提交 echo "# 本地实验项目" > README.md git add . git commit -m "初始提交" ``` **方式2:SourceTree可视化创建** 1. 打开 SourceTree,点击 **File > New/Clone** 2. 选择 **Create New Repository** 标签页 3. 关键参数配置: - **Destination Path**: 指定本地目录(如 `D:\projects\local-demo`) - **Name**: 仓库显示名称(可选) - **Type**: 选择 **Git** 4. 取消勾选 **"Create remote repository on..."**(不创建远程仓库) 5. 点击 **Create** 完成 --- #### 三、本地仓库典型使用场景 | 场景 | 操作示例 | SourceTree支持 | |-----------------------|-----------------------------------|----------------------------------------| | 独立开发实验性功能 | `git checkout -b dev` 创建新分支 | 右键分支列表 > **Create Branch** | | 代码版本回退 | `git reset --hard HEAD~2` | 右键提交记录 > **Reset Current Branch** | | 差异对比 | `git diff` | 双击文件 > **Compare with Working Copy** | | 临时存储修改 | `git stash` | 顶部工具栏 **Stash** 按钮 | --- #### 四、本地仓库文件结构解析 ``` my-local-project/ ├── .git/ # 版本控制核心(禁止手动修改) │ ├── objects/ # 数据对象存储 │ ├── refs/ # 分支和标签引用 │ └── HEAD # 当前分支指针 ├── README.md # 工作区文件 └── src/ # 项目代码目录 ``` **关键特性**: - 所有版本历史存储在 `.git` 目录 - 删除项目目录即彻底销毁仓库(无远程备份时) - 可通过 `git bundle` 导出离线备份包 --- #### 五、与远程仓库的关键差异对比 | 功能维度 | 纯本地仓库 | 含远程仓库 | |----------------|--------------------------------|--------------------------------| | **数据安全** | 依赖本地存储 | 多设备冗余备份 | | **协作支持** | 不支持多人协作 | 支持push/pull操作 | | **历史追溯** | 可完整查看所有本地提交 | 需同步后查看远程记录 | | **空间占用** | 仅存储当前分支数据 | 默认克隆所有分支历史 | --- #### 六、后续扩展建议 **若后期需关联远程仓库**: ```bash # 添加远程地址 git remote add origin https://github.com/user/repo.git # 首次推送(SourceTree中可点击"Push"按钮操作) git push -u origin main ``` **SourceTree 快捷操作**: 1. 点击 **Repository > Add Remote** 2. 输入远程仓库URL和名称(默认origin) 3. 使用 **Push** 按钮上传代码 --- #### 七、常见问题解决方案 **Q1:本地仓库如何迁移到其他电脑?** - 直接复制整个项目目录(含.git文件夹) - 或使用 `git clone file:///D:/projects/local-demo` 本地克隆 **Q2:如何避免误删本地仓库?** - 定期用 `git bundle create backup.bundle --all` 创建离线包 - 启用SourceTree的自动备份功能(**Tools > Options > Auto Backup**) **Q3:本地提交记录混乱如何清理?** ```bash # 交互式变基(SourceTree支持可视化操作) git rebase -i HEAD~5 ``` --- #### 八、性能优化技巧 1. **关闭文件监控**(大项目适用): ```bash git config core.ignoreStat true ``` 2. **使用浅层克隆**(从本地仓库克隆时): ```bash git clone --depth 1 file:///path/to/repo ``` 3. **清理历史垃圾**: ```bash git gc --auto ``` --- 通过纯本地Git仓库,开发者可以完全离线完成代码版本管理,SourceTree的图形界面进一步降低了操作门槛。建议重要项目后期仍关联远程仓库以实现数据备份和协作需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值