-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGit使用入门.html
1 lines (1 loc) · 48.1 KB
/
Git使用入门.html
1
<!-- build time:Wed Mar 09 2022 10:17:54 GMT+0800 (GMT+08:00) --><!doctype html><html class="theme-next mist" lang="zh-Hans"><head><meta name="generator" content="Hexo 3.8.0"><meta name="google-site-verification" content="7Tau9WyVgxnsEY9oYedu9g0U6_8akOX3wiKbaYcrg9A"><meta name="baidu-site-verification" content="EVwLiaxdxX"><link href="/css/xps13.css" rel="stylesheet" type="text/css"><link href="/css/message.css" rel="stylesheet" type="text/css"><link href="//fonts.googleapis.com/css?family=Baloo+Bhaijaan|Inconsolata|Josefin+Sans|Montserrat" rel="stylesheet"><script type="text/javascript" src="/js/jquery-1.11.3.min.js"></script><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1"><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css"><link href="/css/main.css?v=5.1.1" rel="stylesheet" type="text/css"><meta name="keywords" content="Git,"><link rel="alternate" href="/atom.xml" title="MrBird" type="application/atom+xml"><link rel="shortcut icon" type="image/x-icon" href="/bird.png?v=5.1.1"><meta name="description" content="准备Git环境首先在ubuntu中查看是否安装了Git:123$ gitThe program 'git' is currently not installed. You can install it by typing:sudo apt install gitlinux提示尚未安装Git,并提供了安装的指令:sudo apt install git。第一次使用Git需要设置个人信息,如用户名邮箱"><meta name="keywords" content="Git"><meta property="og:type" content="article"><meta property="og:title" content="Git使用入门"><meta property="og:url" content="http://mrbird.cc/Git使用入门.html"><meta property="og:site_name" content="MrBird"><meta property="og:description" content="准备Git环境首先在ubuntu中查看是否安装了Git:123$ gitThe program 'git' is currently not installed. You can install it by typing:sudo apt install gitlinux提示尚未安装Git,并提供了安装的指令:sudo apt install git。第一次使用Git需要设置个人信息,如用户名邮箱"><meta property="og:locale" content="zh-Hans"><meta property="og:updated_time" content="2020-01-21T03:30:03.126Z"><meta name="twitter:card" content="summary"><meta name="twitter:title" content="Git使用入门"><meta name="twitter:description" content="准备Git环境首先在ubuntu中查看是否安装了Git:123$ gitThe program 'git' is currently not installed. You can install it by typing:sudo apt install gitlinux提示尚未安装Git,并提供了安装的指令:sudo apt install git。第一次使用Git需要设置个人信息,如用户名邮箱"><script type="text/javascript" id="hexo.configurations">var NexT=window.NexT||{},CONFIG={root:"/",scheme:"Mist",sidebar:{position:"left",display:"always",offset:12,offset_float:0,b2t:!1,scrollpercent:!0},fancybox:!1,motion:!1}</script><title>Git使用入门 | MrBird</title></head><body ondragstart="return!1" class="animsition" lang="zh-Hans" style="overflow-x:hidden;padding-left:280px"><script type="text/javascript" src="/js/mo.min.js"></script><style>body{text-rendering:geometricPrecision!important;font-family:'Josefin Sans','PingFang SC'!important;-webkit-font-smoothing:antialiased!important;-webkit-text-size-adjust:100%!important;background-color:#f4f6f7}@media (min-width:768px) and (max-width:991px){body .header-innerr{width:700px!important}}.header-innerr{margin:0 auto;padding:100px 0 70px;width:880px}@media (min-width:1600px){.container .header-innerr{width:996px}}.header-innerr{position:relative}.header-innerr{padding:0}.header-innerr:after,.header-innerr:before{content:" ";display:table}.header-innerr:after{clear:both}@media (max-width:767px){.header-innerr{width:auto;padding:10px;margin-bottom:-20px}}</style><div class="container sidebar-position-left page-post-detail"><div class="headband"></div><header id="header" class="header"><div class="header-inner"><div class="site-brand-wrapper"><div class="site-meta"><link href="https://fonts.font.im/css?family=Merienda" rel="stylesheet"><div class="custom-logo-site-title"></div><h1 class="site-subtitle" itemprop="description"></h1></div><div class="site-nav-toggle"><button><span class="btn-bar"></span> <span class="btn-bar"></span> <span class="btn-bar"></span></button></div></div><nav class="site-nav"><div class="site-search"><div class="popup search-popup local-search-popup"><div class="local-search-header clearfix"><span class="search-icon"><i class="fa fa-search"></i> </span><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span><div class="local-search-input-wrapper"><input autocomplete="off" placeholder="Search" spellcheck="false" type="text" id="local-search-input"></div></div><div id="local-search-result"></div></div></div></nav></div><div class="header-innerr"></div></header><main id="main" class="main"><div class="main-inner"><div class="content-wrap"><div id="content" class="content"><div id="posts" class="posts-expand"><article class="post post-type-normal" itemscope itemtype="http://schema.org/Article"><link itemprop="mainEntityOfPage" href="http://mrbird.cc/Git使用入门.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="name" content="MrBird"><meta itemprop="description" content=""><meta itemprop="image" content="/images/blogImage.jpg"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="MrBird"></span><header class="post-header"><h2 class="post-title" itemprop="name headline">Git使用入门</h2><div class="post-meta"><span class="post-time"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text"></span> <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-27T15:04:31+08:00">2017-05-27 </time></span><span></span> <span class="post-meta-divider">|</span> <span class="page-pv"><i class="fa fa-laptop"></i> Visit count <span class="busuanzi-value" id="busuanzi_value_page_pv"></span></span></div></header><div class="post-body" itemprop="articleBody"><h2 id="准备Git环境"><a href="#准备Git环境" class="headerlink" title="准备Git环境"></a>准备Git环境</h2><p>首先在ubuntu中查看是否安装了Git:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git</span><br><span class="line">The program <span class="string">'git'</span> is currently not installed. You can install it by typing:</span><br><span class="line">sudo apt install git</span><br></pre></td></tr></table></figure><p></p><p>linux提示尚未安装Git,并提供了安装的指令:<code>sudo apt install git</code>。第一次使用Git需要设置个人信息,如用户名邮箱等:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">$ git config --global user.name <span class="string">"mrbird"</span></span><br><span class="line">$ git config --global user.email <span class="string">"[email protected]"</span></span><br></pre></td></tr></table></figure><p></p><a id="more"></a><h2 id="创建Git项目"><a href="#创建Git项目" class="headerlink" title="创建Git项目"></a>创建Git项目</h2><p>首先创建一个projects目录,然后在projects下创建first-project项目,包含foo.txt和bar.txt两个文件:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">$ mkdir projects</span><br><span class="line">$ <span class="built_in">cd</span> projects/</span><br><span class="line">$ mkdir first-project</span><br><span class="line">$ <span class="built_in">cd</span> first-project/</span><br><span class="line">$ vi foo.txt</span><br><span class="line">$ vi bar.txt</span><br><span class="line">$ tree -A projects/</span><br><span class="line">projects/</span><br><span class="line">└── first-project</span><br><span class="line"> ├── bar.txt</span><br><span class="line"> └── foo.txt</span><br><span class="line"> </span><br><span class="line">1 directory, 2 files</span><br></pre></td></tr></table></figure><p></p><h3 id="创建版本库"><a href="#创建版本库" class="headerlink" title="创建版本库"></a>创建版本库</h3><p>首先,我们需要在first-project下创建一个Git版本库,用于存储项目本身及其历史。对于一个带有版本库的项目目录,一般称之为<strong>工作区</strong>。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> first-project/</span><br><span class="line">$ git init</span><br><span class="line">Initialized empty Git repository <span class="keyword">in</span> /home/mrbird/projects/first-project/.git/</span><br></pre></td></tr></table></figure><p></p><p>再次使用<code>tree</code>命令就可以看到,在first-project目录下多了个.git隐藏目录,该目录就是Git用于跟踪并管理版本库用的:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ tree -aA projects/</span><br><span class="line">projects/</span><br><span class="line">└── first-project</span><br><span class="line"> ├── bar.txt</span><br><span class="line"> ├── foo.txt</span><br><span class="line"> └── .git</span><br><span class="line">...</span><br></pre></td></tr></table></figure><p></p><h3 id="首次提交"><a href="#首次提交" class="headerlink" title="首次提交"></a>首次提交</h3><p>接下来,需要将foo.txt和bar.txt添加到版本库中去。在Git中,通常将项目的一个版本称之为一次提交。提交分为两个步骤:</p><ol><li>使用<code>git add</code>命令来确定下次提交前应该包含的文件;</li><li>使用<code>git commit</code>命令将这些文件传送到版本库中,并生成散列值以标识这次更新。</li></ol><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ git add foo.txt bar.txt </span><br><span class="line">$ git commit -m <span class="string">"add foo.txt bar.txt"</span></span><br><span class="line">[master (root-commit) c2e4810] add foo.txt bar.txt</span><br><span class="line"> 2 files changed, 0 insertions(+), 0 deletions(-)</span><br><span class="line"> create mode 100644 bar.txt</span><br><span class="line"> create mode 100644 foo.txt</span><br></pre></td></tr></table></figure><h3 id="检查状态"><a href="#检查状态" class="headerlink" title="检查状态"></a>检查状态</h3><p>现在将first-project目录下的bar.txt文件删除,添加bar.html文件,修改foo.txt文件内容。然后使用<code>git status</code>命令查看状态:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br><span class="line">On branch master</span><br><span class="line">Changes not staged <span class="keyword">for</span> commit:</span><br><span class="line"> (use <span class="string">"git add/rm <file>..."</span> to update what will be committed)</span><br><span class="line"> (use <span class="string">"git checkout -- <file>..."</span> to discard changes <span class="keyword">in</span> working directory)</span><br><span class="line"> </span><br><span class="line"> deleted: bar.txt</span><br><span class="line"> modified: foo.txt</span><br><span class="line"> </span><br><span class="line">Untracked files:</span><br><span class="line"> (use <span class="string">"git add <file>..."</span> to include <span class="keyword">in</span> what will be committed)</span><br><span class="line"> </span><br><span class="line"> bar.html</span><br><span class="line"> </span><br><span class="line">no changes added to commit (use <span class="string">"git add"</span> and/or <span class="string">"git commit -a"</span>)</span><br></pre></td></tr></table></figure><p></p><p>因为还没有使用<code>git add</code>命令将bar.html添加到版本库,所以显示为未跟踪状态(Untracked)。 我们也可以使用<code>git diff</code>命令来查看被修改的文件具体修改了啥:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git diff foo.txt</span><br><span class="line">diff --git a/foo.txt b/foo.txt</span><br><span class="line">index e69de29..3f9a7b1 100644</span><br><span class="line">--- a/foo.txt</span><br><span class="line">+++ b/foo.txt</span><br><span class="line">@@ -0,0 +1 @@</span><br><span class="line">+foo file</span><br></pre></td></tr></table></figure><p></p><p>可以看出,foo.txt文件添加了一行内容:foo file。</p><h3 id="提交修改"><a href="#提交修改" class="headerlink" title="提交修改"></a>提交修改</h3><p>将上述的修改归档成一次新的提交,即添加foo.txt和新文件bar.html,删除bar.txt:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git add foo.txt bar.html </span><br><span class="line">$ git rm bar.txt </span><br><span class="line">rm <span class="string">'bar.txt'</span></span><br></pre></td></tr></table></figure><p></p><p>再次使用<code>git status</code>命令查看当前状态:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$ git status</span><br><span class="line">On branch master</span><br><span class="line">Changes to be committed:</span><br><span class="line"> (use <span class="string">"git reset HEAD <file>..."</span> to unstage)</span><br><span class="line"> </span><br><span class="line"> new file: bar.html</span><br><span class="line"> deleted: bar.txt</span><br><span class="line"> modified: foo.txt</span><br></pre></td></tr></table></figure><p></p><p>这时候我们就可以放心的使用<code>git commit</code>来提交这些修改:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">$ git commit -m <span class="string">"add bar.html,modify foo.txt,delete bar.txt"</span></span><br><span class="line">[master 94418b1] add bar.html,modify foo.txt,delete bar.txt</span><br><span class="line"> 3 files changed, 2 insertions(+)</span><br><span class="line"> create mode 100644 bar.html</span><br><span class="line"> delete mode 100644 bar.txt</span><br></pre></td></tr></table></figure><p></p><h3 id="显示历史"><a href="#显示历史" class="headerlink" title="显示历史"></a>显示历史</h3><p>使用<code>git log</code>命令可以查看所有提交的历史,并按时间顺序倒序排列:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">log</span></span><br><span class="line">commit 94418b10e64f46bc4f48049037fadcb12efc5d10</span><br><span class="line">Author: mrbird <[email protected]></span><br><span class="line">Date: Sat May 27 14:09:51 2017 +0800</span><br><span class="line"> </span><br><span class="line"> add bar.html,modify foo.txt,delete bar.txt</span><br><span class="line"> </span><br><span class="line">commit c2e48100c4f2fc8afaed2b1cac90deaf6a7e34ec</span><br><span class="line">Author: mrbird <[email protected]></span><br><span class="line">Date: Sat May 27 11:08:29 2017 +0800</span><br><span class="line"> </span><br><span class="line"> add foo.txt bar.txt</span><br></pre></td></tr></table></figure><p></p><h2 id="Git协作功能"><a href="#Git协作功能" class="headerlink" title="Git协作功能"></a>Git协作功能</h2><p>假如现在还有另外一位开发者也参与开发first-project这个项目,为了方便实验,现在在projects目录下开辟另外一个工作区first-project-clone,供第二位开发者使用。</p><h3 id="克隆版本库"><a href="#克隆版本库" class="headerlink" title="克隆版本库"></a>克隆版本库</h3><p>现使用<code>git clone</code>来克隆first-project,该版本库副本包含了所有原始信息和整个项目的历史信息:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">clone</span> projects/first-project/ projects/first-project-clone</span><br><span class="line">Cloning into <span class="string">'projects/first-project-clone'</span>...</span><br><span class="line"><span class="keyword">done</span>.</span><br></pre></td></tr></table></figure><p></p><p>克隆后,观察项目结构:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ tree -aA projects/</span><br><span class="line">projects/</span><br><span class="line">├── first-project</span><br><span class="line">│ ├── bar.html</span><br><span class="line">│ ├── foo.txt</span><br><span class="line">│ └── .git</span><br><span class="line">│ ├── ...</span><br><span class="line">└── first-project-clone</span><br><span class="line"> ├── bar.html</span><br><span class="line"> ├── foo.txt</span><br><span class="line"> └── .git</span><br><span class="line"> ├── ...</span><br></pre></td></tr></table></figure><p></p><h3 id="从另一个版本库中获取修改"><a href="#从另一个版本库中获取修改" class="headerlink" title="从另一个版本库中获取修改"></a>从另一个版本库中获取修改</h3><p>现在,在first-project/foo.txt文件中插入一行信息”hello world,hello git”,并且提交修改:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> projects/first-project</span><br><span class="line">$ vi foo.txt </span><br><span class="line">$ git add foo.txt </span><br><span class="line">$ git commit -m <span class="string">"modify foo.txt in original"</span></span><br><span class="line">[master 8e1b132] modify foo.txt <span class="keyword">in</span> original</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure><p></p><p>同时修改克隆版本库first-project-clone中的bar.html,并且提交修改:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git add bar.html</span><br><span class="line">$ git commit -m <span class="string">"modify bar.html in clone"</span></span><br><span class="line">[master 796e40d] modify bar.html <span class="keyword">in</span> <span class="built_in">clone</span></span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure><p></p><p>现在,新的提交已经被存入到了first-project版本库中,但是其克隆版本库first-project-clone并没有取得这次修改(即foo.txt还是保持原样)。如果克隆版本库也要获取原版本库中的修改,我们可以使用<code>git pull</code>命令来获取:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> ../first-project-clone/</span><br><span class="line">$ cat foo.txt </span><br><span class="line">foo file</span><br><span class="line">$ git pull</span><br><span class="line">remote: Counting objects: 3, <span class="keyword">done</span>.</span><br><span class="line">remote: Compressing objects: 100% (2/2), <span class="keyword">done</span>.</span><br><span class="line">remote: Total 3 (delta 0), reused 0 (delta 0)</span><br><span class="line">Unpacking objects: 100% (3/3), <span class="keyword">done</span>.</span><br><span class="line">From /home/mrbird/projects/first-project</span><br><span class="line"> 8e1b132..27b76ec master -> origin/master</span><br><span class="line">Merge made by the <span class="string">'recursive'</span> strategy.</span><br><span class="line"> foo.txt | 1 +</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br><span class="line">$ cat foo.txt </span><br><span class="line">foo file</span><br><span class="line">hello world,hello git</span><br></pre></td></tr></table></figure><p></p><p>由于在创建克隆版本库的时候,原版本库的路径就已经被存储到克隆版本库中了。所以在克隆版本库中使用git pull命令,不带路径的话,默认就是从原版本库中取回修改。 从结果中可以看出,Git从原版本库中取回了修改,与克隆体中的修改进行了比对,并且合并了这些修改。这个过程称为合并(merge)。</p><div class="note danger"><p>在pull过程中,如果发生了冲突,比如两位开发者同时对foo.txt文件进行了修改,Git不会自动去处理这些冲突,必须手动去做修改。</p></div>在pull取回修改,并且merge合并之后,可以使用<code>git log --graph</code>命令来查看图形化日志:<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">log</span> --graph --oneline </span><br><span class="line">* 8747b24 Merge branch <span class="string">'master'</span> of /home/mrbird/projects/first-project</span><br><span class="line">|\ </span><br><span class="line">| * 27b76ec modify foo.txt <span class="keyword">in</span> original</span><br><span class="line">* | 796e40d modify bar.html <span class="keyword">in</span> <span class="built_in">clone</span></span><br><span class="line">|/ </span><br><span class="line">* 94418b1 add bar.html,modify foo.txt,delete bar.txt</span><br><span class="line">* c2e4810 add foo.txt bar.txt</span><br></pre></td></tr></table></figure><p></p><p>从上面可以清晰的看出,分支合并的情况。</p><h3 id="从任意版本库中取回修改"><a href="#从任意版本库中取回修改" class="headerlink" title="从任意版本库中取回修改"></a>从任意版本库中取回修改</h3><p><code>git pull</code>带上仓库路径的话就可以从任意一个版本库中取回修改了,现在在原版本库first-project中取回其克隆版本库first-project-clone对bar.html的修改:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">$ git pull ../first-project-clone/</span><br><span class="line">remote: Counting objects: 5, <span class="keyword">done</span>.</span><br><span class="line">remote: Compressing objects: 100% (4/4), <span class="keyword">done</span>.</span><br><span class="line">remote: Total 5 (delta 0), reused 0 (delta 0)</span><br><span class="line">Unpacking objects: 100% (5/5), <span class="keyword">done</span>.</span><br><span class="line">From ../first-project-clone</span><br><span class="line"> * branch HEAD -> FETCH_HEAD</span><br><span class="line">Updating 27b76ec..8747b24</span><br><span class="line">Fast-forward</span><br><span class="line"> bar.html | 1 +</span><br><span class="line"> 1 file changed, 1 insertion(+)</span><br></pre></td></tr></table></figure><p></p><h3 id="创建共享版本库"><a href="#创建共享版本库" class="headerlink" title="创建共享版本库"></a>创建共享版本库</h3><p>除了使用<code>git pull</code>命令从别的版本库取回修改外,我们还可以使用<code>git push</code>命令将本地修改传送给其他版本库。 不过,<code>git push</code>命令只适用于那些没有开发者在上面具体工作的版本库。通常的做法是创建一个没有工作区的版本库,也称之为裸版本库。裸版本库就是单纯用于存放各个开发这提交(push)的修改,方便别的开发者取回(pull)这些修改。现使用<code>git clone --bare</code>命令来创建裸版本库:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">$ git <span class="built_in">clone</span> --bare first-project first-project-bare</span><br><span class="line">Cloning into bare repository <span class="string">'first-project-bare'</span>...</span><br><span class="line"><span class="keyword">done</span>.</span><br></pre></td></tr></table></figure><p></p><h3 id="使用push向裸版本库提交修改"><a href="#使用push向裸版本库提交修改" class="headerlink" title="使用push向裸版本库提交修改"></a>使用push向裸版本库提交修改</h3><p>再次修改原版本库first-project中的foo.txt文件,并且提交修改:</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">$ git add foo.txt </span><br><span class="line">$ git commit -m <span class="string">"modify foo.txt in original again"</span></span><br><span class="line">[master 31e1f6f] modify foo.txt <span class="keyword">in</span> original again</span><br><span class="line"> 1 file changed, 1 insertion(+), 1 deletion(-)</span><br></pre></td></tr></table></figure><p></p><p>现在使用<code>git push</code>向裸版本库提交这次修改,push需要指定目标版本库路径以及其分支。</p><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">$ git push ../first-project-bare/ master</span><br><span class="line">Counting objects: 3, <span class="keyword">done</span>.</span><br><span class="line">Compressing objects: 100% (2/2), <span class="keyword">done</span>.</span><br><span class="line">Writing objects: 100% (3/3), 316 bytes | 0 bytes/s, <span class="keyword">done</span>.</span><br><span class="line">Total 3 (delta 0), reused 0 (delta 0)</span><br><span class="line">To ../first-project-bare/</span><br><span class="line"> 8747b24..31e1f6f master -> master</span><br></pre></td></tr></table></figure><p></p><p>如果另一位开发者在我们之前就已经push了修改,那此次的push操作将会被拒绝,必须先pull回修改,才能push我们的修改。</p><h3 id="使用pull从裸版本库取回修改"><a href="#使用pull从裸版本库取回修改" class="headerlink" title="使用pull从裸版本库取回修改"></a>使用pull从裸版本库取回修改</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ <span class="built_in">cd</span> ../first-project</span><br><span class="line">$ git pull ../first-project-bare/ master</span><br><span class="line">remote: Counting objects: 3, <span class="keyword">done</span>.</span><br><span class="line">remote: Compressing objects: 100% (2/2), <span class="keyword">done</span>.</span><br><span class="line">remote: Total 3 (delta 0), reused 0 (delta 0)</span><br><span class="line">Unpacking objects: 100% (3/3), <span class="keyword">done</span>.</span><br><span class="line">From ../first-project-bare</span><br><span class="line"> * branch master -> FETCH_HEAD</span><br><span class="line">Updating 8747b24..31e1f6f</span><br><span class="line">Fast-forward</span><br><span class="line"> foo.txt | 2 +-</span><br><span class="line"> 1 file changed, 1 insertion(+), 1 deletion(-)</span><br></pre></td></tr></table></figure><script>$(".post-body a").not(".thispage").addClass("ignore-href").attr("target","_blank")</script></div><div></div><div><div style="padding:10px 0;margin:20px auto;width:90%;text-align:center;color:#878787" id="reward-div"><div>请作者喝瓶肥宅水🥤</div><button id="rewardButton" style="margin-top:10px" disable="enable" onclick='var e=document.getElementById("QR");"none"===e.style.display?e.style.display="block":e.style.display="none"'><span style="height:46px;width:46px;line-height:46px;border-radius:50%;color:#fe5f55;font-weight:600;background:#ffd5be;border:none;box-shadow:0 4px 8px 0 rgba(255,213,190,.4)">¥</span></button><div id="QR" style="display:none"><div id="wechat" style="display:inline-block"><img id="wechat_qr" src="/img/wechat_pay.png" alt="MrBird WeChat Pay"></div><div id="alipay" style="display:inline-block"><img id="alipay_qr" src="/img/ali_pay.png" alt="MrBird Alipay"></div></div></div><style>#QR img{width:auto;margin:.8em 1em 0 1em}</style></div><div><ul class="post-copyright"><li class="post-copyright-author"><strong>本文作者:</strong> MrBird</li><li class="post-copyright-link"><strong>本文链接:</strong> <a href="http://mrbird.cc/Git使用入门.html" title="Git使用入门">http://mrbird.cc/Git使用入门.html</a></li><li class="post-copyright-license"><strong>版权声明: </strong>本博客所有文章除特别声明外,均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明出处!</li></ul></div><footer class="post-footer"><div class="post-tags" style="margin-bottom:1.3rem"><a href="/tags/Git/" rel="tag"># Git</a></div><div class="post-nav"><div class="post-nav-next post-nav-item"><a href="/jQuery中的Deferred和Promise.html" rel="next" title="jQuery中的Deferred和Promise"><i class="fa fa-chevron-left"></i> jQuery中的Deferred和Promise</a></div><span class="post-nav-divider"></span><div class="post-nav-prev post-nav-item"><a href="/Leanote博客添加过渡动画.html" rel="prev" title="Leanote博客添加过渡动画">Leanote博客添加过渡动画 <i class="fa fa-chevron-right"></i></a></div></div></footer></article><hr><div id="container"></div><div class="post-spread"><div id="comment-div"></div><style>.valine .vlist{margin-bottom:3rem}.valine .vwrap .vcontrol .col.col-60{text-align:left}.valine .vlist .vcard .vhead,.valine .vlist .vcard section .vfooter{text-align:left}.valine .vlist .vcard section{padding-bottom:.5rem!important}.vname{color:#6db33f!important}div#comment-div{margin-bottom:-8rem}.valine .vlist .vcard .vcontent .code,.valine .vlist .vcard .vcontent code,.valine .vlist .vcard .vcontent pre{background:#fbfbfb}.valine .vlist .vcard .vcontent a{color:#6db33f}.valine .vlist .vcard .vimg{border-radius:3px}.valine .vinfo{text-align:left}.valine .vbtn{border-radius:2px;padding:.3rem 1.25rem}.valine .vbtn:active,.valine .vbtn:hover{color:#6db33f;border-color:#6db33f;background-color:#fff}.valine .vwrap .vheader .vinput:focus{border-bottom-color:#6db33f}.valine .vlist .vcard .vcontent.expand:before{background:-webkit-gradient(linear,left top,left bottom,from(hsla(0,0%,100%,0)),to(hsla(0,0%,100%,.2)));background:linear-gradient(180deg,hsla(0,0%,100%,0),hsla(0,0%,100%,.2))}.valine .vlist .vcard .vcontent.expand:after{content:"点击展开";font-size:.4rem;text-align:right;left:-1rem;background:hsla(0,0%,100%,.2)}.valine .vlist .vcard section .vfooter .vat{color:#b3b3b3}.valine .vlist .vcard section .vfooter .vat:hover{color:#6db33f}.vcontent img{margin:0}.valine .info{display:none}</style><script type="text/javascript" src="/js/av.min.js"></script><script type="text/javascript" src="/js/Valine.min.js"></script><script type="text/javascript" src="/js/activate-power-mode.js"></script><script>POWERMODE.colorful=!0,POWERMODE.shake=!1,document.body.addEventListener("input",POWERMODE),new Valine({el:"#comment-div",notify:!1,verify:!0,appId:"SMcDFP1bN1jgb9Lo8JmtICHm-gzGzoHsz",appKey:"dH4nrUrt3V5XiJiI6Qyejnbi",placeholder:"",path:window.location.pathname,avatar:"monsterid",guest_info:["nick","mail","link"]});var chicken='<a href="#"><img src="https://image.uisdc.com/wp-content/uploads/2018/06/uisdc-chat-chicken.gif" class="checken"></a>';$("#comment-div").prepend(chicken)</script></div></div><script>var $bqinline=$("img[alt='bq-inline']");$bqinline.css({width:"2.3rem",height:"2.3rem",display:"inline","vertical-align":"text-bottom"})</script></div><div class="comments" id="comments"></div></div><aside id="sidebar" class="sidebar" onselectstart="return!1"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">Contents</li><li class="sidebar-nav-overview" data-target="site-overview">Site Preview</li></ul><section class="site-overview sidebar-panel"><div class="sidebar-sticky sticky"><div itemscope itemtype="https://mrbird.cc"><div class="author__header"><div class="author__avatar"><img src="/images/blogImage.jpg" class="author__avatar" alt="MrBird" itemprop="image"></div><div class="author__content"><div class="author__name" itemprop="name">MrBird</div><p class="author__bio" itemprop="description">A simple blog, code repository, just keep blogging</p></div><div class="author__count"><a href="/archives" class="ignore-href"><span class="count">14</span> <span>Archives</span> </a><a href="/tags" class="ignore-href"><span class="count">2</span> <span>Labels</span></a></div></div><div class="author__urls-wrapper"><ul class="author__urls social-icons"><li><a href="/" itemprop="url" class="ignore-href">🏠 Home</a></li><li><a href="/archives" itemprop="url" class="ignore-href">📦 Archives</a></li><li><a href="/tags" itemprop="url" class="ignore-href">🔖 Labels</a></li><li><a href="/friends" itemprop="url" class="ignore-href">👬 Friends</a></li><li><a href="javascript:;" class="popup-trigger animsition-link">🔍 Search</a></li></ul></div></div></div></section><section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active"><div class="post-toc"><div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#准备Git环境"><span class="nav-number">1.</span> <span class="nav-text">准备Git环境</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建Git项目"><span class="nav-number">2.</span> <span class="nav-text">创建Git项目</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建版本库"><span class="nav-number">2.1.</span> <span class="nav-text">创建版本库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#首次提交"><span class="nav-number">2.2.</span> <span class="nav-text">首次提交</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#检查状态"><span class="nav-number">2.3.</span> <span class="nav-text">检查状态</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#提交修改"><span class="nav-number">2.4.</span> <span class="nav-text">提交修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#显示历史"><span class="nav-number">2.5.</span> <span class="nav-text">显示历史</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Git协作功能"><span class="nav-number">3.</span> <span class="nav-text">Git协作功能</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#克隆版本库"><span class="nav-number">3.1.</span> <span class="nav-text">克隆版本库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#从另一个版本库中获取修改"><span class="nav-number">3.2.</span> <span class="nav-text">从另一个版本库中获取修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#从任意版本库中取回修改"><span class="nav-number">3.3.</span> <span class="nav-text">从任意版本库中取回修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建共享版本库"><span class="nav-number">3.4.</span> <span class="nav-text">创建共享版本库</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用push向裸版本库提交修改"><span class="nav-number">3.5.</span> <span class="nav-text">使用push向裸版本库提交修改</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用pull从裸版本库取回修改"><span class="nav-number">3.6.</span> <span class="nav-text">使用pull从裸版本库取回修改</span></a></li></ol></li></ol></div></div></section></div><div id="asider-footer"><div id="links"><li><a href="https://love.mrbird.cc" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-diamond" aria-hidden="true"></i></a></li><li><a href="https://cloud.mrbird.cn" target="_blank" itemprop="url" class="ignore-href"><i class="fa fa-fw fa-skyatlas" aria-hidden="true"></i></a></li><li><a href="https://gitee.com/mrbirdd" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-codepen" aria-hidden="true"></i></a></li><li><a href="https://github.com/wuyouzhuguli" target="_blank" itemprop="sameAs" class="ignore-href"><i class="fa fa-fw fa-github-alt" aria-hidden="true"></i></a></li></div><div id="author"></div><script type="text/javascript">var $smsheoschzd100dn="@ 2016 - "+(new Date).getFullYear()+" MrBird";document.getElementById("author").innerHTML=$smsheoschzd100dn</script><div><script type="text/javascript" src="/js/busuanzi.js"></script> UV <span class="busuanzi-value" id="busuanzi_value_site_uv" style="cursor:pointer" title="统计开始时间:2019年7月5日"></span> PV <span class="busuanzi-value" id="busuanzi_value_site_pv" style="cursor:pointer" title="统计开始时间:2019年7月5日"></span></div></div><script>function c__(){var o=sidebar_nav_toc.attr("class");o.indexOf("active")!=-1?footer.hide(300):footer.show(300)}var sidebar_nav_toc=$(".sidebar-nav-toc"),footer=$("#asider-footer");c__(),$(".sidebar-nav").on("click",function(){c__()})</script></aside></div></main><div class="back-to-top"><span id="scrollpercent"><span>0</span></span></div></div><script type="text/javascript">"[object Function]"!==Object.prototype.toString.call(window.Promise)&&(window.Promise=null)</script><script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script><script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script><script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script><script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script><script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script><script type="text/javascript" src="/js/src/utils.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/motion.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/post-details.js?v=5.1.1"></script><script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.1"></script><script type="text/javascript">function proceedsearch(){$("body").append('<div class="search-popup-overlay local-search-pop-overlay"></div>').css("overflow","hidden"),$(".search-popup-overlay").click(onPopupClose),$(".popup").toggle();var t=$("#local-search-input");t.attr("autocapitalize","none"),t.attr("autocorrect","off"),t.focus()}var isfetched=!1,isXml=!0,search_path="search.xml";0===search_path.length?search_path="search.xml":search_path.endsWith("json")&&(isXml=!1);var path="/"+search_path,onPopupClose=function(t){$(".popup").hide(),$("#local-search-input").val(""),$(".search-result-list").remove(),$("#no-result").remove(),$(".local-search-pop-overlay").remove(),$("body").css("overflow","")},searchFunc=function(t,e,o){"use strict";$("body").append('<div class="search-popup-overlay local-search-pop-overlay"><div id="search-loading-icon"><i class="fa fa-spinner fa-pulse fa-2x fa-fw"></i></div></div>').css("overflow","hidden"),$("#search-loading-icon").css("margin","20% auto 0 auto").css("text-align","center"),$.ajax({url:t,dataType:isXml?"xml":"json",async:!0,success:function(t){isfetched=!0,$(".popup").detach().appendTo(".header-inner");var n=isXml?$("entry",t).map(function(){return{title:$("title",this).text(),content:$("content",this).text(),url:$("url",this).text()}}).get():t,r=document.getElementById(e),s=document.getElementById(o),a=function(){var t=r.value.trim().toLowerCase(),e=t.split(/[\s\-]+/);e.length>1&&e.push(t);var o=[];if(t.length>0&&n.forEach(function(n){function r(e,o,n,r){for(var s=r[r.length-1],a=s.position,i=s.word,l=[],h=0;a+i.length<=n&&0!=r.length;){i===t&&h++,l.push({position:a,length:i.length});var p=a+i.length;for(r.pop();0!=r.length&&(s=r[r.length-1],a=s.position,i=s.word,p>a);)r.pop()}return c+=h,{hits:l,start:o,end:n,searchTextCount:h}}function s(t,e){var o="",n=e.start;return e.hits.forEach(function(e){o+=t.substring(n,e.position);var r=e.position+e.length;o+='<b class="search-keyword">'+t.substring(e.position,r)+"</b>",n=r}),o+=t.substring(n,e.end)}var a=!1,i=0,c=0,l=n.title.trim(),h=l.toLowerCase(),p=n.content.trim().replace(/<[^>]+>/g,""),u=p.toLowerCase(),f=decodeURIComponent(n.url),d=[],g=[];if(""!=l&&(e.forEach(function(t){function e(t,e,o){var n=t.length;if(0===n)return[];var r=0,s=[],a=[];for(o||(e=e.toLowerCase(),t=t.toLowerCase());(s=e.indexOf(t,r))>-1;)a.push({position:s,word:t}),r=s+n;return a}d=d.concat(e(t,h,!1)),g=g.concat(e(t,u,!1))}),(d.length>0||g.length>0)&&(a=!0,i=d.length+g.length)),a){[d,g].forEach(function(t){t.sort(function(t,e){return e.position!==t.position?e.position-t.position:t.word.length-e.word.length})});var v=[];0!=d.length&&v.push(r(l,0,l.length,d));for(var C=[];0!=g.length;){var $=g[g.length-1],m=$.position,x=$.word,w=m-20,y=m+80;w<0&&(w=0),y<m+x.length&&(y=m+x.length),y>p.length&&(y=p.length),C.push(r(p,w,y,g))}C.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hits.length!==e.hits.length?e.hits.length-t.hits.length:t.start-e.start});var T=parseInt("1");T>=0&&(C=C.slice(0,T));var b="";b+=0!=v.length?"<li><a href='"+f+"' class='search-result-title'>"+s(l,v[0])+"</a>":"<li><a href='"+f+"' class='search-result-title'>"+l+"</a>",C.forEach(function(t){b+="<a href='"+f+'\'><p class="search-result">'+s(p,t)+"...</p></a>"}),b+="</li>",o.push({item:b,searchTextCount:c,hitCount:i,id:o.length})}}),1===e.length&&""===e[0])s.innerHTML='<div id="no-result"><i class="fa fa-search fa-5x" /></div>';else if(0===o.length)s.innerHTML='<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>';else{o.sort(function(t,e){return t.searchTextCount!==e.searchTextCount?e.searchTextCount-t.searchTextCount:t.hitCount!==e.hitCount?e.hitCount-t.hitCount:e.id-t.id});var a='<ul class="search-result-list">';o.forEach(function(t){a+=t.item}),a+="</ul>",s.innerHTML=a}};r.addEventListener("input",a),$(".local-search-pop-overlay").remove(),$("body").css("overflow",""),proceedsearch()}})};$(".popup-trigger").click(function(t){t.stopPropagation(),isfetched===!1?searchFunc(path,"local-search-input","local-search-result"):proceedsearch()}),$(".popup-btn-close").click(onPopupClose),$(".popup").click(function(t){t.stopPropagation()}),$(document).on("keyup",function(t){var e=27===t.which&&$(".search-popup").is(":visible");e&&onPopupClose()})</script></body><script>$(function(){$("a").not(".nav-link,.cloud-tie-join-count,.ignore-href,.jstree-anchor").addClass("animsition-link")});var burst1=new mojs.Burst({left:0,top:0,radius:{5:40},children:{shape:"circle",fill:["red","cyan","orange"],fillOpacity:.8,radiusX:3.5,radiusY:3.5}});document.addEventListener("click",function(a){null==a.target.href&&"footer"!=a.target.className&&"copyright"!=a.target.className&&"author__urls social-icons"!=a.target.className&&"author__avatar"!=a.target.className&&"sidebar sidebar-active"!=a.target.className&&burst1.tune({x:a.pageX,y:a.pageY}).generate().replay()})</script><script type="text/javascript" src="/js/message.js"></script></html><!-- rebuild by neat -->