【Git】对于github的本地ssh客户端的配置与分支的使用 - 教程
<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>
<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="htmledit_views atom-one-dark" id="content_views"><h2>一、添加ssh公钥</h2><h3>1.使用git bash命令窗口</h3><p><img src="https://i-blog.csdnimg.cn/direct/bba552eb6d164eaf8839c50c1884ec90.png"></p><h3>2.查看git用户以及邮箱配置</h3><h4>(1)查看当前仓库的本地配置(仓库级配置)</h4>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>git config --local --list</code></pre>
<h4>(2)查看全局配置(用户级配置)</h4>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>git config --global --list</code></pre>
<p> 这些配置对当前用户的所有 Git 仓库有效,存储在用户目录下(Windows 通常是 <code>C:\Users\你的用户名\.gitconfig</code>,Linux/macOS 是 <code>~/.gitconfig</code>)</p><h3 style="background-color: rgba(0, 0, 0, 0)">补充1:</h3><h4>查看所有配置(合并显示)</h4><p>如果想一次性查看所有级别的配置(系统级 + 全局级 + 仓库级,优先级:仓库级 > 全局级 > 系统级),可以直接用:</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>git config --list</code></pre>
<h4>常用配置项说明</h4><p>查看时可能会看到这些关键配置(如果设置过的话):</p><ul><li><code>user.name</code>:你的 Git 用户名</li><li><code>user.email</code>:你的 Git 邮箱(通常与 SSH 密钥的邮箱一致)</li><li><code>remote.origin.url</code>:当前仓库的远程仓库地址(如 SSH 地址 <code>git@github.com:xxx/xxx.git</code> 或 HTTPS 地址)</li><li><code>core.editor</code>:Git 默认使用的编辑器</li></ul><p>如果需要修改配置(例如设置用户名 / 邮箱),可以用 <code>git config --global user.name "你的名字"</code> 或 <code>git config --global user.email "新邮箱"</code> 这类命令。</p><h3>3.生成 SSH 密钥对</h3><p>输入以下命令(推荐使用更安全的 <code>ed25519</code> 算法,若系统不支持则用 <code>rsa</code>):</p><h4>方法 1:推荐的 ed25519 算法(更安全)</h4>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh-keygen -t ed25519 -C "你的邮箱地址"</code></pre>
<h4>方法 2:兼容旧系统的 RSA 算法</h4><p>如果执行上面的命令提示不支持 <code>ed25519</code>,改用 RSA 算法:</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh-keygen -t rsa -b 4096 -C "你的邮箱地址"</code></pre>
<blockquote><p>注意:<code>-C</code> 后面的邮箱建议与你的 Git 配置邮箱一致(可通过 <code>git config user.email</code> 查看),方便识别密钥用途。</p></blockquote><h3>4.找到你的ssh的公钥所在的地方</h3><p>C:\Users\name\.ssh\</p><p><img src="https://i-blog.csdnimg.cn/direct/8df3334ddc834f539ab2f19ad65408ae.png"></p><p>.pub就是公钥</p><h3>5.点击github网页的右上角用户头像进入设置</h3><p><br><img src="https://i-blog.csdnimg.cn/direct/282d4113feec433e98af538ab51bef38.png"></p><h3>6.选择ssh选项,然后点击new ssh key,将公钥里的内容复制粘贴到ssh密钥框里</h3><p><img src="https://i-blog.csdnimg.cn/direct/45289ef2892b4e86bcc2fd3519ad6f09.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/9aa39e3d52234a3598ba23ff1a2b184a.png"></p><h3><strong>7.测试 SSH 连接 </strong></h3><p>在 Git Bash 中输入对应平台的测试命令</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code># 测试 GitHub 连接
ssh -T git@github.com
# 测试 GitLab 连接
ssh -T git@gitlab.com
# 测试 Gitee 连接
ssh -T git@gitee.com</code></pre>
<p> 首次连接会提示确认主机指纹,输入 <code>yes</code> 后回车。如果看到类似 "Hi 你的用户名!You've successfully authenticated..." 的提示,说明配置成功。</p><p>完成后,你就可以继续使用 SSH 方式操作 Git 仓库了(如 <code>git clone git@xxx</code>、<code>git push</code> 等)。</p><h3>8.测试链接失败情况处理</h3><p>出现</p><p><img src="https://i-blog.csdnimg.cn/direct/5ed37f07b58c4ac09eb8571aa8b1fbab.png"></p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>$ ssh-add /c/Users/name/.ssh/ssh_key_git/id_rsa
Could not open a connection to your authentication agent.</code></pre>
<p> 出现 "Could not open a connection to your authentication agent" 错误,是因为 SSH 代理(ssh-agent)没有启动。需要先启动代理再加载密钥,具体步骤如下:</p><h4>解决方法:</h4><ol><li><p><strong>启动 SSH 代理</strong><br> 在 Git Bash 中输入</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>eval $(ssh-agent -s)</code></pre> <p>成功启动后会显示类似 <code>Agent pid 1234</code> 的信息(数字是进程 ID)。</p></li><li><p><strong>重新加载私钥</strong><br> 再次执行加载密钥的命令(使用你的私钥路径):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh-add /c/Users/zky/.ssh/ssh_key_git/id_rsa</code></pre> </li><li><p><strong>验证是否加载成功</strong><br> 输入以下命令查看已加载的密钥:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh-add -l</code></pre> <p>如果显示了你的密钥指纹,说明加载成功。</p></li></ol><p>如果还是报超时错误,需要换一下端口以及其他配置。</p><p>在C:\Users\name\.ssh\的这个目录下才是ssh的客户端的配置地方,新建文件conf,如下:</p><p><img src="https://i-blog.csdnimg.cn/direct/560b8066676f4c68ac280fac64569406.png"></p><p>添加一下内容:</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code># GitHub 配置
Host github.com
Hostname ssh.github.com# GitHub 的 SSH 服务器地址
Port 443 # 用 443 端口(部分网络屏蔽 22 端口时可用)
User git # GitHub 固定用户名,必须是 git
IdentityFile ~/.ssh/ssh_key_git/id_rsa</code></pre>
<p>补充一下对于Windows下的ssh的客户端和服务器端的一些常用命令:</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#查看关于ssh的所有服务的安装状态
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
#配置本机电脑的ssh服务器端:快速确认 “Windows 防火墙是否允许 OpenSSH 服务器通过”
Get-NetFirewallRule -Name *OpenSSH-Server* | Select Name, DisplayName, Enabled
#配置本机电脑的ssh服务器端:设置sshd ssh服务器端自启动
Set-Service -Name sshd -StartupType 'Automatic'
#配置本机电脑的ssh服务器端:在 Windows 系统防火墙中创建一条允许外部设备通过 SSH 协议(默认端口 22)远程连接本机的入站规则,确保 SSH 服务能正常接收外部连接请求
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
#配置本机电脑的ssh服务器端:启动本地sshd服务器端
Start-Service sshd
#配置本机电脑的ssh服务器端:停止本地sshd服务器端
Stop-Service -Name sshd
#配置本机电脑的ssh服务器端:获取本地sshd服务器端状态
Get-Service -Name sshd
#本机电脑的ssh客户端:连接另外一台安装了ssh服务器的电脑
ssh 用户名@服务器IP或主机名 -p 端口号
#-v是打印日志功能
ssh -v 用户名@服务器IP或主机名 -p 端口号
#传输文件夹(需加 -r 参数,递归传输)
# 本地文件夹 → 远程
scp -r 本地文件夹路径 用户名@服务器IP或主机名:远程保存路径
# 远程文件夹 → 本地
scp -r 用户名@服务器IP或主机名:远程文件夹路径 本地保存路径
#通过 ssh-copy-id 命令将本地公钥写入远程服务器的授权列表(避免每次输入密码)
ssh-copy-id 用户名@服务器IP或主机名</code></pre>
<h4 style="background-color: rgba(0, 0, 0, 0)">补充说明:</h4><ul><li>每次重启 Git Bash 后,都需要重新执行 <code>eval $(ssh-agent -s)</code> 启动代理,再用 <code>ssh-add</code> 加载密钥(如果设置了密钥密码,此时需要输入密码)。</li><li>若想避免每次手动启动代理,可以配置 Git Bash 自动启动 ssh-agent(需要修改 <code>.bashrc</code> 或 <code>.profile</code> 文件,适合有经验的用户)。</li></ul><p>完成后,再次测试与 GitHub 的连接:</p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh -T git@github.com</code></pre>
<hr><p>在 Windows 系统中,要让 SSH 代理(<code>ssh-agent</code>)开机自动启动并加载你的 SSH 密钥,可以通过以下两种方法实现:</p><h4><strong>方法 1:通过 Git Bash 配置自动启动(推荐)</strong></h4><p>这种方法会让 Git Bash 启动时自动运行 <code>ssh-agent</code> 并加载密钥,步骤如下:</p><ol><li><p><strong>打开 Git Bash 配置文件</strong><br> 在 Git Bash 中输入以下命令,编辑 <code>.bashrc</code> 文件(如果没有会自动创建):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>vim ~/.bashrc</code></pre> </li><li><p><strong>添加自动启动脚本</strong><br> 在文件末尾粘贴以下内容(替换为你的私钥路径):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code># 启动 SSH 代理
if [ -z "$SSH_AUTH_SOCK" ]; then
# 检查是否已有 ssh-agent 进程
eval $(ssh-agent -s)
# 加载你的 SSH 私钥(替换为实际路径)
ssh-add /c/Users/name/.ssh/ssh_key_git/id_rsa
fi</code></pre>
<ul></ul></li><li><p><strong>测试配置</strong><br> 关闭当前 Git Bash 窗口,重新打开一个,会自动启动代理并加载密钥。可以用以下命令验证:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh-add -l# 查看已加载的密钥</code></pre> </li></ol><ol><li><p><strong>检测到配置文件情况</strong>:系统中存在 <code>~/.bashrc</code>(这是你之前配置自动启动 SSH 代理的文件),但缺少 <code>~/.bash_profile</code>、<code>~/.bash_login</code> 或 <code>~/.profile</code> 这些登录级别的配置文件。</p></li><li><p><strong>自动修复操作</strong>:Git Bash 自动为你创建了 <code>~/.bash_profile</code> 文件,并且在其中添加了加载 <code>~/.bashrc</code> 的指令。这样一来,无论你是以登录方式还是非登录方式启动 Git Bash,<code>~/.bashrc</code> 中的配置(启动 SSH 代理、加载密钥等)都会生效。</p></li></ol><h4>后续操作建议:</h4><ol><li><p>无需额外处理这个提示,它只是告知你系统做了自动配置。</p></li><li><p>可以通过以下命令确认 <code>~/.bash_profile</code> 是否正确创建:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>cat ~/.bash_profile</code></pre> <p>正常情况下会看到类似这样的内容(用于加载 <code>.bashrc</code>):</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code># generated by Git for Windows
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc</code></pre> </li><li><p>重新打开 Git Bash 测试,之前配置的 SSH 代理自动启动和密钥加载功能应该已经正常工作了。可以再次执行连接测试:</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>ssh -T git@github.com</code></pre> </li></ol><p> 如果看到 GitHub 的欢迎信息,说明所有配置都已生效,接下来就可以正常使用 <code>git clone</code>、<code>git push</code> 等命令了。</p><hr><h3 style="background-color: rgba(0, 0, 0, 0)">补充2(有些时候会把ssh客户端和sshd服务器端的配置路径的配置文件搞混):</h3><h4>Windows 系统(安装 OpenSSH Server 后)</h4><ul><li><strong>默认路径</strong>(适用于通过系统自带 “可选功能” 安装的 OpenSSH Server): <p>powershell</p> <pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code>C:\ProgramData\ssh\sshd_config</code></pre> <p></p></li><li>注意:<code>ProgramData</code> 是隐藏目录,需在文件资源管理器中开启 “显示隐藏文件” 才能直接访问。</li><li>早期版本或手动安装的 OpenSSH 可能位于:<code>C:\OpenSSH\sshd_config</code>(需根据实际安装路径调整)。</li></ul><hr><h2>二、仓库分支</h2><h3>1.先clone一个仓库下来作为测试演示</h3>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">$ git clone git@github.com:Molesidy/ubuntu_git_rep_test.git
Cloning into 'ubuntu_git_rep_test'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0 (from 0)
Receiving objects: 100% (3/3), done.</code></pre>
<h3>2.查看分支</h3>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#查看当前分支情况
git branch</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/95cde2f4db7047cb97fc83251d05c718.png"></p><h3>3.新建分支</h3>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#新建一个分支branch_test1
git branch branch_test1</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/eafe7e6bed834e7d83c266c62a655a86.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/121d3aa8ba0545fdb5ec7faaf2283733.png"></p><h3>4.切换到分支上开发操作</h3><p><img src="https://i-blog.csdnimg.cn/direct/6bba5a9f8e1b482cbcf743c93cb2e207.png"></p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#切换到新建的分支branch_test1
git checkout branch_test1</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/885f9ac584bf43a9ad3c1abfecf597d5.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/c249d29158be4f3ba32fde061bf4cd54.png"></p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#如果希望在创建分支的同时切换到该分支,可以使用以下命令:
git checkout -b branchname</code></pre>
<h3>5.在 branch_test1分支上将仓库的测试文件进行写入数据改变,此时远程的mytest.txt文件是空的<br><img src="https://i-blog.csdnimg.cn/direct/a9488d5503d44f5d82691b13f95d4d60.png"></h3><h3>6.在branch_test1分支上,进行提交文件mytest.txt</h3>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#将新建分支test推送到GitHub上
git push origin branch_test1或者 git push origin branch_test1:branch_test1
#本质是「本地分支 → 远程仓库分支」的映射关系,格式可拆解为:
#git push:
#远程仓库名是你现在所在的仓库名字默认是origin</code></pre>
<p><img src="https://i-blog.csdnimg.cn/direct/31bb17443e39401bb3d92f34285893fb.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/468f5350b040442ea32f9fefaa59c106.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/c44e2230df8246c9b07809dd30f7f331.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/dd3b2f591dde44e5b0ea74507dc6f521.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/6bc0b179f3af4baab1a3623520d1af14.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/f52543892e2747c7a5a1d48dd2067fa5.png"></p><h3>7.github切回main,查看mytest.txt</h3><p><img src="https://i-blog.csdnimg.cn/direct/821547166b534de1bd9dcbcc96a1c605.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/b82bf4691c0f4f0184d1e5fc43d6d805.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/04c7ce3ee52a40f1b5c7ffb15504b523.png"></p><h3>8.切回main,创建一个新的文件test2.txt,来回切换一次,看看区别</h3><p><img src="https://i-blog.csdnimg.cn/direct/d162105aa814424c99a0a9ecc5c7b2a3.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/8019495663314adc82f7483d8c660f28.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/9d3e6326a1424aad8ef786bb254261af.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/89dde7a0a9c3464088d62a903ef4860a.png"></p><p>在main分支提交测试文件二到本地分支main后,再切回branch_test1分支看文件就看不到test2.txt了</p><p><img src="https://i-blog.csdnimg.cn/direct/bb85f8747dfd41a4a5ca6cca82f61837.png"></p><p>然后我将测试文件2推送到远程的main了</p><h3>9.尝试将branch_test1的测试文件1:mytest.txt合并到main,现在的情况是本地仓库的mytest.txt看起来是只有一个文件,但是逻辑上是两个,查看内容,即使我现在在main分支下将全部内容提交推送到远程,mytest.txt 里并没有 hello github</h3><p><img src="https://i-blog.csdnimg.cn/direct/988498be7d974251880a6ae1fa092292.png"></p><p>main</p><p><img src="https://i-blog.csdnimg.cn/direct/595f2a63215a43f7955dc33d353ad68d.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/cb92f82137fe44e391db8739e4da7dcf.png"></p><p>branch</p><p><img src="https://i-blog.csdnimg.cn/direct/c0a7ae35d59e47a1bd054248e58d0b6f.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/b26d4260ecbe4b5c87d8f14275a77e2d.png"></p><h3>10.分支的合并</h3><p>当在不同的分支上进行了独立的开发工作后,我们可能需要将某个分支的修改合并到主线或其他分支上。Git提供了合并(merge)操作来实现这个功能。</p><p>切回main</p><p><img src="https://i-blog.csdnimg.cn/direct/6b3fb083cd6c4aabbd19054bcfe20c20.png"></p>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#指定哪一个分支合并到你现在的分支下
git merge branch_test1</code></pre>
<p style="text-align: center"><img src="https://i-blog.csdnimg.cn/direct/59865fae002e4ce4a9d69552dd474626.png"></p><p>这是 Git 在执行<strong>分支合并操作</strong>时自动打开的<strong>提交信息编辑界面</strong>(默认使用 Vim 编辑器),目的是让你为这次合并填写 “提交说明”(记录合并的原因、内容等)。</p><h4>界面内容解读</h4><ul><li><p>蓝色提示文字:</p><ul><li><code>Merge branch 'branch_test1'</code>:说明当前要合并的是名为 <code>branch_test1</code> 的分支。</li><li><code>Please enter a commit message to explain why this merge is necessary...</code>:提示你 “输入提交信息,解释这次合并为何必要(尤其是当合并的是‘更新后的上游分支到主题分支’时)”。</li><li><code>Lines starting with '#' will be ignored, and an empty message aborts the commit.</code>:以 <code>#</code> 开头的行是 “注释行”,Git 会忽略它们;如果提交信息为空,这次合并会被取消。</li></ul></li><li><p>下方大量带 <code>#</code> 的行:是 Git 自动生成的 “合并上下文”(比如分支信息、合并关联的提交等),仅用于参考,不会被当作提交信息的一部分。</p></li></ul><h4>如何操作这个界面(完成合并提交)</h4><p>当前处于 Vim 的<strong>普通模式</strong>,需要先进入「插入模式」才能输入文字,步骤如下:</p><ol><li>按键盘上的 <code>i</code> 键(进入 Vim 插入模式,此时左下角会显示 <code>-- INSERT --</code>);</li><li>在编辑区域输入<strong>有意义的合并提交信息</strong>(比如 <code>Merge branch_test1: 合并测试分支的功能更新</code>);</li><li>按 <code>Esc</code> 键(回到 Vim 普通模式);</li><li>输入 <code>:wq</code>(冒号 + <code>wq</code>,意思是 “保存(write)并退出(quit)”),然后按回车键。</li></ol><p>这样就完成了合并的提交,Git 会记录这次合并及你填写的说明。</p><h4>补充:如果想放弃合并?</h4><p>如果临时不想合并了,在这个界面中:</p><ul><li>按 <code>Esc</code> 键回到普通模式;</li><li>输入 <code>:q!</code>(冒号 + <code>q!</code>,意思是 “强制(!)退出(quit)且不保存”),然后按回车键。<br> Git 会取消这次合并操作,回到合并前的状态。</li></ul><p><img src="https://i-blog.csdnimg.cn/direct/a69e6aef71e940b3b476b8482ba82046.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/bc8f60fc3c7a41f2a2365405093f305a.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/2405ba984f2042459ef27964cac44a8a.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/f72f645f163c4e2e8b7e3bec6c275040.png"></p><p>合并完成,提交一下</p><p><img src="https://i-blog.csdnimg.cn/direct/2a779e1aed1e47c9a998356b26fb86a4.png"></p><h3>11.到branch_test1分支下修改一下文件,演示解决冲突(这里冲突没出来就后面有空再补)</h3><p><img src="https://i-blog.csdnimg.cn/direct/17fe0ed194c44dc69c765da6ad8b45b9.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/6875c99154e643f9bd8a04b943e17834.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/46672e839a334a4aada9d25b10998854.png"></p><p><img src="https://i-blog.csdnimg.cn/direct/a72d95da473845a0af7dc3eadb5532cc.png"></p><h3>12.合并完成后有时候就删了</h3>
<pre style="white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important"><code class="language-bash">#branchname是你希望删除的分支的名称。注意,只有当该分支的修改已经合并到其他分支时,才能被安全地删除。
git branch -d branchname
#如果分支的修改尚未合并,可以使用强制删除的命令:
git branch -D branchname</code></pre>
<h3>13.现在又有许多其他命令,更多的命令就看看菜鸟教程吧,所以命令常用常新。</h3><p>Git 分支管理 | 菜鸟教程</p></div><br><br>
来源:https://www.cnblogs.com/yjbjingcha/p/19091169
頁:
[1]