<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Git - 标签 - ISLAND</title><link>https://youngxhui.top/tags/git/</link><description>Git - 标签 - ISLAND</description><generator>Hugo -- gohugo.io</generator><language>zh-CN</language><managingEditor>youngxhui@gmail.com (youngxhui)</managingEditor><webMaster>youngxhui@gmail.com (youngxhui)</webMaster><copyright>本作品采用知识共享署名-非商业性使用 4.0 国际许可协议进行许可。</copyright><lastBuildDate>Sun, 30 Apr 2017 20:49:21 +0000</lastBuildDate><atom:link href="https://youngxhui.top/tags/git/" rel="self" type="application/rss+xml"/><item><title>Git教程及使用经验</title><link>https://youngxhui.top/2017/04/git%E6%95%99%E7%A8%8B%E5%8F%8A%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C/</link><pubDate>Sun, 30 Apr 2017 20:49:21 +0000</pubDate><author><name>youngxhui</name></author><guid>https://youngxhui.top/2017/04/git%E6%95%99%E7%A8%8B%E5%8F%8A%E4%BD%BF%E7%94%A8%E7%BB%8F%E9%AA%8C/</guid><description><![CDATA[<h2 id="前言" class="headerLink">
    <a href="#%e5%89%8d%e8%a8%80" class="header-mark"></a>前言</h2><p>经过一周的准备，完成了 Git 的一些使用经验及教程。</p>
<p>主要是从命令行的角度来讲解。</p>
<h2 id="git-是什么" class="headerLink">
    <a href="#git-%e6%98%af%e4%bb%80%e4%b9%88" class="header-mark"></a>Git 是什么</h2><p>git 在维基百科上的解释是这样的</p>
<blockquote>
  <p>git是一个分布式版本控制软件，最初由林纳斯·托瓦兹（Linus Torvalds）创作，于2005年以GPL发布。最初目的是为更好地管理Linux内核开发而设计。应注意的是，这与GNU Interactive Tools（一个类似Norton Commander界面的文件管理器）有所不同。</p>

</blockquote><p>在我看来 Git 是一个 版本控制工具 和 团队协作 软件。</p>
<h2 id="git-版本控制" class="headerLink">
    <a href="#git-%e7%89%88%e6%9c%ac%e6%8e%a7%e5%88%b6" class="header-mark"></a>Git 版本控制</h2><p>简单的使用看这个博客 <a href="http://youngxhui.github.io/2016/08/13/Git%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/" target="_blank" rel="noopener noreferrer">Git教程操作</a></p>
<p>这个博客会在这个基础上更加详细的讲解命令行的使用，以及一些操作原理。</p>
<h3 id="git-status" class="headerLink">
    <a href="#git-status" class="header-mark"></a>git status</h3><p>当我们通过 <code>git add</code> 命令添加文件到 git 中的时候，可以通过 <code>git status</code> 来了解当前代码库的情况。</p>
<p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git%20status.png'     ></p>
<p>通过 <code>git status</code>这个命令我们可以知道当前代码库的状态</p>
<h3 id="git-add--" class="headerLink">
    <a href="#git-add--" class="header-mark"></a>git add -?</h3><p>为什么每次都要使用 <code>git add</code> 把修改文件添加，之后在提交。</p>
<p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git%E4%BB%93%E5%BA%93.png'     ></p>
<p>这就是 git 的一个原理图。</p>
<p>git 工作时候分为工作区和版本库。工作区就是我们自己本地编辑的区域。而代码库又有一个 暂存区/缓存区。我们每次提交版本(<code>commit</code>) 都是在暂存区向代码库提交。所以我们每次都要通过 <code>add</code> 命令进行对修改的添加。</p>
<p>在给 git 添加文件的时候，我们使用的是 <code>git add &lt;filename&gt;</code>,但是当我们要添加大量的文件的时候，采用 <code>git add</code> 不仅浪费时间，而且还容易出错。此时我们使用 <code>git add -A</code> 会把所更改(包括文件的添加，删除，修改)的文件添加到代码库中。</p>
<p><code>git add -i</code> 会进入一个命令的子系统，提供我们选择。</p>
<p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git%20add%20-i.png'     ></p>
<h3 id="git-diff" class="headerLink">
    <a href="#git-diff" class="header-mark"></a>git diff</h3><p><code>git diff</code> 是一个比较代码的命令，如果不加参数表示本次和上一次的比较。我们可以添加要比较的两个版本的哈希值。</p>
<p><code>git diff 01df58 6e1baa</code></p>
<h2 id="git-协作开发" class="headerLink">
    <a href="#git-%e5%8d%8f%e4%bd%9c%e5%bc%80%e5%8f%91" class="header-mark"></a>Git 协作开发</h2><p>之前的 Git 是用来用做简单的 版本控制，记录了每一个版本。下面我们进行团队协作开发。</p>
