大能大能无所不能 發表於 2025-12-8 21:15:00

【 Git:本地方案上传至 Gitee 仓库】

<style>pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; line-height: 1.6 !important; padding: 16px !important; margin: 16px 0 !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; tab-size: 4 !important; -moz-tab-size: 4 !important; max-width: 100% !important; box-sizing: border-box !important }
code { font-family: "Consolas", "Monaco", "Courier New", monospace !important; font-size: 14px !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; overflow-wrap: normal !important; display: inline !important; background: rgba(0, 0, 0, 0) !important; border: none !important; padding: 0 !important; margin: 0 !important; line-height: inherit !important }
pre code { background: rgba(0, 0, 0, 0) !important; border: 0 !important; border-radius: 0 !important; display: block !important; line-height: 1.6 !important; margin: 0 !important; max-width: none !important; overflow: visible !important; padding: 0 !important; white-space: pre !important; word-wrap: normal !important; word-break: normal !important; color: inherit !important }
.token.comment, .token.prolog, .token.doctype, .token.cdata { color: rgba(112, 128, 144, 1) !important; font-style: italic !important }
.token.punctuation { color: rgba(153, 153, 153, 1) !important }
.token.atrule, .token.attr-value, .token.keyword { color: rgba(0, 119, 170, 1) !important; font-weight: bold !important }
.token.function, .token.class-name { color: rgba(221, 74, 104, 1) !important; font-weight: bold !important }
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted { color: rgba(102, 153, 0, 1) !important }
.token.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: rgba(153, 0, 85, 1) !important }
.cnblogs-markdown pre, .cnblogs-post-body pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; background-color: rgba(248, 248, 248, 1) !important; border: 1px solid rgba(225, 228, 232, 1) !important; border-radius: 6px !important; padding: 16px !important; margin: 16px 0 !important }
pre, pre, pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important }</style>
      <div class="markdown_views prism-atom-one-dark" id="content_views"><svg style="display: none" xmlns="http://www.w3.org/2000/svg"><path d="M5,0 0,2.5 5,5z" id="raphael-marker-block" stroke-linecap="round" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0)"></path></svg><h3>介绍</h3><p>本教程提供了一套完整、清晰的操作流程,帮助开发者将本地已有的项目快速上传至 Gitee(码云)仓库,适用于初次使用 Gitee 进行版本控制的用户,也可作为日常操作参考手册。</p><p>通过本教程,你将掌握从 Gitee 仓库创建、本地仓库配置到代码推送的全流程,解决上传过程中常见的冲突、版本不兼容等问题,高效实现本地项目的云端管理。</p><h3>前提条件</h3><ol><li>本地电脑已安装 Git 客户端(可从 Git 官网 下载)。</li><li>已在 Gitee 平台注册账户(注册地址:Gitee 官网)。</li><li>本地已完成项目开发(确保项目文件结构完整)。</li></ol><h3>操作步骤</h3><h4>1. Gitee 端:创建远程仓库</h4><ol><li>登录 Gitee 账户,点击个人头像旁的「+」号,选择「新建仓库」。</li><li>填写仓库基本信息:
