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

322 lines
503 KiB
HTML
Raw Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>常用技巧</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><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 href="../../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" 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"><h2 class="page-title"><a href="../../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><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></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="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" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</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><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="n
<p>Git 配置文件分为三级系统级—system、用户级—global和目录级—local三者的使用优先级以离目录repository最近为原则如果三者的配置不一样则生效优先级 <strong>目录级>用户级>系统级</strong></p>
<ul>
<li><strong>系统级</strong>/etc/gitconfig使用 <strong>—system</strong> 来进行配置,该配置<strong>对系统上所有用户及他们所拥有的仓库都生效</strong>的配置值。</li>
</ul>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --system</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;用户名&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --system</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.email</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;邮箱地址&quot;`</span></span></code></pre></figure>
<ul>
<li><strong>用户级</strong>~/.gitconfig使用 <strong>—global</strong> 来进行配置,该配置<strong>对当前用户上所有的仓库有效</strong></li>
</ul>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;用户名&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.email</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;邮箱地址&quot;</span></span></code></pre></figure>
<ul>
<li><strong>目录级</strong>.git/config使用 <strong>—local</strong> 来进行配置,<strong>只对当前仓库生效。</strong></li>
</ul>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --local</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;用户名&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --local</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.email</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;邮箱地址&quot;</span></span></code></pre></figure>
<h2 id="设置默认分支">设置默认分支<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#设置默认分支" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>可以根据需求设置为<strong>系统级—system、用户级—global和目录级—local</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> init.defaultBranch</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">分支名</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"></span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span></code></pre></figure>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 设置默认分支 main</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> init.defaultBranch</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> main</span></span></code></pre></figure>
<h2 id="多平台换行符问题-lf-or-crlf">多平台换行符问题 (LF or CRLF)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多平台换行符问题-lf-or-crlf" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>**文本文件所使用的换行符,在不同的系统平台上是不一样的。**UNIX/Linux 使用的是 0x0ALF早期的 Mac OS 使用的是 0x0DCR后来的 OS X 在更换内核后与 UNIX 保持一致了。但 DOS/Windows 一直使用 0x0D0ACRLF 作为换行符。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 提交时转换为LF检出时转换为CRLF</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.autocrlf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 提交时转换为LF检出时不转换</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.autocrlf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> input</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 提交检出均不转换</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.autocrlf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 拒绝提交包含混合换行符的文件</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.safecrlf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 允许提交包含混合换行符的文件</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.safecrlf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 提交包含混合换行符的文件时给出警告</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.safecrlf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> warn</span></span></code></pre></figure>
<p>如果涉及到在多个系统平台上工作,推荐将 git 做如下配置:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.autocrlf</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> input</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> core.safecrlf</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span></span></code></pre></figure>
<h2 id="alias-重命名别名">Alias 重命名别名<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#alias-重命名别名" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> alias.st</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> status</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> //status</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 缩写成</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> st</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> alias.co</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> checkout</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> //checkout</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 缩写成</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> co</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> alias.br</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> branch</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> //branch</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 缩写成</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> br</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> alias.ci</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> //commit</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 缩写成</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ci</span></span></code></pre></figure>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> g</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ga</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git add'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gco</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git checkout'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gcb</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git checkout -b'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gcm</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git checkout master'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gcd</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git checkout develop'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gd</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git diff'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gf</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git fetch'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gfo</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git fetch origin'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gl</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git pull'</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> gp</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'git push'</span></span></code></pre></figure>
<p>Git 常用命令,可以根据实际需要创建缩写命令。</p>
<h2 id="gpg-签名提交">GPG 签名提交<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gpg-签名提交" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="创建新的gpg-密钥">创建新的GPG 密钥<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建新的gpg-密钥" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ol>
<li>生成 GPG 密钥对</li>
</ol>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1.1</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 创建</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 密钥</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --full-gen-key</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1.2</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 列出</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 密钥</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --list-secret-keys</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --keyid-format</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> LONG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;your_email@example.com&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1.3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 复制</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 密钥</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ID以下示例复制</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 4AEA00A342C24CA3。</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sec</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ed25519/4AEA00A342C24CA3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 2021-09-14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [SC]</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> 6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">绝对</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ]</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your_name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;your_email@example.com&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ssb</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cv25519/812B586FD245B560</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 2021-09-14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [E]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1.4</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 导出</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 公钥(以上述</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ID</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 为例)</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --armor</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --export</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 4AEA00A342C24CA3</span></span></code></pre></figure>
<ol start="2">
<li>平台添加 GPG 公钥配置</li>
</ol>
<p>将 GPG 公钥添加到 git 托管平台,例如 Github、Gitlab、Gitee 等。</p>
<ol start="3">
<li>本地 Git 仓库关联 GPG 密钥</li>
</ol>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.1</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 列出</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 密钥</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --list-secret-keys</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --keyid-format</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> LONG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;your_email@example.com&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.2</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 复制</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> GPG</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 密钥</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ID以下示例复制</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 4AEA00A342C24CA3。</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sec</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ed25519/4AEA00A342C24CA3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 2021-09-14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [SC]</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> 6DE3507E82DEB6E8828FAAC34AEA00A342C24BD4</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">uid</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [ </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">绝对</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ]</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your_name</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;your_email@example.com&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ssb</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cv25519/812B586FD245B560</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 2021-09-14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [E]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 3.3</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 本地</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> Git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 仓库中配置该密钥,对</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 提交进行签名。</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> user.signingkey</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 4AEA00A342C24CA3</span></span></code></pre></figure>
<ol start="4">
<li>签名 Git commit</li>
</ol>
<p>运行 Git commit 命令时需要用到 <code>-S</code> 参数,如果不希望每次都要输入 <code>-S</code> 标志,可以配置默认签名。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 签名提交</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -S</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;your_commit_message&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 开启默认签名提交</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit.gpgsign</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span></span></code></pre></figure>
<ol start="5">
<li>验证签名</li>
</ol>
<p>最后可以推送签名提交到 git 托管平台,验证是否签名成功!</p>
<h3 id="导入已有的-gpg-密钥">导入已有的 GPG 密钥<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#导入已有的-gpg-密钥" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>如果你已经有现成的 GPG 密钥,可以导入并使用。</p>
<ol>
<li>
<p><strong>导入 GPG 私钥</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --import</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your_private_key_file</span></span></code></pre></figure>
</li>
<li>
<p><strong>导入 GPG 公钥</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --import</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your_public_key_file</span></span></code></pre></figure>
</li>
<li>
<p><strong>信任导入的 GPG 密钥</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --edit-key</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> your_key_id</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">> </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">trust</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">> </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (ultimate </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">trust</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">gpg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">> </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">quit</span></span></code></pre></figure>
</li>
<li>
<p>重复<strong>创建新的GPG 密钥</strong>中的第三步。</p>
</li>
</ol>
<h2 id="压缩提交记录">压缩提交记录<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#压缩提交记录" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>如果在新分支进行了多次提交,然后想合并到主分支。可以使用 —squash 选项,压缩合并提交。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">//</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 将目标分支的提交压缩为一个,然后合并分支</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> merge</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --squash</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">branchNam</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">e</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">></span></span></code></pre></figure>
<h2 id="忽略文件更改">忽略文件更改<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#忽略文件更改" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></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 role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置-git-的凭据存储" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>当使用 Git 进行代码管理时,你可以配置 Git 的凭据存储,这样就不需要每次都输入用户名和密码。下面是详细的步骤:</p>
<h3 id="使用凭证缓存">使用凭证缓存<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用凭证缓存" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<ol>
<li>
<p><strong>开启凭证缓存</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cache</span></span></code></pre></figure>
</li>
<li>
<p><strong>设置缓存超时时间</strong>(默认为 15 分钟,以下示例设置为 1 小时):</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'cache --timeout=3600'</span></span></code></pre></figure>
</li>
</ol>
<h3 id="使用凭证管理器">使用凭证管理器<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用凭证管理器" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>Git for Windows 以及较新的 Git 版本提供了凭证管理器Credential Manager可以在多平台下存储和管理凭证。</p>
<ol>
<li>
<p><strong>Windows 用户</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> manager</span></span></code></pre></figure>
</li>
<li>
<p><strong>MacOS 用户</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> osxkeychain</span></span></code></pre></figure>
</li>
<li>
<p><strong>Linux 用户</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cache</span></span></code></pre></figure>
</li>
</ol>
<h3 id="使用凭证存储">使用凭证存储<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用凭证存储" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>这种方法会将凭证以纯文本的方式存储在磁盘上,安全性较低。</p>
<ol>
<li>
<p><strong>开启凭证存储</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> credential.helper</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> store</span></span></code></pre></figure>
</li>
<li>
<p><strong>第一次使用 Git 操作时输入用户名和密码后,凭证会被保存到磁盘</strong></p>
</li>
<li>
<p><strong>使用 <code>cat</code> 命令查看 <code>.git-credentials</code> 文件的内容</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">cat</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ~/.git-credentials</span></span></code></pre></figure>
</li>
</ol>
<p><strong>请注意</strong>,使用 <code>store</code> 凭据存储方式会将密码明文保存在磁盘上,所以请确保你的机器是安全的,并且不要在共享或公共设备上使用该功能。</p>
<h2 id="全局的-git-日志美化">全局的 Git 日志美化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#全局的-git-日志美化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<p>可以使用 <code>git config --global format.pretty</code> 设置一个全局的 Git 日志格式化语句。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="bash" data-theme="github-light github-dark"><code data-language="bash" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> config</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --global</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> format.pretty</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &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></figure>
<p>常用的配置选项及其描述:</p>
<div class="table-container"><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></div></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</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;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="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></button></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;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</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" class><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="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&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;:&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;:&quot;Personal/Book/文学&q
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../../postscript.js" type="module"></script></html>