<h3 id="git-分支" class="headerLink">
    <a href="#git-%e5%88%86%e6%94%af" class="header-mark"></a>Git 分支</h3><p>当一个人开发功能 A 而另一个人开发功能 B ，之后代码进行整合的时候，使代码既有功能 A 也有功能 B 。在 Git 中，Git 给了我们分支的概念。</p>
<p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git%20branch-a.png'     ></p>
<p>分支可以使用我们快速的开发协作，并且快速的合并。</p>
<h3 id="分支的使用" class="headerLink">
    <a href="#%e5%88%86%e6%94%af%e7%9a%84%e4%bd%bf%e7%94%a8" class="header-mark"></a>分支的使用</h3><p>通过命令 <code>git branch</code> 来查看分支情况，当前分支会前面带 * 标识。<code>git branch &lt;branch name&gt;</code> 来创建分支。
分支创建会拷贝当前的内容到新的分支。通过 <code>git checkout &lt;branch name&gt;</code> 来切换到我们需要切换的分支。</p>
<p>我们也可以通过 <code>git checkout -b &lt;filename&gt;</code> 来创建分支并且切换到该分支。</p>
<h3 id="分支的合并" class="headerLink">
    <a href="#%e5%88%86%e6%94%af%e7%9a%84%e5%90%88%e5%b9%b6" class="header-mark"></a>分支的合并</h3><p>当我们创建出新的分支的时候，就可以对这个分支进行版本的迭代。当我们做完版本迭代，版本更新的时候，就会向我们的 <code>master</code> 分支上就行合并。</p>
<p>如果我们要向 <code>master</code> 分支上合并我们新创建的分支叫 <code>dev</code> ，那么我们先切换分支到 <code>master</code>，在通过 <code>git merge dev</code> 进行合并两个分支。期间会有合并冲突，通过修改来进行保留。</p>
<h2 id="git-flow" class="headerLink">
    <a href="#git-flow" class="header-mark"></a>Git flow</h2><p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git%20flow.png'     ></p>
<p>如上图，git flow 是 <code>git</code> 给我们的一个协作的开发流程，<code>master</code> 分支一般是用于上线产品的发布，<code>develop</code> 分支使用开发， 在他的支线上我们一般会创建功能分支。我们在功能分支上开发完成后合并到 <code>develop</code> 分支上。这样就完成了我们的 git flow 开发。</p>
<p><a href="https://island-hexo.oss-cn-beijing.aliyuncs.com/Git&amp;Github.pptx" target="_blank" rel="noopener noreferrer">PPT下载</a></p>
]]></description></item><item><title>Git基本操作</title><link>https://youngxhui.top/2016/08/git%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/</link><pubDate>Sat, 13 Aug 2016 17:09:57 +0000</pubDate><author><name>youngxhui</name></author><guid>https://youngxhui.top/2016/08/git%E5%9F%BA%E6%9C%AC%E6%93%8D%E4%BD%9C/</guid><description><![CDATA[<h2 id="下载安装git客户端" class="headerLink">
    <a href="#%e4%b8%8b%e8%bd%bd%e5%ae%89%e8%a3%85git%e5%ae%a2%e6%88%b7%e7%ab%af" class="header-mark"></a>下载安装Git客户端</h2><p><img class="tw-inline" loading="lazy" src='https://island-hexo.oss-cn-beijing.aliyuncs.com/git.png'   alt="Git"  ></p>
<p><a href="https://git-scm.com/download" target="_blank" rel="noopener noreferrer">git</a>下载</p>
<h2 id="安装教程" class="headerLink">
    <a href="#%e5%ae%89%e8%a3%85%e6%95%99%e7%a8%8b" class="header-mark"></a>安装教程</h2><p>装完成后，还需要最后一步设置，在命令行输入：</p>
<blockquote>
  <p>$ git config &ndash;global user.name &ldquo;Your Name&rdquo;
$ git config &ndash;global user.email &ldquo;<a href="mailto:email@example.com" rel="">email@example.com</a>&rdquo;</p>

</blockquote><p>因为Git是分布式版本控制系统，所以，每个机器都必须自报家门：你的名字和Email地址。</p>
<h2 id="创建本地仓库" class="headerLink">
    <a href="#%e5%88%9b%e5%bb%ba%e6%9c%ac%e5%9c%b0%e4%bb%93%e5%ba%93" class="header-mark"></a>创建本地仓库</h2><blockquote>
  <p>$ git init</p>