<ul><li>仓库名称:填写与本地项目对应的名称(如 <code>Java-Excel-Compare</code>)。</li><li>归属:选择个人账号或所属组织。</li><li>路径:自动生成(可按需修改,建议与仓库名称一致)。</li><li>开源类型:选择「开源」「私有」或「企业内部开源」。</li></ul></li><li>可选配置(按需勾选):
<ul><li>初始化仓库:可选择添加 <code>.gitignore</code>(按项目语言选择,如 Java)、开源许可证(如 MulanPSL-2.0)。</li><li>设置模板:添加 README、Issue 等模板文件。</li><li>分支模型:默认选择「单分支模型(仅创建 master 分支)」。</li></ul></li><li>点击「创建」,完成远程仓库创建,获取仓库地址(HTTPS 格式,如 <code>https://gitee.com/用户名/仓库名.git</code>)。</li></ol><h4>2. 本地端:配置并上传项目</h4><ol><li><p>打开本地项目文件夹,右键点击「Git Bash Here」,打开 Git 命令行工具。</p></li><li><p>初始化本地仓库,将当前目录转为 Git 可管理的仓库:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> init</code></pre> <p>执行后会生成隐藏的</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash">.git</code></pre> <p>文件夹(需开启「显示隐藏文件」才能查看)。</p></li><li><p>绑定本地仓库与 Gitee 远程仓库(这里替换为你的仓库地址):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> remote <span class="token function">add</span> origin https://gitee.com/用户名/仓库名.git</code></pre> </li><li><p>将本地项目所有文件添加到 Git 暂存区:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span></code></pre> <p>需要注意:. 表示当前目录下所有文件,前面需保留空格。</p></li><li><p>提交暂存区文件到本地仓库,填写提交说明(描述本次上传内容):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> commit -m <span class="token string">"项目名即可"</span></code></pre> </li><li><p>同步远程仓库与本地仓库:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> pull origin master --allow-unrelated-histories</code></pre> <p>若直接执行 <code>git pull origin master</code> 报错「fatal: refusing to merge unrelated histories」,需添加 <code>--allow-unrelated-histories</code> 参数,解决本地与远程仓库无相关性的问题。</p></li><li><p>推送本地仓库文件到 Gitee 远程仓库:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> push origin master</code></pre> </li></ol><h4>3. 验证上传结果</h4><p>登录 Gitee 账户,进入创建的远程仓库,查看文件列表。若能看到本地项目的所有文件(如 <code>src</code> 目录、配置文件等),说明上传成功。</p><h3>常见问题解决</h3><h4>1. 拉取时出现合并冲突(如 README.md 冲突)</h4><ul><li><p>原因:本地与远程仓库的同一文件存在不同内容,比如更新了本地的README.md,但是远程的内容没有更新。</p></li><li><p>解决:</p><ul><li><strong>在本地删除 <code>README.md</code> 并提交</strong></li></ul> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token comment"># 删除本地的 README.md 文件</span>
<span class="token function">git</span> <span class="token function">rm</span> README.md
<span class="token comment"># 提交删除操作</span>
<span class="token function">git</span> commit -m <span class="token string">"这里可以写项目名,也可以标注其他的,比如:删除本地 README.md,准备上传新文件"</span></code></pre>
<ul><li><strong>将新的 <code>README.md</code> 复制到本地仓库并提交</strong></li></ul><p>将你本地准备好的新 <code>README.md</code> 文件复制到项目根目录,然后执行</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token comment"># 添加新的README.md 到暂存区</span>
<span class="token function">git</span> <span class="token function">add</span> README.md
<span class="token comment"># 提交新文件</span>
<span class="token function">git</span> commit -m <span class="token string">"这里可以写项目名,也可以标注其他的"</span></code></pre>
<ul><li><strong>拉取远程最新更新(避免冲突)</strong></li></ul><p>为了确保远程没有新的更改(防止后续推送冲突),先拉取一次:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> pull origin master</code></pre> <p>如果出现冲突,按照提示解决后再提交。</p><ul><li><strong>推送本地更改到远程</strong></li></ul> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> push origin master</code></pre> </li></ul><h4>2. 推送时提示「RPC failed; HTTP 408」</h4><ul><li>原因:项目文件过大,网络传输超时。</li><li>解决:使用 Git LFS 跟踪大文件,或压缩项目后重新上传,具体操作可参考 Git LFS 官方文档。</li></ul><h4>3. 绑定远程仓库时提示「fatal: remote origin already exists」</h4><ul><li>原因:本地仓库已绑定过其他远程地址。</li><li>解决:先删除原有绑定,再重新绑定新仓库地址:<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="prism language-bash"><span class="token function">git</span> remote <span class="token function">rm</span> origin
<span class="token function">git</span> remote <span class="token function">add</span> origin https://gitee.com/用户名/仓库名.git</code></pre> </li></ul><h3>参考资料</h3><ul><li>Git 官方教程:Git - Book</li><li>菜鸟教程 Git 入门:Git 教程</li><li>Gitee 官方帮助文档:Gitee 帮助中心</li></ul></div><br><br>
来源:https://www.cnblogs.com/clnchanpin/p/19323697
頁: [1]
查看完整版本: 【 Git:本地方案上传至 Gitee 仓库】