亚礼德 發表於 2025-6-7 19:30:00

完整教程:从零开始,学会上传,更新,维护github仓库

<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 }
p code, li code, td code, h1 code, h2 code, h3 code, h4 code, h5 code, h6 code { background-color: rgba(27, 31, 35, 0.05) !important; border-radius: 3px !important; font-size: 85% !important; margin: 0 !important; padding: 0.2em 0.4em !important; white-space: nowrap !important; font-family: "Consolas", "Monaco", "Courier New", monospace !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.property, .token.tag, .token.boolean, .token.number, .token.constant, .token.symbol, .token.deleted { color: rgba(153, 0, 85, 1) !important }
.token.selector, .token.attr-name, .token.string, .token.char, .token.builtin, .token.inserted { color: rgba(102, 153, 0, 1) !important }
.token.operator, .token.entity, .token.url, .language-css .token.string, .style .token.string { color: rgba(154, 110, 58, 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.regex, .token.important, .token.variable { color: rgba(238, 153, 0, 1) !important }
.language-javascript .token.keyword { color: rgba(0, 0, 255, 1) !important; font-weight: bold !important }
.language-javascript .token.string { color: rgba(0, 128, 0, 1) !important }
.language-javascript .token.number { color: rgba(0, 102, 204, 1) !important }
.language-javascript .token.operator { color: rgba(102, 102, 102, 1) !important }
.language-javascript .token.function { color: rgba(121, 85, 72, 1) !important; font-weight: bold !important }
.language-javascript .token.class-name { color: rgba(46, 125, 50, 1) !important; font-weight: bold !important }
.language-bash .token.function { color: rgba(0, 102, 204, 1) !important }
.language-bash .token.string { color: rgba(0, 128, 0, 1) !important }
.language-bash .token.keyword { color: rgba(0, 0, 255, 1) !important; font-weight: bold !important }
.token { display: inline !important; white-space: inherit !important }
.pre-numbering, .hljs-button, .opt-box, .hide-preCode-box { display: none !important }
.markdown_views pre, #content_views pre, .htmledit_views pre, .prettyprint, .hljs { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; font-family: "Consolas", "Monaco", "Courier New", monospace !important }
.language-javascript, .language-java, .language-python, .language-bash, .language-css, .language-html { white-space: pre !important; word-wrap: normal !important; font-family: "Consolas", "Monaco", "Courier New", monospace !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 }
pre, pre * { box-sizing: border-box !important }
@media (max-width: 768px) { pre { font-size: 12px !important; padding: 12px !important; overflow-x: auto !important } }</style> <div id="content_views" class="htmledit_views atom-one-dark"><p>以下是一份从头到尾、覆盖安装、配置、创建仓库、上传项目到 GitHub 的完整教程。全程使用通用示例,不具备任何具体的仓库链接,仅供参考。</p><hr><h3>一、准备工作</h3><h4>1. 注册 GitHub 账号</h4><ol><li><p>打开浏览器,访问 GitHub官网(输入 “GitHub” 即可找到)。</p></li><li><p>点击“Sign up”或“注册”按钮,按提示输入邮箱、用户名、密码,并搞定邮箱验证。</p></li><li><p>登陆后,进入个人主页,即可进行后续操作。</p></li></ol><hr><h3>二、安装并安装 Git</h3><h4>1. 在不同操作系统上安装 Git</h4><ul><li><p><strong>Windows:</strong></p><ol><li><p>打开浏览器,搜索“Git<span class="words-blog hl-git-1" data-tit="Windows" data-pretit="windows">Windows</span>安装”并找到官网的下载页面。</p></li><li><p>下载最新版本的安装包(通常是以<code>.exe</code>结尾的资料),双击运行后一路“Next”结束安装。</p></li><li><p>安装过程中保持默认设置即可(除非你有特殊需求)。</p></li></ol></li><li><p><strong>macOS:</strong></p><ol><li><p>可以通过包管理器安装(推荐 Homebrew)。在终端中执行:</p> <pre data-index="0" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-bash">/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"brew install git</code></pre> <p>假如不想使用 Homebrew,也可以直接下载<span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;macOS\&quot;}&quot;}" data-tit="macOS" data-pretit="macos">macOS</span>安装包,按照提示安装。</p></li></ol></li><li><p><strong>Linux(以 Ubuntu 为例):</strong><br>打开终端,执行:</p> <pre data-index="1" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-sql">sudo apt updatesudo apt install git -y</code></pre> </li></ul><p>安装达成后,在任意终端(Windows 下可打开 Git Bash)里输入:</p> <pre data-index="2" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-sql">git --version</code></pre> <p>如果输出类似<code>git version 2.x.x</code>,说明安装成功。</p><h4>2. 全局配置 Git 用户信息</h4><p>安装完成后,需要告诉 Git 是谁在提交代码。打开终端,依次执行:</p> <pre data-index="3" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-csharp">git config --global user.name "你的用户名"git config --global user.email "你的邮箱@example.com"</code></pre> <ul><li><p><code>user.name</code>:填写你在 GitHub 上注册的用户名或昵称。</p></li><li><p><code>user.email</code>:填写与你 GitHub 帐号绑定的邮箱地址。</p></li></ul><p>配置完毕后,许可用下面命令检查:</p> <pre data-index="4" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-scss">git config --global --list</code></pre> <p>会看到类似:</p> <pre data-index="5" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">user.name=你的用户名user.email=你的邮箱@example.com</code></pre> <hr><h3>三、为安全验证配置 SSH(可选,但推荐)</h3><p>使用 SSH 方式推送/拉取代码,可以避免每次输入用户名和密码(Token)。下面介绍 SSH 公钥的生成与添加。</p><h4>1. 生成 SSH 密钥对</h4><ol><li><p>打开终端(或 Git Bash),执行:</p> <pre data-index="6" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-perl">ssh-keygen -t ed25519 -C "你的邮箱@example.com"</code></pre> <ul><li><p>按提示一路回车即可,默认会在用户主目录下生成<code>.ssh/id_ed25519</code>(私钥)和 <code>.ssh/id_ed25519.pub</code>(公钥)两个文件。</p></li><li><p>如果提示 “文件已存在”,可选择覆盖或另存为其他文件名,也能够按提示输入一个新的名称。</p></li></ul></li><li><p>生成完成后,执行以下命令查看公钥内容:</p> <pre data-index="7" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">cat ~/.ssh/id_ed25519.pub</code></pre> <p>终端会输出一串以<code>ssh-ed25519</code>开头的字符串,这就是公钥。</p></li></ol><h4>2. 将 SSH 公钥添加到 GitHub</h4><ol><li><p>在浏览器中,登陆 GitHub,点击右上角头像,选择“Settings”(设置)。</p></li><li><p>在左侧栏中找到<strong>SSH and GPG keys</strong>,点击进入。</p></li><li><p>点击 <strong>New SSH key</strong>(新增 SSH 密钥),在“Title”框里可以填写一个有意义的名字(例如 “My Laptop”),然后把刚才复制的公钥粘贴到下面的文本框中,最后点击<strong>Add SSH key</strong>。</p></li><li><p>如果 GitHub 要求你输入账号密码或两步验证码,按提示输入即可。</p></li></ol><p>添加成功后,SSH 验证就设置完毕。行测试连接:</p> <pre data-index="8" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-typescript">ssh -T git@github.com</code></pre> <p>要是提示类似 “Hi &lt;用户名&gt;! You've successfully authenticated...” 则说明 SSH 验证配置正确。</p><hr><h3>四、在 GitHub 上创建一个空仓库</h3><ol><li><p>登陆 GitHub 后,点击右上角的 “+” 按钮,选择<strong>New repository</strong>(新建仓库)。</p></li><li><p>在 “Create a new repository” 页面填写:</p><ul><li><p><strong>Repository name</strong>:为你的项目起个名字,例如<code>my-project</code>。</p></li><li><p><strong>Description</strong>(可选):添加简短描述。</p></li><li><p><strong>Public</strong> 或 <strong>Private</strong>:根据应该选择公开或私有。</p></li><li><p><strong>Initialize this repository with a README</strong>:通常<strong>不勾选</strong>,否则会自动生成一个<span class="words-blog hl-git-1" data-report-view="{&quot;spm&quot;:&quot;1001.2101.3001.10283&quot;,&quot;extra&quot;:&quot;{\&quot;words\&quot;:\&quot;README\&quot;}&quot;}" data-tit="README" data-pretit="readme">README</span>文件,后续需要先拉取再推送。</p></li></ul></li><li><p>点击下方的 <strong>Create repository</strong>(创建仓库),稍等片刻后就会进入新仓库页面。页面上会给出 HTTPS 和 SSH 两种地址,如:</p><ul><li><p>HTTPS:<code>https://github.com/你的用户名/my-project.git</code></p></li><li><p>SSH: <code>git@github.com:你的用户名/my-project.git</code><br>记住这两个地址中的一种,后续用来关联本地仓库。</p></li></ul></li></ol><hr><h3>五、将本地工程与远程仓库关联并上传</h3><p>下面以一个已存在本地计划的目录为例,演示如何上传到 GitHub。假设本地任务文件夹路径为<code>~/projects/my-project</code>,且目录下已有若干源代码或资源文件。</p><h4>1. 在本地初始化 Git 仓库</h4><ol><li><p>打开终端,切换到项目根目录:</p> <pre data-index="9" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-typescript">cd ~/projects/my-project</code></pre> </li><li><p>在该目录下执行:</p> <pre data-index="10" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-csharp">git init</code></pre> <p>这会创建一个隐藏的<code>.git/</code>文件夹,使该目录成为一个 Git 仓库。</p></li><li><p>你可以运行 <code>ls -A</code> 或 <code>git status</code>来确认目录下已有普通文件,但 Git 还没追踪它们。</p></li></ol><h4>2. 将档案添加到暂存区并提交</h4><ol><li><p><strong>查看当前状态</strong>(可选):</p> <pre data-index="11" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-lua">git status</code></pre> <p>这时 Git 会告诉你哪些文件是 “Untracked files”(未跟踪文件)。</p></li><li><p><strong>把所有文件添加到暂存区</strong>:</p> <pre data-index="12" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-csharp">git add .</code></pre> <p>这样做会把当前目录(及子目录)下所有未被忽略的记录都纳入暂存区。</p></li><li><p><strong>创建第一次提交</strong>:</p> <pre data-index="13" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-sql">git commit -m "首次提交:计划初始化"</code></pre> <p>以上操作会在本地生成一个提交记录(commit),大多数情况下会默认在<code>master</code>分支上。如果想直接把主分支命名为<code>main</code>,可以执行:</p> <pre data-index="14" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-less">git branch -M main</code></pre> <p>这样以后本地仓库的主分支就是<code>main</code>(与 GitHub 通用约定保持一致)。</p></li></ol><h4>3. 将本地仓库与远程仓库关联</h4><ol><li><p>在本地执行下面命令,将远程仓库地址添加为名为<code>origin</code> 的远程源:</p><ul><li><p><strong>使用 HTTPS(得每次输入用户名/密码或 Token):</strong></p> <pre data-index="15" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">git remote add origin https://github.com/你的用户名/my-project.git</code></pre> </li><li><p><strong>使用 SSH(已配置 SSH 密钥则无需输入密码):</strong></p> <pre data-index="16" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-perl">git remote add origin git@github.com:你的用户名/my-project.git</code></pre> </li></ul><p>注意:命令开头务必是小写的<code>git</code>,不要带任何隐藏字符或破折号。</p></li><li><p>添加成功后,可用下面命令确认:</p> <pre data-index="17" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-undefined">git remote -v</code></pre> <p>你会看到类似:</p> <pre data-index="18" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">origin https://github.com/你的用户名/my-project.git (fetch)origin https://github.com/你的用户名/my-project.git (push)</code></pre> <p>或者如果用的是 SSH,则显示:</p> <pre data-index="19" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-perl">origin git@github.com:你的用户名/my-project.git (fetch)origin git@github.com:你的用户名/my-project.git (push)</code></pre> </li></ol><h4>4. 推送本地提交到远程仓库</h4><ol><li><p>假设当前分支是<code>main</code>:</p> <pre data-index="20" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-less">git push -u origin main</code></pre> <p>如果当前分支是<code>master</code>(没有重命名为<code>main</code>),则:</p> <pre data-index="21" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-perl">git push -u origin master</code></pre> <ul><li><p><code>-u origin main</code>:把本地就是的含义<code>main</code>分支推送到远程<code>origin</code>,并建立跟踪关系。以后只需直接执行<code>git push</code> 即可。</p></li></ul></li><li><p>在推送过程中,如果使用 HTTPS,需要输入 GitHub 用户名和密码(如果启用了两步验证,则输入 Personal Access Token);倘若使用 SSH,且 SSH key 已配置好,则不会再提示输入密码。</p></li><li><p>推送成功后,终端会显示类似:</p> <pre data-index="22" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">Enumerating objects: 10, done.Counting objects: 100% (10/10), done.Compressing objects: 100% (7/7), done.Writing objects: 100% (10/10), 1.23 KiB | 1.23 MiB/s, done.Total 10 (delta 0), reused 0 (delta 0)To https://github.com/你的用户名/my-project.git * main -&gt; mainbranch 'main' set up to track 'origin/main'.</code></pre> </li><li><p>刷新浏览器,打开 GitHub 上该仓库页面,就能看到你刚才上传的所有档案。</p></li></ol><hr><h3>六、后续开发与同步</h3><p>项目上传成功后,后续只需按照下面步骤更新远程仓库即可。</p><h4>1. 修改/新增材料后上传</h4><ol><li><p>在本地对档案进行修改或新增文件。</p></li><li><p>查看计划状态:</p> <pre data-index="23" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-lua">git status</code></pre> <p>可能看到哪些文件被修改、哪些是新文件。</p></li><li><p>添加改动到暂存区:</p><ul><li><p>如要一次添加所有改动:</p> <pre data-index="24" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-csharp">git add .</code></pre> </li><li><p>如果只想添加某个文件:</p> <pre data-index="25" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">git add path/to/yourfile.ext</code></pre> </li></ul></li><li><p>提交改动:</p> <pre data-index="26" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-sql">git commit -m "本次更新:简要说明修改内容"</code></pre> </li><li><p>推送到远程:</p> <pre data-index="27" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-perl">git push</code></pre> <p>由于第一次已经用<code>-u origin main</code> 或 <code>-u origin master</code>与远程分支建立了跟踪关系,后续直接<code>git push</code> 即可。</p></li></ol><h4>2. 如果远程仓库有了新的提交(多人协作时常见)</h4><ol><li><p>在推送之前,可以先拉取远程更新:</p> <pre data-index="28" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-css">git pull --rebase origin main</code></pre> <p>或者:</p> <pre data-index="29" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-less">git pull origin main</code></pre> <ul><li><p>带 <code>--rebase</code>可以保持提交历史线性;不带则会自动创建一个合并提交(Merge commit)。</p></li></ul></li><li><p>如果出现冲突(Conflict),按提示打开冲突文件,手动保留、删除冲突标记(<code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>、<code>=======</code>、<code>&gt;&gt;&gt;&gt;&gt;&gt;&gt;</code>),然后执行:</p> <pre data-index="30" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-csharp">git add 冲突已解决的文件git rebase --continue # 如果使用了 --rebase</code></pre> <p>或者如果是普通<code>git pull</code>:</p> <pre data-index="31" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-sql">git add 冲突已处理的文件git commit</code></pre> <p>冲突解决完成后再执行<code>git push</code>,即可把本地改动与远程最新内容同步。</p></li></ol><hr><h3>七、常见问题与注意事项</h3><ol><li><p><strong>命令前不要出现隐藏字符</strong></p><ul><li><p>如果复制粘贴时前面多了不可见破折号(如 “–”)或空格,Git 会识别为非法命令,从而提示 “command not found”。遇到这种情况,请按几次退格键把前导的隐藏字符清除,然后再手动输入<code>git init</code>、<code>git add</code> 等命令。</p></li></ul></li><li><p><strong>.gitignore 文件</strong></p><ul><li><p>在工程根目录创建一个名为<code>.gitignore</code>的文件,写入你不想跟踪(即不想上传到远程)的文件或文件夹规则。常见示例:</p> <pre data-index="32" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">node_modules/*.log.env.DS_Storebuild/*.pyc</code></pre> </li><li><p>这样做可以避免把编译生成的临时文件、IDE 配置、依赖包等无关文件上传到仓库。</p></li></ul></li><li><p><strong>分支管理</strong></p><ul><li><p>默认情况下,你会在<code>main</code>(或 <code>master</code>)分支上工作。要是想开发新功能、修复 Bug,通常会新建分支:</p> <pre data-index="33" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-css">git checkout -b feature/新功能名称</code></pre> </li><li><p>完成开发并测试通过后,切回<code>main</code>分支并合并:</p> <pre data-index="34" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-cobol">git checkout maingit merge feature/新能力名称git branch -d feature/新功能名称</code></pre> </li><li><p>合并完成后,再执行<code>git push</code>即可把合并后的结果推到远程。</p></li></ul></li><li><p><strong>SSH 与 HTTPS</strong></p><ul><li><p>倘若长时间需要频繁推送,推荐使用 SSH 方式,只需在本地生成过一次 SSH 密钥,并把公钥添加到 GitHub,就不需要每次输入用户名/密码或 Token。</p></li><li><p>如果安全要求较高或不方便配置 SSH,也可以继续使用 HTTPS,但每次推送时都可能需要输入 Token。</p></li></ul></li><li><p><strong>Token 和安全</strong></p><ul><li><p>GitHub 已逐步弃用直接使用密码进行 HTTPS 验证,强制使用个人访问令牌(Personal Access Token)。</p></li><li><p>在终端推送时,如果提示要求用户名和密码,你可以把用户名填为 GitHub 用户名,然后把个人访问令牌粘贴到密码处。访问令牌允许在 GitHub 个人设置里新建。</p></li></ul></li><li><p><strong>远程仓库已初始化 README 情况</strong></p><ul><li><p>如果你在新建仓库时勾选了“初始化 README”,远程仓库就会自带一个提交记录。这时本地一开始是空的,假如你直接执行<code>git push</code>会被拒绝(提示需先拉取远程更改)。解决方法是先执行:</p> <pre data-index="35" class="new-version hljs set-code-show" name="code" style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="hljs language-css">git pull --rebase origin main</code></pre> <p>把远程的那次提交拉下来并合并到本地,然后再<code>git push</code>。</p></li></ul></li></ol><hr><h3>八、小结</h3><ol><li><p><strong>安装 Git</strong> 并配置 <code>user.name</code>、<code>user.email</code>。</p></li><li><p><strong>(可选)配置 SSH 密钥</strong>,将公钥添加到 GitHub。</p></li><li><p><strong>在 GitHub 上创建一个空仓库</strong>(不勾选初始化 README)。</p></li><li><p><strong>在本地项目目录执行<code>git init</code></strong>,将计划转为 Git 仓库。</p></li><li><p><strong>执行 <code>git add .</code>、<code>git commit -m "描述"</code></strong>,完毕首次提交。</p></li><li><p><strong>使用 <code>git remote add origin &lt;远程仓库地址&gt;</code></strong> 关联远程。</p></li><li><p><strong>执行 <code>git push -u origin main</code></strong>(或 <code>master</code>)将本地提交推送到 GitHub。</p></li><li><p><strong>后续开发</strong>:用 <code>git add</code> → <code>git commit</code> → <code>git push</code>完毕更新。若需合并远程改动,先<code>git pull --rebase</code> 再 <code>git push</code>。</p></li></ol><p>以上步骤涵盖了从安装软件到首次上传,再到后续代码同步的完整流程。只要每一步都按照说明并自己手动输入命令,就能顺利把项目上传并保持与 GitHub 的同步。</p></div>

---

<div style="background: rgba(240, 253, 244, 1); padding: 15px 18px; border-left: 3px solid rgba(34, 197, 94, 1); border-radius: 6px; margin-top: 30px">
<h3 style="color: rgba(21, 128, 61, 1); margin-top: 0; font-size: 16px"> 推荐阅读</h3>
<ul style="line-height: 2; font-size: 14px; padding-left: 20px">
<li><strong>玩转Git三剑客</strong> — <span style="color: rgba(136, 136, 136, 1); font-size: 13px">高效使用Git进行代码管理</span></li>
<li><strong>Vue开发实战</strong> — <span style="color: rgba(136, 136, 136, 1); font-size: 13px">Vue.js实战开发指南</span></li>
</ul>
</div><br><br>
来源:https://www.cnblogs.com/ljbguanli/p/18916988
頁: [1]
查看完整版本: 完整教程:从零开始,学会上传,更新,维护github仓库