blog-hexo/public/2023/11/06/clopc6c5n000hibz3g71savqe/index.html

750 lines
73 KiB
HTML
Raw Normal View History

2023-11-06 16:05:27 +08:00
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
Git
</title>
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="author" content="Mozzie">
2023-11-08 13:45:34 +08:00
<link rel="canonical" href="https://mozzie.cn/2023/11/06/clopc6c5n000hibz3g71savqe/">
2023-11-06 16:05:27 +08:00
<link rel="icon" type="image/svg" href='data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M16 7h1a2 2 0 0 1 2 2v.5a.5.5 0 0 0 .5.5a.5.5 0 0 1 .5.5v3a.5.5 0 0 1-.5.5a.5.5 0 0 0-.5.5v.5a2 2 0 0 1-2 2h-2"></path><path d="M8 7H6a2 2 0 0 0-2 2v6a2 2 0 0 0 2 2h1"></path><path d="M12 8l-2 4h3l-2 4"></path></g></svg>'>
2023-11-07 13:04:43 +08:00
<link rel="stylesheet" href="/css/4d4ea9c6.css">
2023-11-06 16:05:27 +08:00
<meta name="generator" content="Hexo 6.3.0"></head>
<body id="app">
<!-- 响应式布局按钮 -->
<a class="side-navigation btn-hover btn-hover-bg tip left" data-tip="Expand">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<rect x="4" y="4" width="16" height="16" rx="2"></rect>
<path d="M9 4v16"></path>
</g>
</svg>
</a>
<aside>
<div>
<link rel="stylesheet" href="/css/5bfc518f.css">
<div class="home-bar">
<a href="/" class="tip" data-tip="Home">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512">
<path
d="M172.2 226.8c-14.6-2.9-28.2 8.9-28.2 23.8V301c0 10.2 7.1 18.4 16.7 22c18.2 6.8 31.3 24.4 31.3 45c0 26.5-21.5 48-48 48s-48-21.5-48-48V120c0-13.3-10.7-24-24-24H24c-13.3 0-24 10.7-24 24v248c0 89.5 82.1 160.2 175 140.7c54.4-11.4 98.3-55.4 109.7-109.7c17.4-82.9-37-157.2-112.5-172.2zM209 0c-9.2-.5-17 6.8-17 16v31.6c0 8.5 6.6 15.5 15 15.9c129.4 7 233.4 112 240.9 241.5c.5 8.4 7.5 15 15.9 15h32.1c9.2 0 16.5-7.8 16-17C503.4 139.8 372.2 8.6 209 0zm.3 96c-9.3-.7-17.3 6.7-17.3 16.1v32.1c0 8.4 6.5 15.3 14.8 15.9c76.8 6.3 138 68.2 144.9 145.2c.8 8.3 7.6 14.7 15.9 14.7h32.2c9.3 0 16.8-8 16.1-17.3c-8.4-110.1-96.5-198.2-206.6-206.7z"
fill="currentColor"></path>
</svg>
<span>Mozzie</span>
</a>
<!-- 切换主题 -->
<a id="toggle-theme" class="tip left" data-tip="Switch Theme">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512">
<path d="M256 118a22 22 0 0 1-22-22V48a22 22 0 0 1 44 0v48a22 22 0 0 1-22 22z" fill="currentColor"></path>
<path d="M256 486a22 22 0 0 1-22-22v-48a22 22 0 0 1 44 0v48a22 22 0 0 1-22 22z" fill="currentColor"></path>
<path
d="M369.14 164.86a22 22 0 0 1-15.56-37.55l33.94-33.94a22 22 0 0 1 31.11 31.11l-33.94 33.94a21.93 21.93 0 0 1-15.55 6.44z"
fill="currentColor"></path>
<path
d="M108.92 425.08a22 22 0 0 1-15.55-37.56l33.94-33.94a22 22 0 1 1 31.11 31.11l-33.94 33.94a21.94 21.94 0 0 1-15.56 6.45z"
fill="currentColor"></path>
<path d="M464 278h-48a22 22 0 0 1 0-44h48a22 22 0 0 1 0 44z" fill="currentColor"></path>
<path d="M96 278H48a22 22 0 0 1 0-44h48a22 22 0 0 1 0 44z" fill="currentColor"></path>
<path
d="M403.08 425.08a21.94 21.94 0 0 1-15.56-6.45l-33.94-33.94a22 22 0 0 1 31.11-31.11l33.94 33.94a22 22 0 0 1-15.55 37.56z"
fill="currentColor"></path>
<path
d="M142.86 164.86a21.89 21.89 0 0 1-15.55-6.44l-33.94-33.94a22 22 0 0 1 31.11-31.11l33.94 33.94a22 22 0 0 1-15.56 37.55z"
fill="currentColor"></path>
<path d="M256 358a102 102 0 1 1 102-102a102.12 102.12 0 0 1-102 102z" fill="currentColor"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512">
<path
d="M264 480A232 232 0 0 1 32 248c0-94 54-178.28 137.61-214.67a16 16 0 0 1 21.06 21.06C181.07 76.43 176 104.66 176 136c0 110.28 89.72 200 200 200c31.34 0 59.57-5.07 81.61-14.67a16 16 0 0 1 21.06 21.06C442.28 426 358 480 264 480z"
fill="currentColor"></path>
</svg>
</a>
</div>
2023-11-06 16:14:34 +08:00
<script src="/js/ae2a0e7b.js"></script>
2023-11-06 16:05:27 +08:00
<!-- search -->
2023-11-08 10:11:56 +08:00
<link rel="stylesheet" href="/css/82dd7e5a.css">
2023-11-06 16:05:27 +08:00
<form class="search-group">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20">
<g fill="currentColor">
<path d="M8.5 3a5.5 5.5 0 0 1 4.383 8.823l4.147 4.147a.75.75 0 0 1-.976 1.133l-.084-.073l-4.147-4.147A5.5 5.5 0 1 1 8.5 3zm0 1.5a4 4 0 1 0 0 8a4 4 0 0 0 0-8z" fill="currentColor"></path>
</g>
</svg>
<span id="search-input">Search...</span>
<div class="short-key">
<kbd class="key-cap"><span>Ctrl K</span></kbd>
</div>
</form>
<script>
window.algolia = {
appId: "5DTW808BZ8",
SearchOnlyAPIKey: "27845b245efc8a2853cc0bdc7366ea26"
}
window.search = {
enable: "true"
}
</script>
2023-11-07 16:12:40 +08:00
<script src="/js/b9c2be9c.js"></script>
2023-11-06 16:05:27 +08:00
<!-- navigation -->
<link rel="stylesheet" href="/css/3efc6cb5.css">
<section class="category-nav scrollbar-obtrusive">
<ul class="nav-items">
<li data-path="archives">
<a href="/archives">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M12 7v14"></path>
<path d="M9 18l3 3l3-3"></path>
<circle cx="12" cy="5" r="2"></circle>
</g>
</svg>
<div class="ellipsis">Timeline</div>
</a>
</li>
<li data-path="roadmap">
<a href="/roadmap">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M10.5 20.4l-6.9-6.9c-.781-.781-.781-2.219 0-3l6.9-6.9c.781-.781 2.219-.781 3 0l6.9 6.9c.781.781.781 2.219 0 3l-6.9 6.9c-.781.781-2.219.781-3 0z"></path>
<path d="M9 14v-2c0-.59.414-1 1-1h5"></path>
<path d="M13 9l2 2l-2 2"></path>
</g>
</svg>
<div class="ellipsis">Roadmap</div>
</a>
</li>
<li data-path="resume">
<a href="/resume">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M14 3v4a1 1 0 0 0 1 1h4"></path>
<path d="M5 8V5a2 2 0 0 1 2-2h7l5 5v11a2 2 0 0 1-2 2h-5"></path>
<circle cx="6" cy="14" r="3"></circle>
<path d="M4.5 17L3 22l3-1.5L9 22l-1.5-5"></path>
</g>
</svg>
<div class="ellipsis">Resume</div>
</a>
</li>
</ul>
<p>Category</p>
<ul>
<li class="">
2023-11-08 10:54:36 +08:00
<a href="/categories/EQ/">
2023-11-06 16:05:27 +08:00
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M5.636 5.636a9 9 0 0 1 13.397.747L13.414 12l5.619 5.617A9 9 0 1 1 5.636 5.636z"></path><circle cx="11.5" cy="7.5" r="1" fill="currentColor"></circle></g></svg>
<div class="ellipsis">
<span>
2023-11-08 10:54:36 +08:00
EQ
2023-11-06 16:05:27 +08:00
</span>
</div>
</a>
</li>
<li class="">
2023-11-08 10:54:36 +08:00
<a href="/categories/Hexo/">
2023-11-06 16:05:27 +08:00
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 17v1a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-1"></path><path d="M8 16h8"></path><path d="M8.322 12.582l7.956.836"></path><path d="M8.787 9.168l7.826 1.664"></path><path d="M10.096 5.764l7.608 2.472"></path></g></svg>
<div class="ellipsis">
<span>
2023-11-08 10:54:36 +08:00
Hexo
2023-11-06 16:05:27 +08:00
</span>
</div>
</a>
</li>
<li class="">
2023-11-08 10:54:36 +08:00
<a href="/categories/Finance/">
2023-11-06 16:05:27 +08:00
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="9"></circle><path d="M14.8 9A2 2 0 0 0 13 8h-2a2 2 0 0 0 0 4h2a2 2 0 0 1 0 4h-2a2 2 0 0 1-1.8-1"></path><path d="M12 6v2m0 8v2"></path></g></svg>
<div class="ellipsis">
<span>
2023-11-08 10:54:36 +08:00
Finance
2023-11-06 16:05:27 +08:00
</span>
</div>
</a>
</li>
<li class="active">
<a href="/categories/Front-End/">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24"><g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M20 4l-2 14.5l-6 2l-6-2L4 4z"></path><path d="M7.5 8h3v8l-2-1"></path><path d="M16.5 8H14a.5.5 0 0 0-.5.5v3a.5.5 0 0 0 .5.5h1.423a.5.5 0 0 1 .495.57L15.5 15.5l-2 .5"></path></g></svg>
<div class="ellipsis">
<span>
Front-End
</span>
</div>
</a>
</li>
</ul>
</section>
<script src="/js/f8b20eb9.js"></script>
<!-- icp -->
<div class="icp">
<a target="_blank" rel="noopener" href="http://beian.miit.gov.cn/">苏ICP备19008833号-4</a>
</div>
</div>
</aside>
<main>
2023-11-08 13:45:34 +08:00
<link rel="stylesheet" href="/css/6006f423.css">
2023-11-06 16:05:27 +08:00
<div class="post-container">
<div class="content">
<header>
<link rel="stylesheet" href="/css/de5de8fb.css">
<nav class="breadcrumb">
<a href="/" class="home tip btn-hover right" data-tip="Home">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 512 512">
<path d="M172.2 226.8c-14.6-2.9-28.2 8.9-28.2 23.8V301c0 10.2 7.1 18.4 16.7 22c18.2 6.8 31.3 24.4 31.3 45c0 26.5-21.5 48-48 48s-48-21.5-48-48V120c0-13.3-10.7-24-24-24H24c-13.3 0-24 10.7-24 24v248c0 89.5 82.1 160.2 175 140.7c54.4-11.4 98.3-55.4 109.7-109.7c17.4-82.9-37-157.2-112.5-172.2zM209 0c-9.2-.5-17 6.8-17 16v31.6c0 8.5 6.6 15.5 15 15.9c129.4 7 233.4 112 240.9 241.5c.5 8.4 7.5 15 15.9 15h32.1c9.2 0 16.5-7.8 16-17C503.4 139.8 372.2 8.6 209 0zm.3 96c-9.3-.7-17.3 6.7-17.3 16.1v32.1c0 8.4 6.5 15.3 14.8 15.9c76.8 6.3 138 68.2 144.9 145.2c.8 8.3 7.6 14.7 15.9 14.7h32.2c9.3 0 16.8-8 16.1-17.3c-8.4-110.1-96.5-198.2-206.6-206.7z" fill="currentColor"></path>
</svg>
</a>
<em>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20">
<g fill="none">
<path d="M7.733 4.207a.75.75 0 0 1 1.06.026l5.001 5.25a.75.75 0 0 1 0 1.035l-5 5.25a.75.75 0 1 1-1.087-1.034L12.216 10l-4.51-4.734a.75.75 0 0 1 .027-1.06z" fill="currentColor"></path>
</g>
</svg>
</em>
<a href="/categories/Front-End/">
Front-End
</a>
<em>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 20 20">
<g fill="none">
<path d="M7.733 4.207a.75.75 0 0 1 1.06.026l5.001 5.25a.75.75 0 0 1 0 1.035l-5 5.25a.75.75 0 1 1-1.087-1.034L12.216 10l-4.51-4.734a.75.75 0 0 1 .027-1.06z" fill="currentColor"></path>
</g>
</svg>
</em>
<span class="ellipsis">
Git
</span>
</nav>
<script src="/js/31d6cfe0.js"></script>
</header>
<main class="scrollbar-obtrusive">
<div class="article-container">
<!-- 文章tags -->
<!-- 渲染文章内容 -->
2023-11-08 13:45:34 +08:00
<article>
<h1>Git</h1>
<h1 id="GIT-最佳实践"><a href="#GIT-最佳实践" class="headerlink" title="GIT 最佳实践"></a>GIT 最佳实践</h1><p>GIT 本质是一个数据库,用来存代码的</p>
2023-11-06 16:05:27 +08:00
<ul>
<li>工作区一个沙箱环境GIT 不负责管理,你尽管在沙箱里面对文件进行操作</li>
<li>暂存区:<code>工作区</code>文件变动先不急着提交,暂存到一定数量,在提交到版本库</li>
<li>版本库:</li>
</ul>
<blockquote>
<p>!! Linus 永远的神!</p>
</blockquote>
<h2 id="配置用户"><a href="#配置用户" class="headerlink" title="配置用户"></a>配置用户</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> config <span class="token parameter variable">--global</span> user.name <span class="token string">"mozzie"</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> user.email himozzie@foxmail.com<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h2 id="alias-别名"><a href="#alias-别名" class="headerlink" title="alias 别名"></a>alias 别名</h2><p>解决参数太多,记不住的问题</p>
<blockquote>
<p>!! HEAD -&gt; master HEAD 相当于一个指针,指向当前所在分支</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看项目分支图,</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.lo <span class="token string">"log --oneline --decorate --graph --all"</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h2 id="git-文件结构"><a href="#git-文件结构" class="headerlink" title=".git 文件结构"></a>.git 文件结构</h2><ul>
<li>hooks提交代码前检查代码格式……</li>
<li>info包含一个排除性文件</li>
<li>logs保存日志信息不太需要</li>
<li><code>objects</code>:相当于数据库,存储所有数据内容</li>
<li><code>refs</code>:存放提交对象指针,管理分支的</li>
<li>config配置文件</li>
<li>description仓库描述信息</li>
<li><code>HEAD</code>:指示目前被检出的分支</li>
<li><code>index</code>:文件保存暂存区信息</li>
</ul>
<h2 id="修改远程仓库"><a href="#修改远程仓库" class="headerlink" title="修改远程仓库"></a>修改远程仓库</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># way 1</span>
<span class="token function">git</span> remote set-url origin <span class="token punctuation">[</span>url<span class="token punctuation">]</span>
<span class="token comment"># way 2</span>
<span class="token function">git</span> remote <span class="token function">rm</span> origin
<span class="token function">git</span> remote <span class="token function">add</span> origin <span class="token punctuation">[</span>url<span class="token punctuation">]</span>
<span class="token comment"># way3</span>
修改 config 文件<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="高层命令"><a href="#高层命令" class="headerlink" title="高层命令"></a>高层命令</h2><h3 id="初始化仓库"><a href="#初始化仓库" class="headerlink" title="初始化仓库"></a>初始化仓库</h3><p><code>git init</code></p>
<h3 id="修改添加到暂存区"><a href="#修改添加到暂存区" class="headerlink" title="修改添加到暂存区"></a>修改添加到暂存区</h3><p><code>git add ./</code>,相当于如下操作:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 有多少文件改动,就执行多少次</span>
<span class="token function">git</span> hash-object <span class="token parameter variable">-w</span> 文件名
<span class="token function">git</span> update-index<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>!! git add .&#x2F; 先把工作区生成 git 对象,放到版本库,然后再放到暂存区</p>
</blockquote>
<h3 id="暂存区提交到版本库"><a href="#暂存区提交到版本库" class="headerlink" title="暂存区提交到版本库"></a>暂存区提交到版本库</h3><p><code>git commit -m &#39;comment&#39;</code>,相当于如下操作:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> write-tree
<span class="token function">git</span> commit-tree<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>也可以跳过暂存区提交,<code>git commit -a -m </code></p>
<h3 id="查看哪些修改没有暂存"><a href="#查看哪些修改没有暂存" class="headerlink" title="查看哪些修改没有暂存"></a>查看哪些修改没有暂存</h3><p><code>git diff</code>:没有暂存</p>
<p><code>git diff --staged</code>:查看哪些修改以及被暂存了,但没有提交</p>
<h3 id="查看提交历史记录"><a href="#查看提交历史记录" class="headerlink" title="查看提交历史记录"></a>查看提交历史记录</h3><p><code>git log --oneline</code>,打印出<code>hash值</code>是提交对象</p>
<h2 id="分支"><a href="#分支" class="headerlink" title="分支"></a>分支</h2><p>本质是一个<code>提交对象</code>,每次<code>git branch name</code>中的<code>name</code>,指针<code>HEAD</code>,就会根据<code>name</code>指向提交对象</p>
<p>如果要开发新功能,就新建一个<code>分支A</code>,写完再合<code>master</code>分支,正常来说<code>master</code>分支不会轻易给修改权限。</p>
<p>如果另一个新功能,和<code>分支A</code>同级、并行的,那就<code>切到master</code>分支,在<code>master</code>分支基础上,开<code>分支B</code>,进行新功能开发</p>
<blockquote>
<p>一般来说master 分支没有权限,需要自己重新写开一个分支,分支名用 nickname</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">C1 master
<span class="token operator">|</span>——C2——C3——C4 mozzie<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h3 id="分支列表"><a href="#分支列表" class="headerlink" title="分支列表"></a>分支列表</h3><p><code>git branch</code></p>
<h3 id="创建分支"><a href="#创建分支" class="headerlink" title="创建分支"></a>创建分支</h3><p><code>git branch 分支名</code>,并不会自动切换到分支</p>
<h3 id="切换分支"><a href="#切换分支" class="headerlink" title="切换分支"></a>切换分支</h3><blockquote>
<p>!! 最佳实践每次切换分之前git status 查一下,当前分支一定要是干净的</p>
</blockquote>
<p><code>git checkout 分支名</code></p>
<h3 id="合并分支"><a href="#合并分支" class="headerlink" title="合并分支"></a>合并分支</h3><blockquote>
<p>!! 做任何事情,确保做完了,再合并到 master 分支</p>
</blockquote>
<p>场景:需要增加功能<code>feat:#53</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># HEAD -> master新开一个分支</span>
<span class="token function">git</span> checkout <span class="token parameter variable">-b</span> <span class="token string">'feat53'</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>突然发现 bug需要修复<code>bug:#52</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># HEAD -> feat53先提交#53分支的工作</span>
<span class="token function">git</span> commit <span class="token parameter variable">-a</span> <span class="token parameter variable">-m</span> <span class="token string">'feat53 完成50%'</span>
<span class="token comment"># 切回 master 分支</span>
<span class="token function">git</span> checkout master
<span class="token comment"># HEAD -> master创建 issue52 分支</span>
<span class="token function">git</span> checkout <span class="token parameter variable">-b</span> <span class="token string">'issue52'</span>
<span class="token comment"># 改完了issue52</span>
<span class="token function">git</span> commit <span class="token parameter variable">-a</span> <span class="token parameter variable">-m</span> <span class="token string">'fix:issue52'</span>
<span class="token comment"># HEAD -> master</span>
<span class="token function">git</span> checkout master
<span class="token comment"># 合</span>
<span class="token function">git</span> merge issue52
<span class="token comment"># 删除 issue52分支(hash还在)</span>
<span class="token function">git</span> branch <span class="token parameter variable">-d</span> issue52<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>此时,由于<code>issue52</code>是在之前的 master 分支上生成的,故而<code>feat53</code>的分支仍然存在<code>issue52</code>的 bug所以有可能存在冲突需要手动解决</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># HEAD -> master</span>
<span class="token function">git</span> merge feat53
<span class="token comment"># 此处省略解决冲突</span>
<span class="token function">git</span> <span class="token function">add</span> ./
<span class="token function">git</span> commit <span class="token parameter variable">-m</span> <span class="token string">'fix:merge conflict'</span>
<span class="token comment"># 删除 feat53</span>
<span class="token function">git</span> branch <span class="token parameter variable">-d</span> feat53<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h3 id="删除分支"><a href="#删除分支" class="headerlink" title="删除分支"></a>删除分支</h3><p>查看哪些分支合并到当前分支,<code>git branch --merged</code>,这个列表中分支名字前没有*号的分支通常可以使用<code>git branch -d 分支名</code>删掉</p>
<p><code>git branch -D 分支名</code>,强制删除</p>
<h3 id="新建分支并指向指定提交对象"><a href="#新建分支并指向指定提交对象" class="headerlink" title="新建分支并指向指定提交对象"></a>新建分支并指向指定提交对象</h3><p><code>git branch name commitHash</code>,例<code>git log --oneline</code>如下:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">* hasfh2asd <span class="token number">1</span>.txt
* 1shfd2zsw <span class="token number">2</span>.txt
* 67rf73has <span class="token number">3</span>.txt
* 03uhr4rug <span class="token number">4</span>.txt<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<p>输入<code>git branch CCC 03uhr4rug</code>,那么会创建一个名为<code>CCC</code>的分支,并且<code>CCC</code>分支有<code>4.txt</code></p>
<blockquote>
<p>通常想看原来的某个版本的代码,就可以这样操作,看完,把这个分支删了</p>
</blockquote>
<h3 id="远程分支"><a href="#远程分支" class="headerlink" title="远程分支"></a>远程分支</h3><blockquote>
<p><code>git clone</code>下来的分支,默认就会建立一个<code>远程跟踪分支</code>(同步关系),例如 master 分支</p>
</blockquote>
<ul>
<li>本地分支</li>
</ul>
<p>场景一:如果想公开一个<code>share</code>分支 ,与他人共同写作:</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 过程中会生成生成一个远程跟踪分支 origin/share</span>
<span class="token function">git</span> push origin share<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<p>场景二:创建一个本地分支<code>b1</code>,直接跟踪远程分支<code>orgin/b1</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> checkout <span class="token parameter variable">-b</span> <span class="token string">'b1'</span> <span class="token string">'origin/b1'</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>场景三:已存在一个本地分支<code>dev</code>,改成远程跟踪分支</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># HEAD -> dev建立 本地分支 dev 与 远程分支 origin/dev 关系</span>
<span class="token function">git</span> branch <span class="token parameter variable">-u</span> origin/dev
<span class="token comment"># 这样就可以直接</span>
<span class="token function">git</span> push / <span class="token function">git</span> pull<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<ul>
<li>远程分支</li>
</ul>
<p>查看远程分支:<code>git remote -v</code></p>
<p>查看当前本地分支的远程跟踪分支:<code>git branch -vv</code></p>
<h2 id="远程分支删除-本地更新-prune"><a href="#远程分支删除-本地更新-prune" class="headerlink" title="远程分支删除, 本地更新 prune"></a>远程分支删除, 本地更新 prune</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 不加 --prune和 fetch 等价, 远程被删除的分支不会同步删除本地origin的分支</span>
<span class="token function">git</span> remote update origin <span class="token parameter variable">--prune</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span></span></code></pre>
<h1 id="提交规范"><a href="#提交规范" class="headerlink" title="提交规范"></a>提交规范</h1><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">type<span class="token punctuation">(</span>scope<span class="token punctuation">)</span>: subject
<span class="token comment"># 例如</span>
feat<span class="token punctuation">(</span>miniprogram<span class="token punctuation">)</span>: 增加了小程序模板消息相关功能<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>通常<code>type</code>有如下:</p>
<ul>
<li>feat - 新功能 feature</li>
<li>fix - 修复 bug</li>
<li>docs - 文档注释</li>
<li>style - 代码格式(不影响代码运行的变动)</li>
<li>refactor - 重构、优化(既不增加新功能,也不是修复 bug)</li>
<li>perf - 性能优化</li>
<li>test - 增加测试</li>
<li>chore - 构建过程或辅助工具的变动</li>
<li>revert - 回退</li>
<li>build - 打包</li>
</ul>
<h2 id="自动生成-Change-log"><a href="#自动生成-Change-log" class="headerlink" title="自动生成 Change log"></a>自动生成 Change log</h2><p>原理:利用 <code>child_process</code>获取 <code>git log</code>内容,处理字符串</p>
<pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript"><span class="token keyword">const</span> execSync <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"child_process"</span><span class="token punctuation">)</span><span class="token punctuation">.</span>execSync<span class="token punctuation">;</span> <span class="token comment">//同步子进程</span>
<span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"fs"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> process <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"process"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"path"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> inquirer <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"inquirer"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> dayjs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"dayjs"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> axios <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">"axios"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// env</span>
<span class="token keyword">const</span> isForCommanHuman <span class="token operator">=</span> process<span class="token punctuation">.</span>argv<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token string">"--common"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// changelog.md生成路径</span>
<span class="token keyword">const</span> outputpath <span class="token operator">=</span> path<span class="token punctuation">.</span><span class="token function">resolve</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span><span class="token function">cwd</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">"./changelog.md"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 非空检测</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>fs<span class="token punctuation">.</span><span class="token function">existsSync</span><span class="token punctuation">(</span>outputpath<span class="token punctuation">)</span><span class="token punctuation">)</span> fs<span class="token punctuation">.</span><span class="token function">writeFile</span><span class="token punctuation">(</span>outputpath<span class="token punctuation">,</span> <span class="token string">""</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 华丽的gitlog日志</span>
<span class="token keyword">const</span> <span class="token function-variable function">perfectGitLog</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">startTime<span class="token punctuation">,</span> endTime</span><span class="token punctuation">)</span> <span class="token operator">=></span>
isForCommanHuman
<span class="token operator">?</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">git log --since="</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>startTime<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">" --until="</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>endTime<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">" --no-merges --pretty=format:"%cr %C(cyan)%s"</span><span class="token template-punctuation string">`</span></span>
<span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">git log --since="</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>startTime<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">" --until="</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">$&#123;</span>endTime<span class="token interpolation-punctuation punctuation">&#125;</span></span><span class="token string">" --no-merges --pretty=format:"%C(yellow)%h %C(green)%cn %C(redz)(%cr:%ci) %C(cyan)%s"</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
<span class="token keyword">const</span> wxrobotHook <span class="token operator">=</span>
<span class="token string">"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=be4d4473-c290-4ddd-a089-41df3ed1d601"</span><span class="token punctuation">;</span>
<span class="token comment">// 当前时间</span>
<span class="token keyword">const</span> now <span class="token operator">=</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 2周前</span>
<span class="token keyword">const</span> weeks_2_ago <span class="token operator">=</span> now <span class="token operator">-</span> <span class="token number">14</span> <span class="token operator">*</span> <span class="token number">24</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">;</span>
inquirer
<span class="token punctuation">.</span><span class="token function">prompt</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
<span class="token punctuation">&#123;</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"input"</span><span class="token punctuation">,</span>
<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"startDate"</span><span class="token punctuation">,</span>
<span class="token literal-property property">message</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">起始时间,默认13天前 \n</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
<span class="token keyword">default</span><span class="token operator">:</span> <span class="token function">dayjs</span><span class="token punctuation">(</span>weeks_2_ago<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"YYYY.MM.DD"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token function-variable function">validate</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">val</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d&#123;4&#125;.\d&#123;2&#125;.\d&#123;2&#125;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#123;</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"input"</span><span class="token punctuation">,</span>
<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"endDate"</span><span class="token punctuation">,</span>
<span class="token literal-property property">message</span><span class="token operator">:</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">结束时间,默认今天 \n</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span>
<span class="token keyword">default</span><span class="token operator">:</span> <span class="token function">dayjs</span><span class="token punctuation">(</span>now<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">"YYYY.MM.DD"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token function-variable function">validate</span><span class="token operator">:</span> <span class="token punctuation">(</span><span class="token parameter">val</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d&#123;4&#125;.\d&#123;2&#125;.\d&#123;2&#125;</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>val<span class="token punctuation">)</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#123;</span>
<span class="token literal-property property">type</span><span class="token operator">:</span> <span class="token string">"rawlist"</span><span class="token punctuation">,</span>
<span class="token literal-property property">message</span><span class="token operator">:</span> <span class="token string">"是否通知到微信机器人:"</span><span class="token punctuation">,</span>
<span class="token literal-property property">name</span><span class="token operator">:</span> <span class="token string">"notifyWxrobot"</span><span class="token punctuation">,</span>
<span class="token literal-property property">choices</span><span class="token operator">:</span> <span class="token punctuation">[</span><span class="token string">"Y"</span><span class="token punctuation">,</span> <span class="token string">"N"</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">answers</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
<span class="token keyword">const</span> <span class="token punctuation">&#123;</span> startDate<span class="token punctuation">,</span> endDate<span class="token punctuation">,</span> notifyWxrobot <span class="token punctuation">&#125;</span> <span class="token operator">=</span> answers<span class="token punctuation">;</span>
<span class="token keyword">const</span> rowTemplate <span class="token operator">=</span> <span class="token function">perfectGitLog</span><span class="token punctuation">(</span>startDate<span class="token punctuation">,</span> endDate<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">let</span> fmt <span class="token operator">=</span> <span class="token function">execSync</span><span class="token punctuation">(</span>rowTemplate<span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">feat: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"✅: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">fix: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"🐛: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">chore: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"🎨: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">perf: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"⚡: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">docs: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"📝: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">refactor: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"🔨: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">anno: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"🔖: "</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">style: </span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">,</span> <span class="token string">"👷: "</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
fs<span class="token punctuation">.</span><span class="token function">writeFileSync</span><span class="token punctuation">(</span>outputpath<span class="token punctuation">,</span> fmt<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span><span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// 通知微信群聊机器人</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>notifyWxrobot <span class="token operator">===</span> <span class="token string">"Y"</span><span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
axios<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span>wxrobotHook<span class="token punctuation">,</span> <span class="token punctuation">&#123;</span>
<span class="token literal-property property">msgtype</span><span class="token operator">:</span> <span class="token string">"markdown"</span><span class="token punctuation">,</span>
<span class="token literal-property property">markdown</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
<span class="token literal-property property">content</span><span class="token operator">:</span> fmt<span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">,</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span>
<span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">error</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">&#123;</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">.</span>isTtyError<span class="token punctuation">)</span> <span class="token punctuation">&#123;</span>
<span class="token comment">// Prompt couldn't be rendered in the current environment</span>
<span class="token punctuation">&#125;</span> <span class="token keyword">else</span> <span class="token punctuation">&#123;</span>
<span class="token comment">// Something else went wrong</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="使用-husky-eslint-规范提交"><a href="#使用-husky-eslint-规范提交" class="headerlink" title="使用 husky+eslint 规范提交"></a>使用 husky+eslint 规范提交</h2><blockquote>
<p>需要配合 eslint</p>
</blockquote>
<p><code>git init</code>后,<code>yarn add husky</code>,在<code>package.json</code>配置</p>
<pre class="line-numbers language-json" data-language="json"><code class="language-json"><span class="token punctuation">&#123;</span>
<span class="token property">"husky"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
<span class="token property">"hooks"</span><span class="token operator">:</span> <span class="token punctuation">&#123;</span>
<span class="token property">"pre-commit"</span><span class="token operator">:</span> <span class="token string">"npm run lint"</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span>
<span class="token punctuation">&#125;</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h2 id="生成-ssh-key"><a href="#生成-ssh-key" class="headerlink" title="生成 ssh key"></a>生成 ssh key</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">ssh-keygen <span class="token parameter variable">-t</span> rsa <span class="token parameter variable">-C</span> <span class="token string">"himozzie@foxmail.com"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h2 id="查看-ssh-公钥"><a href="#查看-ssh-公钥" class="headerlink" title="查看 ssh 公钥"></a>查看 ssh 公钥</h2><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">cat</span> ~/.ssh/id_rsa.pub<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h1 id="git-代理"><a href="#git-代理" class="headerlink" title="git 代理"></a>git 代理</h1><blockquote>
<p>前提条件是开了扶墙工具</p>
</blockquote>
<p>git clone 拉取方式选择 http&#x2F;https(默认会让你输入账号密码,比较蛋疼),不要选择 ssl 拉取</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> config <span class="token parameter variable">--global</span> http.proxy <span class="token string">'http://127.0.0.1:1081'</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> https.proxy <span class="token string">'https://127.0.0.1:1081'</span>
<span class="token comment"># 清除</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> http.proxy
<span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token parameter variable">--unset</span> https.proxy<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<h1 id="终端临时代理"><a href="#终端临时代理" class="headerlink" title="终端临时代理"></a>终端临时代理</h1><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># cmd临时代理方案cmd窗口关闭则代理失效</span>
<span class="token builtin class-name">set</span> <span class="token assign-left variable">http_proxy</span><span class="token operator">=</span>http://127.0.0.1:50015
<span class="token builtin class-name">set</span> <span class="token assign-left variable">https_proxy</span><span class="token operator">=</span>http://127.0.0.1:50015<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<h1 id="git-钩子-hooks"><a href="#git-钩子-hooks" class="headerlink" title="git 钩子(hooks)"></a>git 钩子(hooks)</h1><p>原理:项目 git push 到远程仓库,远程仓库的钩子 post 通知 www&#x2F;wwwroot 下的站点 pull 远程仓库</p>
<blockquote>
<p>git 钩子需要 git 服务和 pull 在同一环境中</p>
</blockquote>
<ul>
<li>创建远程仓库,配置钩子,git post-receive 钩子</li>
</ul>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/bash</span>
<span class="token builtin class-name">unset</span> <span class="token variable"><span class="token variable">$(</span><span class="token function">git</span> rev-parse --local-env-vars<span class="token variable">)</span></span><span class="token punctuation">;</span>
<span class="token comment"># post-receive接收到pull指令后,执行bash命令</span>
<span class="token builtin class-name">cd</span> /www/wwwroot/doc.mozzie.cn/ <span class="token operator">&amp;&amp;</span> <span class="token function">git</span> pull origin master<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>
<blockquote>
<p>web 目录下 doc.mozzie.cn 是网站目录,本地项目编译打包后,直接 git push 的目录</p>
</blockquote>
<h2 id="web-钩子"><a href="#web-钩子" class="headerlink" title="web 钩子"></a>web 钩子</h2><p>待耍</p>
<h1 id="项目"><a href="#项目" class="headerlink" title="项目"></a>项目</h1><h2 id="统计项目代码行数"><a href="#统计项目代码行数" class="headerlink" title="统计项目代码行数"></a>统计项目代码行数</h2><p>统计所有人代码增删量,拷贝如下命令, git bash 终端git 项目某分支下执行</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> log <span class="token parameter variable">--format</span><span class="token operator">=</span><span class="token string">'%aN'</span> <span class="token operator">|</span> <span class="token function">sort</span> <span class="token parameter variable">-u</span> <span class="token operator">|</span> <span class="token keyword">while</span> <span class="token builtin class-name">read</span> name<span class="token punctuation">;</span> <span class="token keyword">do</span> <span class="token builtin class-name">echo</span> <span class="token parameter variable">-en</span> <span class="token string">"<span class="token variable">$name</span><span class="token entity" title="\t">\t</span>"</span><span class="token punctuation">;</span> <span class="token function">git</span> log <span class="token parameter variable">--author</span><span class="token operator">=</span><span class="token string">"<span class="token variable">$name</span>"</span> <span class="token parameter variable">--pretty</span><span class="token operator">=</span>tformat: <span class="token parameter variable">--numstat</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'&#123; add += $1; subs += $2; loc += $1 - $2 &#125; END &#123; printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc &#125;'</span> -<span class="token punctuation">;</span> <span class="token keyword">done</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h2 id="统计制定提交者代码量"><a href="#统计制定提交者代码量" class="headerlink" title="统计制定提交者代码量"></a>统计制定提交者代码量</h2><p>替换<code>username</code>为提交者的名称</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> log <span class="token parameter variable">--author</span><span class="token operator">=</span><span class="token string">"username"</span> <span class="token parameter variable">--pretty</span><span class="token operator">=</span>tformat: <span class="token parameter variable">--numstat</span> <span class="token operator">|</span> <span class="token function">awk</span> <span class="token string">'&#123; add += $1; subs += $2; loc += $1 - $2 &#125; END &#123; printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc &#125;'</span> -<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<h1 id="搭建-gogs"><a href="#搭建-gogs" class="headerlink" title="搭建 gogs"></a>搭建 gogs</h1><p>gogs 官网下载,压缩包解压到<code>www/wwwroot/</code></p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token builtin class-name">cd</span> /www/wwwroot/gogs
<span class="token comment"># 启服务,默认3000的端口被占用则 ./gogs web -port 3001</span>
./gogs web<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p>浏览器访问<code>http://yourip:3000/install</code>,注意服务器安全组放行 3000 端口</p>
<ul>
<li>为 gogs 添加 mysql 数据库</li>
<li>配置 mysql</li>
<li>配置 gogs 相关信息</li>
<li>配置其他信息</li>
</ul>
<p>停掉<code>./gogs web</code></p>
<p>在刚安装的 gogs 路径下,找到<code>/gogs/scripts/systemd/gogs.service</code> 文件复制一份</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token assign-left variable">User</span><span class="token operator">=</span>root
<span class="token assign-left variable">Group</span><span class="token operator">=</span>root
<span class="token assign-left variable">WorkingDirectory</span><span class="token operator">=</span>/www/wwwroot/git.mozzie.cn
<span class="token assign-left variable">ExecStart</span><span class="token operator">=</span>/www/wwwroot/git.mozzie.cn/gogs web
<span class="token assign-left variable">Restart</span><span class="token operator">=</span>always
<span class="token assign-left variable">Environment</span><span class="token operator">=</span><span class="token environment constant">USER</span><span class="token operator">=</span>root <span class="token assign-left variable"><span class="token environment constant">HOME</span></span><span class="token operator">=</span>/www/wwwroot/git.mozzie.cn<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>将修改好的<code>gogs.service</code>文件上传到<code>/etc/systemd/system</code>下,并执行以下命令来激活 gogs</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">sudo</span> systemctl <span class="token builtin class-name">enable</span> gogs
<span class="token comment"># 启动gogs</span>
<span class="token function">sudo</span> systemctl start gogs<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>
<p><code>/gogs/custom/conf/app.ini</code>,修改该文件可以自定义配置,安装步骤填错了,可以这里修改,重启 gogs 服务即可</p>
<h2 id="配置-gogs-GIT-钩子"><a href="#配置-gogs-GIT-钩子" class="headerlink" title="配置 gogs GIT 钩子"></a>配置 gogs GIT 钩子</h2><p>场景:以 Gogs 为例,先 git push <code>A项目</code> 到远程仓库 <code>REPO</code>,服务端 web 目录<code>webB</code>文件夹触发钩子执行 git pull</p>
<p>1、gogs 初始化一个仓库<code>REPO</code>,仓库设置&#x3D;&gt;管理 GIT 钩子&#x3D;&gt;post-receive填入</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token shebang important">#!/bin/sh</span>
<span class="token builtin class-name">export</span> <span class="token assign-left variable">GIT_WORK_TREE</span><span class="token operator">=</span>/www/wwwroot/webB
<span class="token builtin class-name">export</span> <span class="token assign-left variable">GIT_DIR</span><span class="token operator">=</span><span class="token variable">$&#123;GIT_WORK_TREE&#125;</span>/.git
<span class="token builtin class-name">cd</span> <span class="token variable">$&#123;GIT_WORK_TREE&#125;</span>
<span class="token function">git</span> pull<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>2、利用 ssh 工具登录服务器</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">ssh</span> root@mozzie.cn<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>3、生成 ssh key</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash">ssh-keygen <span class="token parameter variable">-t</span> rsa <span class="token parameter variable">-C</span> <span class="token string">"himozzie@foxmail.com"</span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>4、配置公钥到 gogs</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">cat</span> ~/.ssh/id_rsa.pub<span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>
<p>把打印出的公钥,配置到 ssh 密钥</p>
<p>5、最后一步</p>
<p>配置好服务端的公钥后,就可以无需用户名密码,<code>cd /webB</code>目录下,执行<code>git pull</code>,此后每次<code>git push A项目</code>,服务端都会触发<code>GIT钩子</code>,自动从<code>REPO</code>拉取最新的仓库文件</p>
2023-11-08 13:45:34 +08:00
</article>
2023-11-06 16:05:27 +08:00
<link rel="stylesheet" href="/css/80d65618.css">
<div class="copyright">
2023-11-08 13:45:34 +08:00
<a target="_blank" href="https://mozzie.cn/2023/11/06/clopc6c5n000hibz3g71savqe/">
2023-11-06 16:05:27 +08:00
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M3 19a9 9 0 0 1 9 0a9 9 0 0 1 9 0"></path>
<path d="M3 6a9 9 0 0 1 9 0a9 9 0 0 1 9 0"></path>
<path d="M3 6v13"></path>
<path d="M12 6v13"></path>
<path d="M21 6v13"></path>
</g>
</svg>
<span>Git</span>
</a>
<ul>
<li>
<span>Author</span>
<p> Mozzie</p>
</li>
<li>
<span>Published on</span>
<p>2023-11-06</p>
</li>
<li>
<span>License</span>
<p><a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a></p>
</li>
</ul>
</div>
<script src="/js/31d6cfe0.js"></script>
<!-- 评论 -->
<div id="vcomments"></div>
</div>
</main>
</div>
<div class="meta-container">
<div class="toc-wrapper content-dialog">
<p class="catalog">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 24 24">
<g fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
<path d="M4 6h16"></path>
<path d="M4 12h16"></path>
<path d="M4 18h12"></path>
</g>
</svg>
<span>Catalog</span>
</p>
<!-- 文章toc -->
<ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#GIT-%E6%9C%80%E4%BD%B3%E5%AE%9E%E8%B7%B5"><span class="toc-number">1.</span> <span class="toc-text">GIT 最佳实践</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E7%94%A8%E6%88%B7"><span class="toc-number">1.1.</span> <span class="toc-text">配置用户</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#alias-%E5%88%AB%E5%90%8D"><span class="toc-number">1.2.</span> <span class="toc-text">alias 别名</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#git-%E6%96%87%E4%BB%B6%E7%BB%93%E6%9E%84"><span class="toc-number">1.3.</span> <span class="toc-text">.git 文件结构</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93"><span class="toc-number">1.4.</span> <span class="toc-text">修改远程仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AB%98%E5%B1%82%E5%91%BD%E4%BB%A4"><span class="toc-number">1.5.</span> <span class="toc-text">高层命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9D%E5%A7%8B%E5%8C%96%E4%BB%93%E5%BA%93"><span class="toc-number">1.5.1.</span> <span class="toc-text">初始化仓库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E6%B7%BB%E5%8A%A0%E5%88%B0%E6%9A%82%E5%AD%98%E5%8C%BA"><span class="toc-number">1.5.2.</span> <span class="toc-text">修改添加到暂存区</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9A%82%E5%AD%98%E5%8C%BA%E6%8F%90%E4%BA%A4%E5%88%B0%E7%89%88%E6%9C%AC%E5%BA%93"><span class="toc-number">1.5.3.</span> <span class="toc-text">暂存区提交到版本库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%93%AA%E4%BA%9B%E4%BF%AE%E6%94%B9%E6%B2%A1%E6%9C%89%E6%9A%82%E5%AD%98"><span class="toc-number">1.5.4.</span> <span class="toc-text">查看哪些修改没有暂存</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95"><span class="toc-number">1.5.5.</span> <span class="toc-text">查看提交历史记录</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E6%94%AF"><span class="toc-number">1.6.</span> <span class="toc-text">分支</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%86%E6%94%AF%E5%88%97%E8%A1%A8"><span class="toc-number">1.6.1.</span> <span class="toc-text">分支列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%88%86%E6%94%AF"><span class="toc-number">1.6.2.</span> <span class="toc-text">创建分支</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%87%E6%8D%A2%E5%88%86%E6%94%AF"><span class="toc-number">1.6.3.</span> <span class="toc-text">切换分支</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%88%E5%B9%B6%E5%88%86%E6%94%AF"><span class="toc-number">1.6.4.</span> <span class="toc-text">合并分支</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%88%86%E6%94%AF"><span class="toc-number">1.6.5.</span> <span class="toc-text">删除分支</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E5%BB%BA%E5%88%86%E6%94%AF%E5%B9%B6%E6%8C%87%E5%90%91%E6%8C%87%E5%AE%9A%E6%8F%90%E4%BA%A4%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.6.6.</span> <span class="toc-text">新建分支并指向指定提交对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF"><span class="toc-number">1.6.7.</span> <span class="toc-text">远程分支</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94
</div>
</div>
</div>
<script>
2023-11-08 10:11:56 +08:00
window.page = {
use: ''
}
2023-11-06 16:05:27 +08:00
window.katex = {
2023-11-08 10:11:56 +08:00
enable: "",
2023-11-06 16:05:27 +08:00
jsCdn: "//cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.js",
cssCdn: "//cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css"
}
window.mermaid = {
2023-11-08 10:11:56 +08:00
enable: "",
2023-11-06 16:05:27 +08:00
cdn: "//cdn.jsdelivr.net/npm/mermaid@10.4.0/dist/mermaid.min.js",
}
window.valine = {
2023-11-08 10:11:56 +08:00
enable: "",
2023-11-06 16:05:27 +08:00
appId: 'TisMit6uhflounFqAN3ZGjgq-MdYXbMMI',
appKey: 'CdjirjYdz07U5i62ElsJvXUh',
avatar: 'monsterid',
cdn: '//unpkg.com/valine@latest/dist/Valine.min.js',
serverURLs: '//tismit6u.api.lncldglobal.com'
};
</script>
2023-11-08 10:11:56 +08:00
<script src="/js/24e1d5fd.js"></script>
2023-11-06 16:05:27 +08:00
</main>
</body>
<script>
window.i18n = {
"tip-collapse": "Collapse",
"tip-expand": "Expand",
"text-select": "select",
"text-move": "movement",
"text-esc": "close",
"tip-status-done": "Done",
"tip-status-doing": "In Progress",
"tip-status-todo": "Todo",
"tip-status-other": "Duplicate",
"tip-status-default": "Default",
"tip-roadmap-today": "Positioning to the today",
"text-search": "Search...",
"text-today": "Today",
// month
"January": "January",
"February": "February",
"March": "March",
"April": "April",
"May": "May",
"June": "June",
"July": "July",
"August": "August",
"September": "September",
"October": "October",
"November": "November",
"December": "December",
}
</script>
2023-11-06 16:14:34 +08:00
<script src="/js/58c91c4e.js"></script>
2023-11-06 16:05:27 +08:00
</html>