1
0
wiki/Tech/getting-started/版本控制/Git/进阶/参考手册.html

330 lines
475 KiB
HTML
Raw Normal View History

2024-08-05 18:05:18 +08:00
<!DOCTYPE html>
<html><head><title>参考手册</title><meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="参考手册"/><meta property="og:description" content="Git 参考手册."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../../static/icon.png"/><meta name="description" content="Git 参考手册."/><meta name="generator" content="Quartz"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
(function() {
window.dataLayer = window.dataLayer || [];
function gtag() {
window.dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'G-MHMEL0F832');
})();
</script><link href="../../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Schibsted Grotesk:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch(`../../../../../static/contentIndex.json`).then(data => data.json())</script></head><body data-slug="Tech/getting-started/版本控制/Git/进阶/参考手册"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35;" xmlSpace="preserve"><title>Light mode</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447-1
<p>有两种方式获取一个 Git 仓库,仓库是 Git 存放你要保存的快照的数据的地方。</p>
<ul>
<li>初始化一个新的 Git 仓库。</li>
<li>克隆一个存在的项目。</li>
</ul>
<h3 id="git-init">Git Init<a aria-hidden="true" tabindex="-1" href="#git-init" class="internal"> §</a></h3>
<p>在命令行当前目录中执行 git init就可以将当前目录初始化为 Git 仓库。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-l</span></span>
<span data-line><span style="color:var(--shiki-token-function);">total</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">init</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Initialized</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">empty</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">repository</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/root/gitStudy/.git/</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 在 /root/gitStudy/.git/ 目录初始化空 Git 仓库完毕。</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-a</span></span>
<span data-line><span style="color:var(--shiki-token-function);">.</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">..</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">.git</span></span></code></pre></div>
<h3 id="git-clone">Git Clone<a aria-hidden="true" tabindex="-1" href="#git-clone" class="internal"> §</a></h3>
<p>如果需要克隆一个存在的项目。,在命令行当前目录中执行 git clone [url]url 为项目地址然后就可以了。git clone 实际上是一个封装了其他几个命令的命令。</p>
<ol>
<li>它创建了一个新目录,切换到新的目录。</li>
<li>然后 git init 来初始化一个空的 Git 仓库, 然后为你指定的 URL 添加一个(默认名称为 origin 的)远程仓库。</li>
<li>再针对远程仓库执行 git fetch最后通过 git checkout 将远程仓库的最新提交检出到本地的工作目录。</li>
</ol>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">clone</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git://github.com/schacon/simplegit.git</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Initialized</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">empty</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">repository</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">in</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">/private/tmp/simplegit/.git/</span></span>
<span data-line><span style="color:var(--shiki-token-function);">remote:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Counting</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">objects:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-token-string);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">done.</span></span>
<span data-line><span style="color:var(--shiki-token-function);">remote:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Compressing</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">objects:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-token-string);">%</span><span style="color:var(--shiki-color-text);"> (86/86), done.</span></span>
<span data-line><span style="color:var(--shiki-token-function);">remote:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Total</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-color-text);"> (delta </span><span style="color:var(--shiki-token-constant);">35</span><span style="color:var(--shiki-color-text);">), reused 0 (</span><span style="color:var(--shiki-token-function);">delta</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-color-text);">)</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Receiving</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">objects:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-token-string);">%</span><span style="color:var(--shiki-color-text);"> (100/100), 9.51 KiB, done.</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Resolving</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">deltas:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100</span><span style="color:var(--shiki-token-string);">%</span><span style="color:var(--shiki-color-text);"> (35/35), done.</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">simplegit/</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span></span>
<span data-line><span style="color:var(--shiki-token-function);">README</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Rakefile</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">lib</span></span></code></pre></div>
<h2 id="快照基础">快照基础<a aria-hidden="true" tabindex="-1" href="#快照基础" class="internal"> §</a></h2>
<p>Git 的工作就是创建和保存项目的快照,及与之后的快照进行对比。</p>
<p>简而言之,使用 git add 添加需要追踪的新文件和待提交的更改, 然后使用 git status 和 git diff 查看有何改动, 最后用 git commit 将你的快照记录。绝大部分时候的基本流程都是这样的。</p>
<h3 id="git-add">Git Add<a aria-hidden="true" tabindex="-1" href="#git-add" class="internal"> §</a></h3>
<p>在 Git 中提交修改文件之前,需要将它们添加到缓存。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 添加 helloworld.cpp 至缓存区</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">helloworld.cpp</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 添加当前目录至缓存区</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">.</span></span></code></pre></div>
<h3 id="git-status">Git Status<a aria-hidden="true" tabindex="-1" href="#git-status" class="internal"> §</a></h3>
<p>在 Git 中执行 git status 命令,查看代码在缓存与当前工作目录的状态。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 简短输出</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 详细输出</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span></span></code></pre></div>
<h3 id="git-diff">Git Diff<a aria-hidden="true" tabindex="-1" href="#git-diff" class="internal"> §</a></h3>
<p>在 Git 中使用 git diff 描述已临时提交的或者已修改但尚未提交的改动。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 尚未缓存的改动</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">diff</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 查看已缓存的改动</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">diff</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--cached</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 查看已缓存的与未缓存的所有改动</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">diff</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">HEAD</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 显示摘要而非整个 diff</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">diff</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--stat</span></span></code></pre></div>
<h3 id="git-commit">Git Commit<a aria-hidden="true" tabindex="-1" href="#git-commit" class="internal"> §</a></h3>
<p>在 Git 中使用 git add 将内容写入缓存,然后执行 git commit 就将它实际存储快照了。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 配置提交信息</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">config</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--global</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">user.name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'Your Name'</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">config</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--global</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">user.email</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">you@somedomain.com</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 添加至缓存</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 输出状态摘要</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span></span>
<span data-line><span style="color:var(--shiki-token-function);">M</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 提交快照</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-m</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'my hola mundo changes'</span></span>
<span data-line><span style="color:var(--shiki-color-text);">[master </span><span style="color:var(--shiki-token-constant);">68</span><span style="color:var(--shiki-color-text);">aa034] my hola mundo changes</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">files</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">changed,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">insertions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">+</span><span style="color:var(--shiki-color-text);">)</span><span style="color:var(--shiki-token-string);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">deletions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">-</span><span style="color:var(--shiki-color-text);">)</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 使用 -S 标志提交签名加密</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-S</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-m</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'my hola mundo changes'</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 撤销上次提交</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--amend</span></span></code></pre></div>
<p>如果执行 git commit 不适用 -m 选项Git 则会尝试打开一个编辑器以填写提交信息。如果认为使用 git add 提交的流程太过繁琐Git 允许使用 -a 选项跳过这一步。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">vim</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">status</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">M</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-m</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'changes to hello file'</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># On branch master</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># Changed but not updated:</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># (use &quot;git add &lt;file>...&quot; to update what will be committed)</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># (use &quot;git checkout -- &lt;file>...&quot; to discard changes in working directory)</span></span>
<span data-line><span style="color:var(--shiki-token-comment);">#</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># modified: hello.rb</span></span>
<span data-line><span style="color:var(--shiki-token-comment);">#</span></span>
<span data-line><span style="color:var(--shiki-token-function);">no</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">changes</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">added</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">to</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> (use </span><span style="color:var(--shiki-token-string-expression);">&quot;git add&quot;</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">and/or</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;git commit -a&quot;</span><span style="color:var(--shiki-color-text);">)</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-am</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'changes to hello file'</span></span>
<span data-line><span style="color:var(--shiki-color-text);">[master </span><span style="color:var(--shiki-token-constant);">78</span><span style="color:var(--shiki-color-text);">b2670] changes to hello file</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">files</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">changed,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">insertions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">+</span><span style="color:var(--shiki-color-text);">)</span><span style="color:var(--shiki-token-string);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">deletions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">-</span><span style="color:var(--shiki-color-text);">)</span></span></code></pre></div>
<h3 id="git-reset-head">Git Reset HEAD<a aria-hidden="true" tabindex="-1" href="#git-reset-head" class="internal"> §</a></h3>
<p>git reset 命令用于回退版本,可以指定退回某一次提交的版本。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git reset HEAD^</span></span></code></pre></div>
<h3 id="git-rm">Git Rm<a aria-hidden="true" tabindex="-1" href="#git-rm" class="internal"> §</a></h3>
<p>通过执行 git rm 来删除 Git 追踪的文件,它还会删除你的工作目录中的相应文件。</p>
<h3 id="git-mv">Git Mv<a aria-hidden="true" tabindex="-1" href="#git-mv" class="internal"> §</a></h3>
<p>git mv 命令是一个便利命令,用于移到一个文件并且在新文件上执行 git add 命令及在老文件上执行 git</p>
<p>rm 命令。</p>
<h3 id="git-clean">Git Clean<a aria-hidden="true" tabindex="-1" href="#git-clean" class="internal"> §</a></h3>
<p>git clean 是一个用来从工作区中移除不想要的文件的命令。 可以是编译的临时文件或者合并冲突的文件。</p>
<h2 id="分支与合并">分支与合并<a aria-hidden="true" tabindex="-1" href="#分支与合并" class="internal"> §</a></h2>
<p>几乎每一种版本控制系统都以某种形式支持分支一个分支代表一条独立的开发线。使用分支意味着你可以从开发主线上分离开来然后在不影响主线的同时继续工作。Git 的分支模型称为<strong>必杀技特性</strong>,而正是因为它,将 Git 从版本控制系统家族里区分出来。</p>
<h3 id="git-branch">Git Branch<a aria-hidden="true" tabindex="-1" href="#git-branch" class="internal"> §</a></h3>
<p>在 Git 中列出、创建与管理工作上下文。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 列出可用的分支</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">branch</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 创建新分支</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">branch</span><span style="color:var(--shiki-color-text);"> (branchname)</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 删除分支</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">branch</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-d</span><span style="color:var(--shiki-color-text);"> (branchname)</span></span></code></pre></div>
<h3 id="git-switch">Git Switch<a aria-hidden="true" tabindex="-1" href="#git-switch" class="internal"> §</a></h3>
<p>在 Git 中切换至指定分支。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git switch master</span></span></code></pre></div>
<h3 id="git-checkout">Git Checkout<a aria-hidden="true" tabindex="-1" href="#git-checkout" class="internal"> §</a></h3>
<p>在 Git 中 git checkout 创建新分支,并立即切换到它</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git checkout -b (</span><span style="color:var(--shiki-token-function);">branchname</span><span style="color:var(--shiki-color-text);">) </span></span></code></pre></div>
<h3 id="git-merge">Git Merge<a aria-hidden="true" tabindex="-1" href="#git-merge" class="internal"> §</a></h3>
<p>一旦某分支有了独立内容,你终究会希望将它合并回到你的主分支。 你可以使用 <code>git merge</code> 命令将任何分支合并到当前分支中去。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">branch</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">*</span><span style="color:var(--shiki-color-text);"> master</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">removals</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span></span>
<span data-line><span style="color:var(--shiki-token-function);">README</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">more.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">test.txt</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">merge</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">removals</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Updating</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">8</span><span style="color:var(--shiki-token-string);">bd6d8b..8f7c949</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Fast-forward</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">more.txt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">test</span><span style="color:var(--shiki-color-text);">.txt </span><span style="color:var(--shiki-token-keyword);">|</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">files</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">changed,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">insertions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">+</span><span style="color:var(--shiki-color-text);">)</span><span style="color:var(--shiki-token-string);">,</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">deletions</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">-</span><span style="color:var(--shiki-color-text);">)</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">delete</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">mode</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100644</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">more.txt</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">delete</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">mode</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">100644</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">test.txt</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ls</span></span>
<span data-line><span style="color:var(--shiki-token-function);">README</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">hello.rb</span></span></code></pre></div>
<h3 id="git-stash">Git Stash<a aria-hidden="true" tabindex="-1" href="#git-stash" class="internal"> §</a></h3>
<p>git stash 命令用来临时地保存一些还没有提交的工作,以便在分支上不需要提交未完成工作就可以清理工作</p>
<p>目录。</p>
<h3 id="git-log">Git Log<a aria-hidden="true" tabindex="-1" href="#git-log" class="internal"> §</a></h3>
<p>在使用 Git 提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,我们可以使用 git log 命令查看。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">log</span></span>
<span data-line><span style="color:var(--shiki-token-function);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">8</span><span style="color:var(--shiki-token-string);">d585ea6faf99facd39b55d6f6a3b3f481ad0d3d</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Merge:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-string);">cbb6aa</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-string);">ac015d</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Author:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Scott</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Chacon</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">schacon@gmail.co</span><span style="color:var(--shiki-color-text);">m</span><span style="color:var(--shiki-token-keyword);">></span></span>
<span data-line><span style="color:var(--shiki-token-function);">Date:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Fri</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Jun</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">12</span><span style="color:var(--shiki-token-string);">:59:47</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2010</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">+0200</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">Merge</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">branch</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'fix_readme'</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">Conflicts:</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">README</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-function);">commit</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-token-string);">cbb6aae5c0cbd711c098e113ae436801371c95e</span></span>
<span data-line><span style="color:var(--shiki-token-function);">Author:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Scott</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Chacon</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">schacon@gmail.co</span><span style="color:var(--shiki-color-text);">m</span><span style="color:var(--shiki-token-keyword);">></span></span>
<span data-line><span style="color:var(--shiki-token-function);">Date:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Fri</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">Jun</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">12</span><span style="color:var(--shiki-token-string);">:58:53</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2010</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">+0200</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">fixed</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">readme</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">title</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">differently</span></span>
<span data-line> </span>
<span data-line><span style="color:var(--shiki-token-function);">...</span></span></code></pre></div>
<h3 id="git-tag">Git Tag<a aria-hidden="true" tabindex="-1" href="#git-tag" class="internal"> §</a></h3>
<p>如果你达到一个重要的阶段,并希望永远记住那个特别的提交快照,你可以使用 <code>git tag</code> 给它打上标签。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tag</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-a</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">v1.0</span><span style="color:var(--shiki-color-text);"> </span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 指定提交打标签</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tag</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-a</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">v0.9</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">558151</span><span style="color:var(--shiki-token-string);">a</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 指定标签信息命令</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tag</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-a</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">tagnam</span><span style="color:var(--shiki-color-text);">e</span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-m</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;runoob.com标签&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># PGP签名标签命令</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tag</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-s</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">tagnam</span><span style="color:var(--shiki-color-text);">e</span><span style="color:var(--shiki-token-keyword);">></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">-m</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;runoob.com标签&quot;</span></span></code></pre></div>
<h2 id="项目分享与更新">项目分享与更新<a aria-hidden="true" tabindex="-1" href="#项目分享与更新" class="internal"> §</a></h2>
<p>Git 并不像 Subversion 那样有个中心服务器。 目前为止所有的命令都是本地执行的,更新的知识本地的数据库。简而言之使用 git fetch 更新你的项目,使用 git push 分享你的改动。 你可以用 git remote 管理你的远程仓库。</p>
<h3 id="git-remote">Git Remote<a aria-hidden="true" tabindex="-1" href="#git-remote" class="internal"> §</a></h3>
<p>在 Git 中 git remote 命令管理远端仓库列表。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);"># 列出远端别名</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">remote</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 为项目添加一个新的远端仓库</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">remote</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">add</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">remote-nam</span><span style="color:var(--shiki-color-text);">e</span><span style="color:var(--shiki-token-keyword);">></span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 删除现存的某个别名</span></span>
<span data-line><span style="color:var(--shiki-token-function);">$</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">remote</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">rm</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-token-string);">remote-nam</span><span style="color:var(--shiki-color-text);">e</span><span style="color:var(--shiki-token-keyword);">></span></span></code></pre></div>
<h3 id="git-fetch">Git Fetch<a aria-hidden="true" tabindex="-1" href="#git-fetch" class="internal"> §</a></h3>
<p>在 Git 中 git fetch 从远端仓库<strong>下载新分支与数据</strong></p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git fetch </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-color-text);">remote-name</span><span style="color:var(--shiki-token-keyword);">></span></span></code></pre></div>
<h3 id="git-pull">Git Pull<a aria-hidden="true" tabindex="-1" href="#git-pull" class="internal"> §</a></h3>
<p>在 Git 中 **git pull 从远端仓库提取数据并尝试合并到当前分支。**该命令就是在 git fetch 之后紧接着 git merge 远端分支到你所在的任意分支。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git pull </span><span style="color:var(--shiki-token-keyword);">&lt;</span><span style="color:var(--shiki-color-text);">remote-name</span><span style="color:var(--shiki-token-keyword);">></span></span></code></pre></div>
<h3 id="git-push">Git Push<a aria-hidden="true" tabindex="-1" href="#git-push" class="internal"> §</a></h3>
<p>在 Git 中 git push remote-name branch-name 将本地改动推送到远端仓库。 如果可以的话,它会依据你的 branch 的样子,推送到远端的 branch 去。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">$git push github master</span></span></code></pre></div>
<h3 id="git-archive">Git Archive<a aria-hidden="true" tabindex="-1" href="#git-archive" class="internal"> §</a></h3>
<p>git archive 命令用来创建项目一个指定快照的归档文件。</p>
<h3 id="git-submodule">Git Submodule<a aria-hidden="true" tabindex="-1" href="#git-submodule" class="internal"> §</a></h3>
<p>git submodule 命令用来管理一个仓库的其他外部仓库。</p>
<h2 id="检查与比较">检查与比较<a aria-hidden="true" tabindex="-1" href="#检查与比较" class="internal"> §</a></h2>
<h3 id="git-show">Git Show<a aria-hidden="true" tabindex="-1" href="#git-show" class="internal"> §</a></h3>
<p>git show 命令可以以一种简单的人类可读的方式来显示一个 Git 对象。 一般使用此命令来显示一个标签或一</p>
<p>个提交的信息。</p>
<h3 id="git-shortlog">Git Shortlog<a aria-hidden="true" tabindex="-1" href="#git-shortlog" class="internal"> §</a></h3>
<p>git shortlog 是一个用来归纳 git log 的输出的命令。 它可以接受很多与 git log 相同的选项,但是此命</p>
<p>令并不会列出所有的提交,而是展示一个根据作者分组的提交记录的概括性信息</p>
<h3 id="git-describe">Git Describe<a aria-hidden="true" tabindex="-1" href="#git-describe" class="internal"> §</a></h3>
<p>git describe 命令用来接受任何可以解析成一个提交的东西,然后生成一个人类可读的字符串且不可变。</p>
<h2 id="调试">调试<a aria-hidden="true" tabindex="-1" href="#调试" class="internal"> §</a></h2>
<h3 id="git-bisect">Git Bisect<a aria-hidden="true" tabindex="-1" href="#git-bisect" class="internal"> §</a></h3>
<p>git bisect 工具是一个非常有用的调试工具,它通过自动进行一个二分查找来找到哪一个特定的提交是导致</p>
<p>bug 或者问题的第一个提交。</p>
<h3 id="git-blame">Git Blame<a aria-hidden="true" tabindex="-1" href="#git-blame" class="internal"> §</a></h3>
<p>git blame 命令标注任何文件的行,指出文件的每一行的最后的变更的提交及谁是那一个提交的作者。 当你要</p>
<p>找那个人去询问关于这块特殊代码的信息时这会很有用。</p>
<h3 id="git-grep">Git Grep<a aria-hidden="true" tabindex="-1" href="#git-grep" class="internal"> §</a></h3>
<p>git grep 命令可以帮助在源代码中,甚至是你项目的老版本中的任意文件中查找任何字符串或者正则表达式。</p>
<h2 id="补丁">补丁<a aria-hidden="true" tabindex="-1" href="#补丁" class="internal"> §</a></h2>
<h3 id="git-cherry-pick">Git Cherry-pick<a aria-hidden="true" tabindex="-1" href="#git-cherry-pick" class="internal"> §</a></h3>
<p>git cherry-pick 命令用来获得在单个提交中引入的变更,然后尝试将作为一个新的提交引入到你当前分支</p>
<p>上。 从一个分支单独一个或者两个提交而不是合并整个分支的所有变更是非常有用的。</p>
<h3 id="git-rebase">Git Rebase<a aria-hidden="true" tabindex="-1" href="#git-rebase" class="internal"> §</a></h3>
<p>git rebase 命令基本是是一个自动化的 cherry-pick 命令。 它计算出一系列的提交,然后再以它们在其他</p>
<p>地方以同样的顺序一个一个的 cherry-picks 出它们。</p>
<h3 id="git-revert">Git Revert<a aria-hidden="true" tabindex="-1" href="#git-revert" class="internal"> §</a></h3>
<p>git revert 命令本质上就是一个逆向的 git cherry-pick 操作。 它将你提交中的变更的以完全相反的方式</p>
<p>的应用到一个新创建的提交中,本质上就是撤销或者倒转。</p>
<h2 id="邮件">邮件<a aria-hidden="true" tabindex="-1" href="#邮件" class="internal"> §</a></h2>
<h3 id="git-apply">Git Apply<a aria-hidden="true" tabindex="-1" href="#git-apply" class="internal"> §</a></h3>
<p>git apply 命令应用一个通过 git diff 或者甚至使用 GNU diff 命令创建的补丁。 它跟补丁命令做了差不多</p>
<p>的工作,但还是有一些小小的差别。</p>
<h3 id="git-am">Git Am<a aria-hidden="true" tabindex="-1" href="#git-am" class="internal"> §</a></h3>
<p>git am 命令用来应用来自邮箱的补丁。特别是那些被 mbox 格式化过的。 这对于通过邮件接受补丁并将他们</p>
<p>轻松地应用到你的项目中很有用。</p>
<h3 id="git-format-patch">Git Format-patch<a aria-hidden="true" tabindex="-1" href="#git-format-patch" class="internal"> §</a></h3>
<p>git format-patch 命令用来以 mbox 的格式来生成一系列的补丁以便你可以发送到一个邮件列表中。</p>
<h3 id="git-imap-send">Git Imap-send<a aria-hidden="true" tabindex="-1" href="#git-imap-send" class="internal"> §</a></h3>
<p>git imap-send 将一个由 git format-patch 生成的邮箱上传至 IMAP 草稿文件夹。</p>
<h3 id="git-send-email">Git Send-email<a aria-hidden="true" tabindex="-1" href="#git-send-email" class="internal"> §</a></h3>
<p>git send-mail 命令用来通过邮件发送那些使用 git format-patch 生成的补丁。</p>
<h3 id="git-request-pull">Git Request-pull<a aria-hidden="true" tabindex="-1" href="#git-request-pull" class="internal"> §</a></h3>
<p>git request-pull 命令只是简单的用来生成一个可通过邮件发送给某个人的示例信息体。 如果你在公共服务</p>
<p>器上有一个分支,并且想让别人知道如何集成这些变更,而不用通过邮件发送补丁,你就可以执行此命令的输出</p>
<p>发送给这个你想拉取变更的人。</p>
<h2 id="外部系统">外部系统<a aria-hidden="true" tabindex="-1" href="#外部系统" class="internal"> §</a></h2>
<h3 id="git-svn">Git Svn<a aria-hidden="true" tabindex="-1" href="#git-svn" class="internal"> §</a></h3>
<p>git svn 可以使 Git 作为一个客户端来与 Subversion 版本控制系统通信。 这意味着你可以使用 Git 来检出内</p>
<p>容,或者提交到 Subversion 服务器。</p>
<h3 id="git-fast-import">Git Fast-import<a aria-hidden="true" tabindex="-1" href="#git-fast-import" class="internal"> §</a></h3>
<p>对于其他版本控制系统或者从其他任何的格式导入,你可以使用 git fast-import 快速地将其他格式映射到</p>
<p>Git 可以轻松记录的格式。</p>
<h2 id="管理">管理<a aria-hidden="true" tabindex="-1" href="#管理" class="internal"> §</a></h2>
<h3 id="git-gc">Git Gc<a aria-hidden="true" tabindex="-1" href="#git-gc" class="internal"> §</a></h3>
<p>git gc 命令在你的仓库中执行 “garbage collection”删除数据库中不需要的文件和将其他文件打包成一种</p>
<p>更有效的格式。</p>
<h3 id="git-fsck">Git Fsck<a aria-hidden="true" tabindex="-1" href="#git-fsck" class="internal"> §</a></h3>
<p>git fsck 命令用来检查内部数据库的问题或者不一致性。</p>
<h3 id="git-reflog">Git Reflog<a aria-hidden="true" tabindex="-1" href="#git-reflog" class="internal"> §</a></h3>
<p>git reflog 命令分析你所有分支的头指针的日志来查找出你在重写历史上可能丢失的提交。</p>
<h3 id="git-filter-branch">Git Filter-branch<a aria-hidden="true" tabindex="-1" href="#git-filter-branch" class="internal"> §</a></h3>
<p>git filter-branch 命令用来根据某些规则来重写大量的提交记录,例如从任何地方删除文件,或者通过过</p>
<p>滤一个仓库中的一个单独的子目录以提取出一个项目。</p></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content"><ul class="overflow"><li class="depth-0"><a href="#创建与克隆项目" data-for="创建与克隆项目">创建与克隆项目</a></li><li class="depth-1"><a href="#git-init" data-for="git-init">Git Init</a></li><li class="depth-1"><a href="#git-clone" data-for="git-clone">Git Clone</a></li><li class="depth-0"><a href="#快照基础" data-for="快照基础">快照基础</a></li><li class="depth-1"><a href="#git-add" data-for="git-add">Git Add</a></li><li class="depth-1"><a href="#git-status" data-for="git-status">Git Status</a></li><li class="depth-1"><a href="#git-diff" data-for="git-diff">Git Diff</a></li><li class="depth-1"><a href="#git-commit" data-for="git-commit">Git Commit</a></li><li class="depth-1"><a href="#git-reset-head" data-for="git-reset-head">Git Reset HEAD</a></li><li class="depth-1"><a href="#git-rm" data-for="git-rm">Git Rm</a></li><li class="depth-1"><a href="#git-mv" data-for="git-mv">Git Mv</a></li><li class="depth-1"><a href="#git-clean" data-for="git-clean">Git Clean</a></li><li class="depth-0"><a href="#分支与合并" data-for="分支与合并">分支与合并</a></li><li class="depth-1"><a href="#git-branch" data-for="git-branch">Git Branch</a></li><li class="depth-1"><a href="#git-switch" data-for="git-switch">Git Switch</a></li><li class="depth-1"><a href="#git-checkout" data-for="git-checkout">Git Checkout</a></li><li class="depth-1"><a href="#git-merge" data-for="git-merge">Git Merge</a></li><li class="depth-1"><a href="#git-stash" data-for="git-stash">Git Stash</a></li><li class="depth-1"><a href="#git-log" data-for="git-log">Git Log</a></li><li class="depth-1"><a href="#git-tag" data-for="git-tag">Git Tag</a></li><li class="depth-0"><a href="#项目分享与更新" data-for="项目分享与更新">项目分享与更新</a></li><li class="depth-1"><a href="#git-remote" data-for="git-remote">Git Remote</a></li><li class="depth-1"><a href="#git-fetch" data-for="git-fetch">Git Fetch</a></li><li class="depth-1"><a href="#git-pull" data-for="git-pull">Git Pull</a></li><li class="depth-1"><a href="#git-push" data-for="git-push">Git Push</a></li><li class="depth-1"><a href="#git-archive" data-for="git-archive">Git Archive</a></li><li class="depth-1"><a href="#git-submodule" data-for="git-submodule">Git Submodule</a></li><li class="depth-0"><a href="#检查与比较" data-for="检查与比较">检查与比较</a></li><li class="depth-1"><a href="#git-show" data-for="git-show">Git Show</a></li><li class="depth-1"><a href="#git-shortlog" data-for="git-shortlog">Git Shortlog</a></li><li class="depth-1"><a href="#git-describe" data-for="git-describe">Git Describe</a></li><li class="depth-0"><a href="#调试" data-for="调试">调试</a></li><li class="depth-1"><a href="#git-bisect" data-for="git-bisect">Git Bisect</a></li><li class="depth-1"><a href="#git-blame" data-for="git-blame">Git Blame</a></li><li class="depth-1"><a href="#git-grep" data-for="git-grep">Git Grep</a></li><li class="depth-0"><a href="#补丁" data-for="补丁">补丁</a></li><li class="depth-1"><a href="#git-cherry-pick" data-for="git-cherry-pick">Git Cherry-pick</a></li><li class="depth-1"><a href="#git-rebase" data-for="git-rebase">Git Rebase</a></li><li class="depth-1"><
function toggleCallout() {
const outerBlock = this.parentElement;
outerBlock.classList.toggle(`is-collapsed`);
const collapsed = outerBlock.classList.contains(`is-collapsed`);
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
outerBlock.style.maxHeight = height + `px`;
let current = outerBlock;
let parent = outerBlock.parentElement;
while (parent) {
if (!parent.classList.contains(`callout`)) {
return;
}
const collapsed2 = parent.classList.contains(`is-collapsed`);
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
parent.style.maxHeight = height2 + `px`;
current = parent;
parent = parent.parentElement;
}
}
function setupCallout() {
const collapsible = document.getElementsByClassName(
`callout is-collapsible`
);
for (const div of collapsible) {
const title = div.firstElementChild;
if (title) {
title.removeEventListener(`click`, toggleCallout);
title.addEventListener(`click`, toggleCallout);
const collapsed = div.classList.contains(`is-collapsed`);
const height = collapsed ? title.scrollHeight : div.scrollHeight;
div.style.maxHeight = height + `px`;
}
}
}
document.addEventListener(`nav`, setupCallout);
window.addEventListener(`resize`, setupCallout);
</script><script type="module">
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
});
document.addEventListener('nav', async () => {
await mermaid.run({
querySelector: '.mermaid'
})
});
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../../postscript.js" type="module"></script></html>