</blockquote><h2 id="添加-add" class="headerLink">
    <a href="#%e6%b7%bb%e5%8a%a0-add" class="header-mark"></a>添加 add</h2><h3 id="把文件添加到仓库" class="headerLink">
    <a href="#%e6%8a%8a%e6%96%87%e4%bb%b6%e6%b7%bb%e5%8a%a0%e5%88%b0%e4%bb%93%e5%ba%93" class="header-mark"></a>把文件添加到仓库：</h3><blockquote>
  <p>$ git add readme.txt</p>

</blockquote><p>执行上面的命令，没有任何显示，这就对了，Unix的哲学是“没有消息就是好消息”，说明添加成功。</p>
<h3 id="把文件提交到仓库" class="headerLink">
    <a href="#%e6%8a%8a%e6%96%87%e4%bb%b6%e6%8f%90%e4%ba%a4%e5%88%b0%e4%bb%93%e5%ba%93" class="header-mark"></a>把文件提交到仓库：</h3><blockquote>
  <p>$ git commit -m &ldquo;add readme&rdquo;</p>

</blockquote><p><code>git commit</code>命令，<code>-m</code>后面输入的是本次提交的说明，可以输入任意内容，当然最好是有意义的，这样你就能从历史记录里方便地找到改动记录。</p>
<h2 id="同步到github" class="headerLink">
    <a href="#%e5%90%8c%e6%ad%a5%e5%88%b0github" class="header-mark"></a>同步到Github</h2><h3 id="上传到github" class="headerLink">
    <a href="#%e4%b8%8a%e4%bc%a0%e5%88%b0github" class="header-mark"></a>上传到github</h3><p>例如我们给一个叫Test的仓库同步</p>
<blockquote>
  <p>$ git remote add origin <a href="mailto:git@github.com" rel="">git@github.com</a>:youngxhui/Test.git</p>

</blockquote><p>现在可以push到你的仓库了</p>
<blockquote>
  <p>$ git push -u origin master</p>

</blockquote><p>第一次推送时，我们要添加<code>-u</code></p>
<p>之后我们可以这条命令</p>
<blockquote>
  <p>$ git push origin master</p>

</blockquote><h3 id="下载到github" class="headerLink">
    <a href="#%e4%b8%8b%e8%bd%bd%e5%88%b0github" class="header-mark"></a>下载到github</h3><p>从github上下载到本地</p>
<blockquote>
  <pre><code>$ git clone git@github.com:youngxhui/nuc_JavaLab.git
</code></pre>

</blockquote><h2 id="分支的操作" class="headerLink">
    <a href="#%e5%88%86%e6%94%af%e7%9a%84%e6%93%8d%e4%bd%9c" class="header-mark"></a>分支的操作</h2><h3 id="分支的创建" class="headerLink">
    <a href="#%e5%88%86%e6%94%af%e7%9a%84%e5%88%9b%e5%bb%ba" class="header-mark"></a>分支的创建</h3><p>创建分支</p>
<blockquote>
  <p>$ git checkout -b dev</p>

</blockquote><p><code>-b</code> 参数表示创建并切换</p>
<h3 id="查看分支" class="headerLink">
    <a href="#%e6%9f%a5%e7%9c%8b%e5%88%86%e6%94%af" class="header-mark"></a>查看分支</h3><p>用git branch命令查看当前分支：</p>
<blockquote>
  <pre><code>   $ git branch
   * dev
   master
</code></pre>

</blockquote><p>git branch命令会列出所有分支，<strong>当前分支</strong>前面会标一个<code>*</code>号。</p>
<p>我们就可以在dev分支上正常提交</p>
<blockquote>
  <p>$ git add readme.txt</p>

</blockquote><blockquote>
  <p>$ git commit -m &ldquo;branch test&rdquo;</p>

</blockquote><h3 id="切换分支" class="headerLink">
    <a href="#%e5%88%87%e6%8d%a2%e5%88%86%e6%94%af" class="header-mark"></a>切换分支</h3><p>切换回master分支：</p>
<blockquote>
  <p>$ git checkout master</p>

</blockquote><h3 id="合并分支" class="headerLink">
    <a href="#%e5%90%88%e5%b9%b6%e5%88%86%e6%94%af" class="header-mark"></a>合并分支</h3><p>把<code>dev</code>分支合并到<code>master</code></p>
<blockquote>
  <p>$ git merge dev</p>

</blockquote><h3 id="删除分支" class="headerLink">
    <a href="#%e5%88%a0%e9%99%a4%e5%88%86%e6%94%af" class="header-mark"></a>删除分支</h3><blockquote>
  <p>$ git branch -d dev</p>

</blockquote><hr>
]]></description></item></channel></rss>