<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>版本控制 on 墨殇个人博客</title><link>https://example.org/tags/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/</link><description>Recent content from 墨殇个人博客</description><generator>Hugo</generator><language>zh-cn</language><managingEditor>moshang168@gmail.com (墨殇'BLOG)</managingEditor><webMaster>moshang168@gmail.com (墨殇'BLOG)</webMaster><copyright>本博客所有文章除特别声明外，均采用 BY-NC-SA 许可协议。转载请注明出处！</copyright><atom:link href="https://example.org/tags/%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/index.xml" rel="self" type="application/rss+xml"/><item><title>Git入门到常用操作指南：再也不怕提交代码混乱</title><link>https://example.org/posts/git-beginner-guide/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><author>moshang168@gmail.com (墨殇'BLOG)</author><guid>https://example.org/posts/git-beginner-guide/</guid><description>
<![CDATA[<h1>Git入门到常用操作指南：再也不怕提交代码混乱</h1><p>作者：墨殇'BLOG（moshang168@gmail.com）</p>
        
          <h2 id="为什么一定要学git">
<a class="header-anchor" href="#%e4%b8%ba%e4%bb%80%e4%b9%88%e4%b8%80%e5%ae%9a%e8%a6%81%e5%ad%a6git"></a>
为什么一定要学Git
</h2><p>以前改代码或者写文档，大家都是这么备份的：<code>最终版.docx</code>、<code>最终版真的不改了.docx</code>、<code>最终版打死不改了.docx</code>，改坏了找不回历史版本，多人协作的时候互相覆盖文件。Git就是解决这个问题的版本控制工具，记录你文件的每一次修改，随时可以回滚到任何一个历史版本，多人协作的时候自动合并代码，是现在所有程序员和写作者的必备工具。</p>
<hr>
<h2 id="核心概念先搞懂">
<a class="header-anchor" href="#%e6%a0%b8%e5%bf%83%e6%a6%82%e5%bf%b5%e5%85%88%e6%90%9e%e6%87%82"></a>
核心概念先搞懂
</h2><p>新手学Git先记三个最核心的概念，剩下的命令都是围绕这三个概念来的：</p>
<ol>
<li><strong>仓库（Repository）</strong>：就是你项目的文件夹，Git会在里面记录所有文件的修改历史</li>
<li><strong>提交（Commit）</strong>：就是给你的项目拍一个快照，写清楚这次改了什么，之后随时可以回到这个快照</li>
<li><strong>分支（Branch）</strong>：相当于开了一个平行副本，你可以在分支上随便改代码，改完没问题再合并回主分支，不影响主线版本</li>
</ol>
<hr>
<h2 id="第一步安装与基础配置">
<a class="header-anchor" href="#%e7%ac%ac%e4%b8%80%e6%ad%a5%e5%ae%89%e8%a3%85%e4%b8%8e%e5%9f%ba%e7%a1%80%e9%85%8d%e7%bd%ae"></a>
第一步：安装与基础配置
</h2><p>安装好Git之后，打开终端先配置你的身份，这样每次提交的时候Git知道是谁改的代码：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git config --global user.name <span style="color:#e6db74">&#34;你的昵称&#34;</span>
</span></span><span style="display:flex;"><span>git config --global user.email <span style="color:#e6db74">&#34;你的邮箱&#34;</span>
</span></span></code></pre></div><hr>
<h2 id="日常最常用的命令90场景只用这几个">
<a class="header-anchor" href="#%e6%97%a5%e5%b8%b8%e6%9c%80%e5%b8%b8%e7%94%a8%e7%9a%84%e5%91%bd%e4%bb%a490%e5%9c%ba%e6%99%af%e5%8f%aa%e7%94%a8%e8%bf%99%e5%87%a0%e4%b8%aa"></a>
日常最常用的命令（90%场景只用这几个）
</h2><table>
	<thead>
			<tr>
					<th>命令</th>
					<th>作用</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td><code>git init</code></td>
					<td>在当前文件夹初始化一个Git仓库</td>
			</tr>
			<tr>
					<td><code>git clone 仓库地址</code></td>
					<td>把远程仓库的代码下载到本地</td>
			</tr>
			<tr>
					<td><code>git status</code></td>
					<td>查看当前文件夹哪些文件被修改了</td>
			</tr>
			<tr>
					<td><code>git add 文件名</code></td>
					<td>把修改的文件加入待提交列表</td>
			</tr>
			<tr>
					<td><code>git commit -m &quot;本次修改说明&quot;</code></td>
					<td>把待提交的修改打包成一个快照</td>
			</tr>
			<tr>
					<td><code>git push</code></td>
					<td>把本地提交的内容推送到远程仓库</td>
			</tr>
			<tr>
					<td><code>git pull</code></td>
					<td>把远程仓库的最新修改拉到本地</td>
			</tr>
			<tr>
					<td><code>git log</code></td>
					<td>查看所有历史提交记录</td>
			</tr>
	</tbody>
</table>
<hr>
<h2 id="新手最容易用到的救命操作">
<a class="header-anchor" href="#%e6%96%b0%e6%89%8b%e6%9c%80%e5%ae%b9%e6%98%93%e7%94%a8%e5%88%b0%e7%9a%84%e6%95%91%e5%91%bd%e6%93%8d%e4%bd%9c"></a>
新手最容易用到的救命操作
</h2><h3 id="1-改坏了想撤回上次的提交">
<a class="header-anchor" href="#1-%e6%94%b9%e5%9d%8f%e4%ba%86%e6%83%b3%e6%92%a4%e5%9b%9e%e4%b8%8a%e6%ac%a1%e7%9a%84%e6%8f%90%e4%ba%a4"></a>
1. 改坏了想撤回上次的提交
</h3><p>如果你改完代码发现改错了，想回到上次提交的状态：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git checkout .
</span></span></code></pre></div><p>所有修改过的文件都会恢复到上次提交时的样子。</p>
<h3 id="2-提交错了想撤销">
<a class="header-anchor" href="#2-%e6%8f%90%e4%ba%a4%e9%94%99%e4%ba%86%e6%83%b3%e6%92%a4%e9%94%80"></a>
2. 提交错了想撤销
</h3><p>如果你刚commit完发现提交错了，想退回上次的状态重新提交：</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-bash" data-lang="bash"><span style="display:flex;"><span>git reset --soft HEAD^
</span></span></code></pre></div><p>修改的内容还会保留在你的文件夹里，只是撤回了刚才的提交。</p>
        
        <hr><p>本文0001-01-01首发于<a href='https://example.org/'>墨殇个人博客</a>，最后修改于0001-01-01</p>]]></description><category>编程工具</category></item></channel></rss>