特色的昵称 發表於 2025-7-18 00:00:00

Ubuntu下的git安装与配置的图文教程

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>一、安装git<ul class="second_class_ul"><li>1.打开终端,运行以下命令(需要联网)</li><li>2.验证安装</li></ul></li><li>二、配置git<ul class="second_class_ul"><li>2.1.配置用户名及邮箱地址</li><li>2.2.检查配置</li><li>2.3.设置文本编辑器</li></ul></li><li>三、设置SSH密钥<ul class="second_class_ul"><li>3.1.生成密钥</li><li>3.2.将公钥添加到你的远程仓库服务的账户设置中</li><li>3.3.测试SSH连接</li><ul class="third_class_ul"><li>3.3.1.手动配置known_hosts文件 (可选)</li></ul></ul></li></ul></div><p class="maodian"></p><h2>一、安装git</h2>
<p class="maodian"></p><h3>1.打开终端,运行以下命令(需要联网)</h3>
<div class="dxycode"><pre class="brush:bash;">sudo apt-get update
sudo apt-get install git</pre></div>
<p class="maodian"></p><h3>2.验证安装</h3>
<p>安装完成之后,通过运行以下命令验证git是否已经正确安装:</p>
<div class="dxycode"><pre class="brush:bash;">git --version</pre></div>
<p style="text-align:center"><img alt="" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG301Z1.png" /></p>
<p class="maodian"></p><h2>二、配置git</h2>
<p class="maodian"></p><h3>2.1.配置用户名及邮箱地址</h3>
<p>在使用git时,必须要设置你的<strong>用户名</strong>和<strong>电子邮箱地址</strong>。其中电子邮箱地址可以不是真正的有效邮箱,但是必须要有。</p>
<div class="dxycode"><pre class="brush:bash;">git config --global user.name "你的名字"
git config --global user.email "你的电子邮件地址"</pre></div>
<p style="text-align:center"><img alt="" height="79" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG302930.png" width="772" /></p>
<p class="maodian"></p><h3>2.2.检查配置</h3>
<p>输入以下命令,可以查看所有的全局配置。</p>
<div class="dxycode"><pre class="brush:bash;">git config --list --show-origin</pre></div>
<p style="text-align:center"><img alt="" height="79" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG302A6.png" width="691" /></p>
<p class="maodian"></p><h3>2.3.设置文本编辑器</h3>
<p>如果你希望使用特定的文本编辑器来编辑git提交信息,可以设置一个默认的编辑器,此处以vim为例,输入下列命令:</p>
<div class="dxycode"><pre class="brush:bash;">git config --global core.editor "vim"</pre></div>
<p class="maodian"></p><h2>三、设置SSH密钥</h2>
<p>如果你准备使用SSH与远程仓库(例如github、gitee、gitlab等)交互,那么你需要生存一个SSH密钥。</p>
<p class="maodian"></p><h3>3.1.生成密钥</h3>
<p>如果以下命令,生成一个SSH密钥:</p>
<p>(注意<strong>此处的C是大写的C</strong>),如果输入成了小写,可能会跳出提示Too Many Arguments。这个问题也可能出现在空格数量不对的情况。</p>
<div class="dxycode"><pre class="brush:bash;">ssh-keygen -t rsa -C "你的电子邮件地址"</pre></div>
<p style="text-align:center"><img alt="" height="58" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG302602.png" width="777" /></p>
<p>出现新提示,询问你想要将文件存放到哪里,以及设置SSH密钥的密码,这里都选择默认,直接回车。</p>
<p style="text-align:center"><img alt="" height="82" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG302H6.png" width="600" /></p>
<p class="maodian"></p><h3>3.2.将公钥添加到你的远程仓库服务的账户设置中</h3>
<p>3.2.1.通过以下命令查看公钥:</p>
<div class="dxycode"><pre class="brush:bash;">cat ~/.ssh/id_rsa.pub</pre></div>
<p>将输出结果复制,将其添加到你的远程仓库服务账户的SSH密钥设置中。</p>
<p>3.2.2.Github设置SSH公钥</p>
<p>进入github个人界面,点击右上角的<strong>头像</strong>,选择<strong>Settings</strong>。</p>
<p style="text-align:center"><img alt="" height="1137" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG303A4.png" width="558" /></p>
<p>下滑,找到&nbsp;<strong>SSH and GPG Keys&nbsp;</strong>栏目,点击 <strong>New SSH key </strong>。</p>
<p style="text-align:center"><img alt="" height="969" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG303L5.png" width="2179" /></p>
<p>输入标题,将刚才生成的SSH密钥复制粘贴过来,点击Add SSH Key就好。</p>
<p style="text-align:center"><img alt="" height="849" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG303K0.png" width="1572" /></p>
<p>添加成功</p>
<p style="text-align:center"><img alt="" height="469" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG304524.png" width="1576" /></p>
<p class="maodian"></p><h3>3.3.测试SSH连接</h3>
<p>可以尝试ping远程仓库,确保SSH连接正常工作,可以尝试ping远程仓库:</p>
<div class="dxycode"><pre class="brush:bash;">ssh -T git@github.com
#对于其他服务,只需将github.com替换为相应的服务域名。如果一切设置正确,你应该会看到一条欢迎消息。</pre></div>
<p>跳出提示,因为首次连接host 20.205.243.166(就是github的网址IP),处于安全考虑,这个IP不被信任。询问你是否要统一连接,可以直接输入yes自动配置known_hosts文件或者选择手动配置KNOWN_HOSTS文件。</p>
<p style="text-align:center"><img alt="" height="222" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG304556.png" width="1438" /></p>
<p class="maodian"></p><h4>3.3.1.手动配置known_hosts文件 (可选)</h4>
<p>接下来使用vim编辑器来编辑这个文件,将我们上面获取到的密钥粘贴进来。但此时我们先来做一个简单的vim例子,为避免直接上手不会使用vim编辑器。</p>
<p><strong>进入到vim页面之后先不要乱按,按照指示来进行操作</strong>,vim编辑和常规windows下的输入之类的不一样。</p>
<p>1.补充知识vim:</p>
<p>vim刚打开时进入的是①命令模式。</p>
<p><strong>&nbsp;①命令模式(又称一般模式)</strong></p>
<p>在命令模式下,用户键盘的输入会被识别为命令,而不是字符的输入。在次模式之下,用户可以进行光标的移动、删除、复制等操作。输入 i 切换到②输入模式,X删除当前光标所在位置的字符, :切换到底线命令模式。</p>
<p>辨认方法:底部会出现文件名及当前光标所处位置。此处以输入<strong> vim test </strong>为例,左下角文件名为test ,右下角0,0-1是指光标当前在初始位置。</p>
<p style="text-align:center"><img alt="" height="135" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG304930.png" width="1449" /></p>
<p><strong>②输入/插入模式</strong></p>
<p>在命令模式下按下 i , a , o 等键会进入输入模式,在此模式下,用户可以使用键盘输入字符、回车换行、退档键删除字符等操作。按下ESC键,则返回命令模式</p>
<p>&nbsp;辨认方法:底部文字变为<strong>-- 插入 --</strong></p>
<p style="text-align:center"><img alt="" height="22" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG305K8.png" width="669" /></p>
<p>此时可以输入,输入文字 this is a test,来尝试一下。可以看到底部文字为-- 插入 -- ,光标所在的位置也变为了1,14,即光标在第一行第14列。</p>
<p style="text-align:center"><img alt="" height="1138" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG305G1.png" width="1243" /></p>
<p><strong>③底线命令模式</strong>:在命令模式下按下冒号:切换到底线命令模式,此模式用于输入单个或多个字符的命令,如保存文件:w ,退出程序 :q ,保存并退出 :wq,按下ESC键回到命令模式。</p>
<p>辨认方法:光标移动到底部,且底部出现 : 的标记。没有之前的光标在几行几列的信息。</p>
<p style="text-align:center"><img alt="" height="618" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG305N8.png" width="763" /></p>
<p>三种模式的转换,都是以回到命令模式为中转的。</p>
<p style="text-align:center"><img alt="" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG305A5.jpg" /></p>
<p>2.正式编写known_hosts文件</p>
<p>输入下列命令,进入.ssh路径中,查看当前并没有known_hosts文件。</p>
<div class="dxycode"><pre class="brush:bash;">cd ./.ssh</pre></div>
<p style="text-align:center"><img alt="" height="141" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG306459.png" width="868" /></p>
<p>输入<strong>vim known_hosts</strong>。使用vim编辑known_hosts文件。</p>
<div class="dxycode"><pre class="brush:bash;">vim known_hosts</pre></div>
<p>将刚才生成的SSH密钥,用鼠标选中,用鼠标右键复制起来,回到known_hosts文件中,按下i键,开始进入插入模式,鼠标右键粘贴就好。粘贴号之后,按下esc回车键,回到命令模式,按下shift 与&nbsp;:键,回到底线命令模式,输入wq即可保存并退出。</p>
<p>查看是否输入成功,输入<strong>cat known_hosts</strong>查看一下文件的内容。可以发现刚才粘贴的密钥已经保存成功了。</p>
<div class="dxycode"><pre class="brush:bash;">cat known_hosts</pre></div>
<p style="text-align:center"><img alt="" height="253" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG3064B.png" width="772" /></p>
<p>再次输入ssh -T git@github.com ,查看配置是否成功了。</p>
<p style="text-align:center"><img alt="" height="82" src="https://zhuji.jb51.net/uploads/allimg/20250718/2-250GQG306162.png" width="771" /></p>
<p><strong>结语</strong></p>
<p>到此你应该已经在Linux系统下安装并配置好Git了。</p>
<p>以上就是Linux-Ubuntu下的git安装与配置的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: Ubuntu下的git安装与配置的图文教程