1
0
wiki/Technology/GettingStarted/版本控制/Git/进阶/常用技巧.html

370 lines
510 KiB
HTML
Raw Normal View History

2024-09-03 21:23:28 +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="Technology/GettingStarted/版本控制/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.
<p>Git 配置文件分为三级系统级—system、用户级—global和目录级—local三者的使用优先级以离目录repository最近为原则如果三者的配置不一样则生效优先级 <strong>目录级>用户级>系统级</strong></p>
<ul>
<li><strong>系统级</strong>/etc/gitconfig使用 <strong>—system</strong> 来进行配置,该配置<strong>对系统上所有用户及他们所拥有的仓库都生效</strong>的配置值。</li>
</ul>
<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);">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);">--system</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);">&quot;用户名&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">--system</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-expression);">&quot;邮箱地址&quot;`</span></span></code></pre></div>
<ul>
<li><strong>用户级</strong>~/.gitconfig使用 <strong>—global</strong> 来进行配置,该配置<strong>对当前用户上所有的仓库有效</strong></li>
</ul>
<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);">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);">&quot;用户名&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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-expression);">&quot;邮箱地址&quot;</span></span></code></pre></div>
<ul>
<li><strong>目录级</strong>.git/config使用 <strong>—local</strong> 来进行配置,<strong>只对当前仓库生效。</strong></li>
</ul>
<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);">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);">--local</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);">&quot;用户名&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">--local</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-expression);">&quot;邮箱地址&quot;</span></span></code></pre></div>
<h2 id="设置默认分支">设置默认分支<a aria-hidden="true" tabindex="-1" href="#设置默认分支" class="internal"> §</a></h2>
<p>可以根据需求设置为<strong>系统级—system、用户级—global和目录级—local</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-token-function);">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);">init.defaultBranch</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);">分支名</span><span style="color:var(--shiki-color-text);"></span><span style="color:var(--shiki-token-keyword);">></span></span></code></pre></div>
<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);"># 设置默认分支 main</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">init.defaultBranch</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">main</span></span></code></pre></div>
<h2 id="多平台换行符问题-lf-or-crlf">多平台换行符问题 (LF or CRLF)<a aria-hidden="true" tabindex="-1" href="#多平台换行符问题-lf-or-crlf" class="internal"> §</a></h2>
<p>**文本文件所使用的换行符,在不同的系统平台上是不一样的。**UNIX/Linux 使用的是 0x0ALF早期的 Mac OS 使用的是 0x0DCR后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0ACRLF 作为换行符。</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);"># 提交时转换为LF检出时转换为CRLF</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.autocrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">true</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 提交时转换为LF检出时不转换</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.autocrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">input</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 提交检出均不转换</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.autocrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">false</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 拒绝提交包含混合换行符的文件</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.safecrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">true</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 允许提交包含混合换行符的文件</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.safecrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">false</span></span>
<span data-line><span style="color:var(--shiki-token-comment);"># 提交包含混合换行符的文件时给出警告</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.safecrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">warn</span></span></code></pre></div>
<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-function);">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);">core.autocrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">input</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">core.safecrlf</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">true</span></span></code></pre></div>
<h2 id="alias-重命名别名">Alias 重命名别名<a aria-hidden="true" tabindex="-1" href="#alias-重命名别名" class="internal"> §</a></h2>
<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);">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);">alias.st</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);">//status</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);">st</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">alias.co</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">checkout</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">//checkout</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);">co</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">alias.br</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);">//branch</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);">br</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">alias.ci</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);">//commit</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);">ci</span></span></code></pre></div>
<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-keyword);">alias</span><span style="color:var(--shiki-color-text);"> g</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> ga</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git add'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gco</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git checkout'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gcb</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git checkout -b'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gcm</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git checkout master'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gcd</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git checkout develop'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gd</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git diff'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gf</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git fetch'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gfo</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git fetch origin'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gl</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git pull'</span></span>
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> gp</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">'git push'</span></span></code></pre></div>
<p>Git 常用命令,可以根据实际需要创建缩写命令。</p>
<h2 id="gpg-签名提交">GPG 签名提交<a aria-hidden="true" tabindex="-1" href="#gpg-签名提交" class="internal"> §</a></h2>
<h3 id="创建新的gpg-密钥">创建新的GPG 密钥<a aria-hidden="true" tabindex="-1" href="#创建新的gpg-密钥" class="internal"> §</a></h3>
<ol>
<li>生成 GPG 密钥对</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-constant);">1.1</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);">GPG</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--full-gen-key</span></span>
<span data-line> </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-constant);">1.2</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);">GPG</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--list-secret-keys</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--keyid-format</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">LONG</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;your_email@example.com&quot;</span></span>
<span data-line> </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-constant);">1.3</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);">GPG</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);">ID以下示例复制</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-string);">AEA00A342C24CA3。</span></span>
<span data-line><span style="color:var(--shiki-token-function);">sec</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ed25519/4AEA00A342C24CA3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2021</span><span style="color:var(--shiki-token-string);">-09-14</span><span style="color:var(--shiki-color-text);"> [SC]</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4</span></span>
<span data-line><span style="color:var(--shiki-token-function);">uid</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);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">your_name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;your_email@example.com&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-function);">ssb</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cv25519/812B586FD245B560</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2021</span><span style="color:var(--shiki-token-string);">-09-14</span><span style="color:var(--shiki-color-text);"> [E]</span></span>
<span data-line> </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-constant);">1.4</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);">GPG</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);">ID</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--armor</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--export</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-string);">AEA00A342C24CA3</span></span></code></pre></div>
<ol start="2">
<li>平台添加 GPG 公钥配置</li>
</ol>
<p>将 GPG 公钥添加到 git 托管平台,例如 Github、Gitlab、Gitee 等。</p>
<ol start="3">
<li>本地 Git 仓库关联 GPG 密钥</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-constant);">3.1</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);">GPG</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--list-secret-keys</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--keyid-format</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">LONG</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;your_email@example.com&quot;</span></span>
<span data-line> </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-constant);">3.2</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);">GPG</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);">ID以下示例复制</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-string);">AEA00A342C24CA3。</span></span>
<span data-line><span style="color:var(--shiki-token-function);">sec</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">ed25519/4AEA00A342C24CA3</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2021</span><span style="color:var(--shiki-token-string);">-09-14</span><span style="color:var(--shiki-color-text);"> [SC]</span></span>
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4</span></span>
<span data-line><span style="color:var(--shiki-token-function);">uid</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);">]</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">your_name</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;your_email@example.com&quot;</span></span>
<span data-line><span style="color:var(--shiki-token-function);">ssb</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cv25519/812B586FD245B560</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">2021</span><span style="color:var(--shiki-token-string);">-09-14</span><span style="color:var(--shiki-color-text);"> [E]</span></span>
<span data-line> </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-constant);">3.3</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 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);">commit</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-token-function);">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.signingkey</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-token-string);">AEA00A342C24CA3</span></span></code></pre></div>
<ol start="4">
<li>签名 Git commit</li>
</ol>
<p>运行 Git commit 命令时需要用到 <code>-S</code> 参数,如果不希望每次都要输入 <code>-S</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);">签名提交</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">&quot;your_commit_message&quot;</span></span>
<span data-line> </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>
<span data-line><span style="color:var(--shiki-token-function);">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);">commit.gpgsign</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">true</span></span></code></pre></div>
<ol start="5">
<li>验证签名</li>
</ol>
<p>最后可以推送签名提交到 git 托管平台,验证是否签名成功!</p>
<h3 id="导入已有的-gpg-密钥">导入已有的 GPG 密钥<a aria-hidden="true" tabindex="-1" href="#导入已有的-gpg-密钥" class="internal"> §</a></h3>
<p>如果你已经有现成的 GPG 密钥,可以导入并使用。</p>
<ol>
<li>
<p><strong>导入 GPG 私钥</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--import</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">your_private_key_file</span></span></code></pre></div>
</li>
<li>
<p><strong>导入 GPG 公钥</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--import</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">your_public_key_file</span></span></code></pre></div>
</li>
<li>
<p><strong>信任导入的 GPG 密钥</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-token-function);">gpg</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--edit-key</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">your_key_id</span></span>
<span data-line><span style="color:var(--shiki-token-function);">gpg></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">trust</span></span>
<span data-line><span style="color:var(--shiki-token-function);">gpg></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-color-text);"> (ultimate </span><span style="color:var(--shiki-token-string);">trust</span><span style="color:var(--shiki-color-text);">)</span></span>
<span data-line><span style="color:var(--shiki-token-function);">gpg></span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">quit</span></span></code></pre></div>
</li>
<li>
<p>重复<strong>创建新的GPG 密钥</strong>中的第三步。</p>
</li>
</ol>
<h2 id="压缩提交记录">压缩提交记录<a aria-hidden="true" tabindex="-1" href="#压缩提交记录" class="internal"> §</a></h2>
<p>如果在新分支进行了多次提交,然后想合并到主分支。可以使用 —squash 选项,压缩合并提交。</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);">将目标分支的提交压缩为一个,然后合并分支</span></span>
<span data-line><span style="color:var(--shiki-token-function);">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);">--squash</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);">branchNam</span><span style="color:var(--shiki-color-text);">e</span><span style="color:var(--shiki-token-keyword);">></span></span></code></pre></div>
<h2 id="忽略文件更改">忽略文件更改<a aria-hidden="true" tabindex="-1" href="#忽略文件更改" class="internal"> §</a></h2>
<ul>
<li>
<p>未提交:添加忽略文件相对路径到 .gitignore 中即可。</p>
</li>
<li>
<p>已提交:<code>git rm --cached file</code>,添加忽略文件相对路径到 .gitignore 中即可。</p>
</li>
</ul>
<h2 id="配置-git-的凭据存储">配置 Git 的凭据存储<a aria-hidden="true" tabindex="-1" href="#配置-git-的凭据存储" class="internal"> §</a></h2>
<p>当使用 Git 进行代码管理时,你可以配置 Git 的凭据存储,这样就不需要每次都输入用户名和密码。下面是详细的步骤:</p>
<h3 id="使用凭证缓存">使用凭证缓存<a aria-hidden="true" tabindex="-1" href="#使用凭证缓存" class="internal"> §</a></h3>
<ol>
<li>
<p><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-token-function);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cache</span></span></code></pre></div>
</li>
<li>
<p><strong>设置缓存超时时间</strong>(默认为 15 分钟,以下示例设置为 1 小时):</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);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'cache --timeout=3600'</span></span></code></pre></div>
</li>
</ol>
<h3 id="使用凭证管理器">使用凭证管理器<a aria-hidden="true" tabindex="-1" href="#使用凭证管理器" class="internal"> §</a></h3>
<p>Git for Windows 以及较新的 Git 版本提供了凭证管理器Credential Manager可以在多平台下存储和管理凭证。</p>
<ol>
<li>
<p><strong>Windows 用户</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-token-function);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">manager</span></span></code></pre></div>
</li>
<li>
<p><strong>MacOS 用户</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-token-function);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">osxkeychain</span></span></code></pre></div>
</li>
<li>
<p><strong>Linux 用户</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-token-function);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">cache</span></span></code></pre></div>
</li>
</ol>
<h3 id="使用凭证存储">使用凭证存储<a aria-hidden="true" tabindex="-1" href="#使用凭证存储" class="internal"> §</a></h3>
<p>这种方法会将凭证以纯文本的方式存储在磁盘上,安全性较低。</p>
<ol>
<li>
<p><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-token-function);">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);">credential.helper</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">store</span></span></code></pre></div>
</li>
<li>
<p><strong>第一次使用 Git 操作时输入用户名和密码后,凭证会被保存到磁盘</strong></p>
</li>
<li>
<p><strong>使用 <code>cat</code> 命令查看 <code>.git-credentials</code> 文件的内容</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-token-function);">cat</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">~/.git-credentials</span></span></code></pre></div>
</li>
</ol>
<p><strong>请注意</strong>,使用 <code>store</code> 凭据存储方式会将密码明文保存在磁盘上,所以请确保你的机器是安全的,并且不要在共享或公共设备上使用该功能。</p>
<h2 id="全局的-git-日志美化">全局的 Git 日志美化<a aria-hidden="true" tabindex="-1" href="#全局的-git-日志美化" class="internal"> §</a></h2>
<p>可以使用 <code>git config --global format.pretty</code> 设置一个全局的 Git 日志格式化语句。</p>
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="bash" data-theme="default"><code data-language="bash" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">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);">format.pretty</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">&quot;%C(red)%h%C(reset) - %C(yellow)%d%C(reset) %s %C(green)(%cr) %C(bold blue)&lt;%an>%C(reset)&quot;</span></span></code></pre></div>
<p>常用的配置选项及其描述:</p>
<table><thead><tr><th>配置选项</th><th>描述</th></tr></thead><tbody><tr><td><code>%H</code></td><td>提交的完整哈希字符串</td></tr><tr><td><code>%h</code></td><td>提交的简短哈希字符串</td></tr><tr><td><code>%T</code></td><td>树的完整哈希字符串</td></tr><tr><td><code>%t</code></td><td>树的简短哈希字符串</td></tr><tr><td><code>%P</code></td><td>父节点的完整哈希字符串</td></tr><tr><td><code>%p</code></td><td>父节点的简短哈希字符串</td></tr><tr><td><code>%an</code></td><td>作者的名字</td></tr><tr><td><code>%ae</code></td><td>作者的电子邮件地址</td></tr><tr><td><code>%ad</code></td><td>作者修订日期(可以用 —date=选项定制格式)</td></tr><tr><td><code>%ar</code></td><td>作者修订日期相对格式例如“2 weeks ago”</td></tr><tr><td><code>%cn</code></td><td>提交者的名字</td></tr><tr><td><code>%ce</code></td><td>提交者的电子邮件地址</td></tr><tr><td><code>%cd</code></td><td>提交日期</td></tr><tr><td><code>%cr</code></td><td>提交日期,相对格式</td></tr><tr><td><code>%s</code></td><td>提交信息标题</td></tr><tr><td><code>%b</code></td><td>提交信息正文</td></tr><tr><td><code>%Cred</code></td><td>文字颜色为红色</td></tr><tr><td><code>%Cgreen</code></td><td>文字颜色为绿色</td></tr><tr><td><code>%Cblue</code></td><td>文字颜色为蓝色</td></tr><tr><td><code>%Creset</code></td><td>重置前面设置的颜色</td></tr><tr><td><code>%C(yellow)</code></td><td>文字颜色为黄色</td></tr><tr><td><code>%d</code></td><td>修饰(比如分支名,标签名)</td></tr><tr><td><code>%D</code></td><td>修饰的完整列表</td></tr></tbody></table></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="#config-配置" data-for="config-配置">Config 配置</a></li><li class="depth-0"><a href="#设置默认分支" data-for="设置默认分支">设置默认分支</a></li><li class="depth-0"><a href="#多平台换行符问题-lf-or-crlf" data-for="多平台换行符问题-lf-or-crlf">多平台换行符问题 (LF or CRLF)</a></li><li class="depth-0"><a href="#alias-重命名别名" data-for="alias-重命名别名">Alias 重命名别名</a></li><li class="depth-0"><a href="#gpg-签名提交" data-for="gpg-签名提交">GPG 签名提交</a></li><li class="depth-1"><a href="#创建新的gpg-密钥" data-for="创建新的gpg-密钥">创建新的GPG 密钥</a></li><li class="depth-1"><a href="#导入已有的-gpg-密钥" data-for="导入已有的-gpg-密钥">导入已有的 GPG 密钥</a></li><li class="depth-0"><a href="#压缩提交记录" data-for="压缩提交记录">压缩提交记录</a></li><li class="depth-0"><a href="#忽略文件更改" data-for="忽略文件更改">忽略文件更改</a></li><li class="depth-0"><a href="#配置-git-的凭据存储" data-for="配置-git-的凭据存储">配置 Git 的凭据存储</a></li><li class="depth-1"><a href="#使用凭证缓存" data-for="使用凭证缓存">使用凭证缓存</a></li><li class="depth-1"><a href="#使用凭证管理器" data-for="使用凭证管理器">使用凭证管理器</a></li><li class="depth-1"><a href="#使用凭证存储" data-for="使用凭证存储">使用凭证存储</a></li><li class="depth-0"><a href="#全局的-git-日志美化" data-for="全局的-git-日志美化">全局的 Git 日志美化</a></li></ul></div></div><div class="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Journal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Journal/2024&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Journal/2024/W33&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Obsidian&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Obsidian/Templates&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2023&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2024&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/个人成长&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/医学健康&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/历史&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&qu
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>