{"id":1478,"date":"2024-12-03T21:32:56","date_gmt":"2024-12-03T13:32:56","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=1478"},"modified":"2025-10-09T13:54:44","modified_gmt":"2025-10-09T05:54:44","slug":"agt-01","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2024\/12\/03\/agt-01\/","title":{"rendered":"Advanced Git Tutorial 01"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u5de5\u4f5c\u6d41\u7a0b<\/h2>\n<p>&emsp;&emsp;\u4e3a\u4e86\u4e86\u89e3git\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u77e5\u9053\u4ee3\u7801\u5728\u4ec0\u4e48\u5730\u65b9\u3002\u5bf9\u4e8e\u4f7f\u7528git\u7ba1\u7406\u7684\u9879\u76ee\uff0c\u4e3b\u8981\u4f1a\u6709\u5982\u4e0b4\u4e2a\u4f4d\u7f6e\u7528\u6765\u5b58\u653e\u4ee3\u7801\uff1a<\/p>\n<ol>\n<li>Working Directory: \u6211\u4eec\u672c\u5730\u7f16\u8f91\u4ee3\u7801\u7684\u4f4d\u7f6e\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u4ece\u7f16\u8f91\u5668\u4e2d\u770b\u89c1\u7684\u4ee3\u7801\uff1b<\/li>\n<li>Staging Area: Committing\u4e4b\u524d\u7684\u7684\u4e34\u65f6\u4fdd\u5b58\u70b9\uff1b<\/li>\n<li>Local Repository: \u672c\u5730\u5b58\u50a8\u66f4\u6539\u7684\u5730\u65b9\uff1b<\/li>\n<li>Remote Repository: \u7c7b\u4f3cGithub\u7528\u4e8e\u5907\u4efd\u3001\u5171\u4eab\u7684\u670d\u52a1\u5668\u3002<\/li>\n<\/ol>\n<p>&emsp;&emsp;\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e0b\u5e38\u7528\u7684git\u547d\u4ee4\u662f\u5982\u4f55\u5728\u8fd9\u56db\u4e2a\u5730\u65b9\u79fb\u52a8\u4ee3\u7801\u7684\u3002<\/p>\n<figure style=\"width: 841px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/01_clone.drawio.svg\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"841\" height=\"523\" alt=\"\u56fe1\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/01_clone.drawio.svg\" width=\"841\" height=\"523\" alt=\"\u56fe1\" class=\"size-full\" \/><\/noscript> \u56fe1\uff1aGit\u7684\u5de5\u4f5c\u6d41\u7a0b<\/figcaption><\/figure>\n<h3>1.1 clone<\/h3>\n<p>&emsp;&emsp;\u5f53\u6211\u4eec\u4f7f\u7528<code>git clone<\/code>\u7684\u65f6\u5019\uff0c\u4ee3\u7801\u4ece\u8fdc\u7a0b\u4ed3\u5e93\u62f7\u8d1d\u5230\u672c\u5730\u4ed3\u5e93\u3002<\/p>\n<h3>1.2 checkout<\/h3>\n<p>&emsp;&emsp;\u5c06\u672c\u5730\u4ed3\u5e93\u4e2d\u7684\u4ee3\u7801\u5207\u6362\u5230\u6211\u4eec\u7684\u672c\u5730\u6587\u4ef6\uff0c\u4f7f\u5f97\u6211\u4eec\u53ef\u4ee5\u5728\u7f16\u8f91\u5668\u4e2d\u67e5\u770b\u3002\u901a\u5e38\u6765\u8bf4\uff0c\u4e00\u822c\u4f7f\u7528<code>git clone<\/code>\u5305\u542b\u4e86\u5207\u6362\u5230<code>master<\/code>\u5206\u652f\u8fd9\u4e00\u9009\u9879\uff0c\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u672c\u5730\u627e\u5230\u76f8\u5e94\u7684\u6587\u4ef6\uff0c\u4e0d\u9700\u8981\u624b\u52a8\u7684<code>checkout<\/code>\u4e00\u6b21\u3002<\/p>\n<h3>1.3 add<\/h3>\n<p>&emsp;&emsp;\u5c06\u4ee3\u7801\u6dfb\u52a0\u5230\u6682\u5b58\u533a\uff0c\u6b64\u65f6\u4e0d\u5f71\u54cd\u672c\u5730\u6216\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/p>\n<h3>1.4 commit<\/h3>\n<p>&emsp;&emsp;\u5c06\u6682\u5b58\u533a\u4e2d\u6240\u6709\u6587\u4ef6\u63d0\u4ea4\u5230\u672c\u5730\u4ed3\u5e93\uff0c\u5e76\u751f\u6210\u76f8\u5e94\u7684\u63d0\u4ea4\u8bb0\u5f55\u3002<\/p>\n<h3>1.5 push<\/h3>\n<p>&emsp;&emsp;\u5c06\u672c\u5730\u4ed3\u5e93\u7684\u8bb0\u5f55\u540c\u6b65\u5230\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/p>\n<h3>1.6 fetch<\/h3>\n<p>&emsp;&emsp;\u5c06\u8fdc\u7a0b\u4ed3\u5e93\u7684\u8bb0\u5f55\u540c\u6b65\u5230\u672c\u5730\u3002<\/p>\n<h3>1.7 merge<\/h3>\n<p>&emsp;&emsp;\u5c06\u672c\u5730\u4ed3\u5e93\u7684\u4ee3\u7801\u4e0e\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u4ee3\u7801\u8fdb\u884c\u5408\u5e76\u3002<\/p>\n<h3>1.8 pull<\/h3>\n<p>&emsp;&emsp;\u76f8\u5f53\u4e8e<code>fetch<\/code> + <code>merge<\/code>\uff0c\u76f4\u63a5\u5c06\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u7684\u4ee3\u7801\u548c\u8fdc\u7a0b\u4ed3\u5e93\u7684\u4ee3\u7801\u5408\u5e76\u3002<\/p>\n<h2>\u4e8c\u3001\u5206\u652f\u5408\u5e76<\/h2>\n<p>&emsp;&emsp;<code>git checkout<\/code>\u548c<code>git switch<\/code>\u5141\u8bb8\u6211\u4eec\u5207\u6362\u5206\u652f\uff1a<\/p>\n<figure style=\"width: 561px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/02_chechkout.drawio.svg\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"561\" height=\"241\" alt=\"\u56fe2\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/02_chechkout.drawio.svg\" width=\"561\" height=\"241\" alt=\"\u56fe2\" class=\"size-full\" \/><\/noscript> \u56fe2\uff1aCheckout and Switch<\/figcaption><\/figure>\n<p>&emsp;&emsp;<code>git merge<\/code>\u5141\u8bb8\u6211\u4eec\u5408\u5e76\u5206\u652f\uff0c\u53c2\u6570<code>--no-ff<\/code>\u6307\u5b9a\u4e86\u4e0d\u4f7f\u7528fast forward\u7684\u5408\u5e76\u65b9\u5f0f\u3002<\/p>\n<figure style=\"width: 1008px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/03_merge.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"1008\" height=\"836\" alt=\"\u56fe3\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/03_merge.png\" width=\"1008\" height=\"836\" alt=\"\u56fe3\" class=\"size-full\" \/><\/noscript> \u56fe3\uff1aMerge<\/figcaption><\/figure>\n<p>&emsp;&emsp;<code>git rebase<\/code>\u5219\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u79cd\u663e\u5f0f\u7684<code>ff<\/code>\u8fdb\u884c\u5408\u5e76\uff1a<\/p>\n<figure style=\"width: 882px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/04_rebase.drawio.svg\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"882\" height=\"962\" alt=\"\u56fe4\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/04_rebase.drawio.svg\" width=\"882\" height=\"962\" alt=\"\u56fe4\" class=\"size-full\" \/><\/noscript> \u56fe4\uff1aRebase<\/figcaption><\/figure>\n<h2>\u4e09\u3001\u51b7\u95e8\u4f46\u662f\u6709\u7528<\/h2>\n<h3>3.1 git blame<\/h3>\n<ol>\n<li><code>git blame &lt;filename&gt;<\/code>\uff0c\u9010\u884c\u663e\u793a\u6307\u5b9a\u6587\u4ef6\u7684\u6bcf\u4e00\u884c\u4ee3\u7801\u662f\u7531\u8c01\u5728\u4ec0\u4e48\u65f6\u5019\u5f15\u5165\u6216\u4fee\u6539\u7684\uff1b<\/li>\n<li><code>git blame &lt;filename&gt; -L x,y<\/code>\uff0c\u6307\u5b9a\u67e5\u770b\u8303\u56f4\u884c\u53f7\u7684\u4fee\u6539\uff1b<\/li>\n<li><code>git blame &lt;filename&gt; -w<\/code>\uff0c\u5ffd\u7565\u7a7a\u683c\uff1b<\/li>\n<li><code>git blame &lt;filename&gt; -C<\/code>\uff0c\u68c0\u6d4b\u5728\u540c\u4e00\u63d0\u4ea4\u4e2d\u79fb\u52a8\u6216\u590d\u5236\u7684\u884c\uff1b<\/li>\n<li><code>git blame &lt;filename&gt; -C -C<\/code>\uff0c\u68c0\u6d4b\u5728\u540c\u4e00\u4e2a\u63d0\u4ea4\u6216\u521b\u5efa\u6587\u4ef6\u7684\u63d0\u4ea4\u4e2d\u79fb\u52a8\u6216\u590d\u5236\u7684\u884c;<\/li>\n<li><code>git blame &lt;filename&gt; -C -C -C<\/code>\uff0c\u68c0\u6d4b\u5728\u540c\u4e00\u4e2a\u63d0\u4ea4\u6216\u521b\u5efa\u6587\u4ef6\u7684\u63d0\u4ea4\u6216\u4efb\u4f55\u63d0\u4ea4\u4e2d\u79fb\u52a8\u6216\u590d\u5236\u7684\u884c\u3002<\/li>\n<\/ol>\n<p>&emsp;&emsp;\u56e0\u6b64\u4f7f\u7528<code>git blame &lt;filename&gt; -w -C -C -C<\/code>\uff0c\u53ef\u4ee5\u66f4\u597d\u7684\u67e5\u8be2\u5230\u4ee3\u7801\u7684\u201c\u6700\u7ec8\u201d\u8d23\u4efb\u4eba\u3002<\/p>\n<h3>3.2 git log<\/h3>\n<ol>\n<li><code>git log -p &lt;hash\/name&gt;<\/code>\uff0c\u67e5\u770b\u63d0\u4ea4\u7684\u5177\u4f53\u8865\u4e01\uff1b<\/li>\n<li><code>git log -S &lt;str&gt;<\/code>\uff0c\u67e5\u627e\u548cstr\u6709\u5173\u7684\u63d0\u4ea4\uff1b<\/li>\n<li><code>git log -S &lt;str&gt; -p<\/code>\uff0c\u67e5\u627e\u548cstr\u6709\u5173\u7684\u63d0\u4ea4\uff0c\u5e76\u663e\u793a\u5176\u5177\u4f53\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p>&emsp;&emsp;<code>git log -S &lt;str&gt; -p<\/code>\u7528\u4e8e\u67e5\u627e\u4ed3\u5e93\u4e2d\u5386\u53f2\u66f4\u6539\uff0c\u5c24\u5176\u662f\u5220\u9664\u7684\u5185\u5bb9\u5341\u5206\u6709\u6548\u3002<\/p>\n<h3>3.3 git reflog<\/h3>\n<p>&emsp;&emsp;<code>git reflog<\/code>\u5141\u8bb8\u6211\u4eec\u67e5\u770b\u5f53\u524dHEAD\u7684\u6240\u6709git\u64cd\u4f5c\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<pre><code class=\"language-log\">0b98876 (HEAD -&gt; dev) HEAD@{0}: rebase (finish): returning to refs\/heads\/dev\n0b98876 (HEAD -&gt; dev) HEAD@{1}: rebase (pick): up1\n003600d (master) HEAD@{2}: rebase (start): checkout master\n5bba514 HEAD@{3}: checkout: moving from master to dev\n003600d (master) HEAD@{4}: commit: m\nffb409c HEAD@{5}: checkout: moving from dev to master\n5bba514 HEAD@{6}: commit: up2\nef04433 HEAD@{7}: commit: up1\nffb409c HEAD@{8}: checkout: moving from master to dev\nffb409c HEAD@{9}: commit: test\n37f33ca (origin\/master, origin\/HEAD) HEAD@{10}: pull: Fast-forward<\/code><\/pre>\n<h3>3.4 git diff<\/h3>\n<p>&emsp;&emsp;\u9ed8\u8ba4\u7684<code>git diff<\/code>\u4f1a\u6309\u7167<strong>\u884c<\/strong>\u7ed9\u51fadiff\uff0c\u5982\u679c\u6211\u4eec\u52a0\u4e0a\u53c2\u6570<code>--word-diff<\/code>\u5219\u4f1a\u6309\u7167\u201c\u8bcd\/\u5217\u201d\u7ed9\u51fadiff\u3002<\/p>\n<h3>3.5 rerere<\/h3>\n<p>see\uff1a<a href=https:\/\/git-scm.com\/book\/zh\/v2\/Git-%E5%B7%A5%E5%85%B7-Rerere target=\"_blank\"  rel=\"nofollow\" >git-scm<\/a><\/p>\n<h3>3.6 git branch<\/h3>\n<p>&emsp;&emsp;<code>git branch<\/code>\u53ef\u4ee5\u67e5\u770b\u5206\u652f\uff0c<code>git branch --column<\/code>\u5141\u8bb8\u6211\u4eec\u6309\u7167\u591a\u4e2a\u5217\u7684\u65b9\u5f0f\u67e5\u770b\u3002\u540c\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre><code>git config --global column.ui auto\n\ngit config --global branch.sort -committerdate<\/code><\/pre>\n<h3>3.7 git push<\/h3>\n<p>&emsp;&emsp;<code>git push --force-with-lease<\/code>\uff0c\u4f1a\u68c0\u67e5\u5f53\u524d\u7684\u8282\u70b9\u662f\u5426\u662fremote\u4e0a\u7684\u6700\u540e\u4e00\u4e2a\u8282\u70b9\uff08\u6700\u65b0\u7684\uff09\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u5219\u53ef\u4ee5\u8fdb\u884cpush\u3002\u5e38\u7528\u4e8erebase\u4e4b\u540e\u7684\u63d0\u4ea4\u3002<\/p>\n<h3>3.8 git maintenance<\/h3>\n<p>&emsp;&emsp;\u5141\u8bb8\u5728\u540e\u53f0\u81ea\u52a8\u7ef4\u62a4\u9879\u76ee\uff0c\u4f8b\u5982\uff0c\u5b83\u53ef\u4ee5\u6267\u884c\u5982\u4e0b\u4efb\u52a1\uff1a<\/p>\n<ol>\n<li>commit-graph<\/li>\n<li>prefetch<\/li>\n<li>gc<\/li>\n<li>loose-objects<\/li>\n<li>incremental-repack<\/li>\n<li>pack-refs<\/li>\n<\/ol>\n<h3>3.9 git add<\/h3>\n<p>&emsp;&emsp;\u5047\u8bbe\u6211\u4eec\u7684\u4e00\u4e2a\u6587\u4ef6\u5176\u505a\u4e86\u591a\u4e2a\u90e8\u5206\u7684\u4fee\u6539\uff0c\u5e76\u4e14\u5f15\u5165\u4e86\u6211\u4eec\u81ea\u5df1\u7684\u8c03\u8bd5\u8f93\u51fa\u3002\u76f4\u63a5\u4f7f\u7528add\u4f1a\u6dfb\u52a0\u5168\u90e8\u7684\u4fee\u6539\uff0c\u4f7f\u7528<code>add -p<\/code>\u5219\u53ef\u4ee5\u6307\u5b9a\u5176\u63d0\u4ea4\u7684\u90e8\u5206\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-diff\">@@ -1,16 +1,31 @@\n #include &lt;stdio.h&gt;\n\n+void fun1()\n+{\n+       printf(&quot;before hello world\\n&quot;);\n+}\n+\n void demo()\n {\n        ;\n }\n\n+void fun2()\n+{\n+       printf(&quot;after hello world\\n&quot;);\n+}\n+\n int main()\n {\n+       fun1();\n        printf(&quot;hello world\\n&quot;);\n+       printf(&quot;debug %s %d\\n&quot;, __func__, __LINE__);\n        printf(&quot;hello world\\n&quot;);\n        printf(&quot;hello world\\n&quot;);\n        printf(&quot;hello world\\n&quot;);\n+       printf(&quot;debug %s %d\\n&quot;, __func__, __LINE__);\n        printf(&quot;hello world\\n&quot;);\n+       fun2();\n        demo();\n+       printf(&quot;debug %s %d\\n&quot;, __func__, __LINE__);\n }<\/code><\/pre>\n<p>\u5982\u679c\u6211\u4eec\u53ea\u60f3\u63d0\u4ea4fun1\u7684\u5185\u5bb9\uff0c\u5219\u4f7f\u7528<code>-p<\/code>\u6765\u6307\u5b9a\u9700\u8981\u63d0\u4ea4\u7684\u90e8\u5206\u3002\u8f93\u5165<code>git add -p test.c<\/code>\uff0cgit\u4f1a\u5c06\u4ee3\u7801\u62c6\u5206\u4e3a\u591a\u4e2a\u7247\u6bb5\uff0c\u5bf9\u4e8e\u6bcf\u4e00\u4e2a\u7247\u6bb5\u6709\u5982\u4e0b\u547d\u4ee4\u9009\u62e9\uff08git\u4f1a\u63d0\u793a\u4f60<code>Stage this hunk [y,n,q,a,d,\/,s,e,?]?<\/code>\uff09\uff1a<\/p>\n<pre><code>y - stage this hunk\nn - do not stage this hunk\nq - quit; do not stage this hunk or any of the remaining ones\na - stage this hunk and all later hunks in the file\nd - do not stage this hunk or any of the later hunks in the file\ng - select a hunk to go to\n\/ - search for a hunk matching the given regex\nj - leave this hunk undecided, see next undecided hunk\nJ - leave this hunk undecided, see next hunk\nk - leave this hunk undecided, see previous undecided hunk\nK - leave this hunk undecided, see previous hunk\ns - split the current hunk into smaller hunks\ne - manually edit the current hunk\n? - print help\n\ny - \u6682\u5b58\u6b64\u533a\u5757\nn - \u4e0d\u6682\u5b58\u6b64\u533a\u5757\nq - \u9000\u51fa\uff1b\u4e0d\u6682\u5b58\u5305\u62ec\u6b64\u5757\u5728\u5185\u7684\u5269\u4f59\u7684\u533a\u5757\na - \u6682\u5b58\u6b64\u5757\u4e0e\u6b64\u6587\u4ef6\u540e\u9762\u6240\u6709\u7684\u533a\u5757\nd - \u4e0d\u6682\u5b58\u6b64\u5757\u4e0e\u6b64\u6587\u4ef6\u540e\u9762\u6240\u6709\u7684 \u533a\u5757\ng - \u9009\u62e9\u5e76\u8df3\u8f6c\u81f3\u4e00\u4e2a\u533a\u5757\n\/ - \u641c\u7d22\u4e0e\u7ed9\u5b9a\u6b63\u5219\u8868\u8fbe\u793a\u5339\u914d\u7684\u533a\u5757\nj - \u6682\u4e0d\u51b3\u5b9a\uff0c\u8f6c\u81f3\u4e0b\u4e00\u4e2a\u672a\u51b3\u5b9a\u7684\u533a\u5757\nJ - \u6682\u4e0d\u51b3\u5b9a\uff0c\u8f6c\u81f3\u4e00\u4e2a\u533a\u5757\nk - \u6682\u4e0d\u51b3\u5b9a\uff0c\u8f6c\u81f3\u4e0a\u4e00\u4e2a\u672a\u51b3\u5b9a\u7684\u533a\u5757\nK - \u6682\u4e0d\u51b3\u5b9a\uff0c\u8f6c\u81f3\u4e0a\u4e00\u4e2a\u533a\u5757\ns - \u5c06\u5f53\u524d\u7684\u533a\u5757\u5206\u5272\u6210\u591a\u4e2a\u8f83\u5c0f\u7684\u533a\u5757\ne - \u624b\u52a8\u7f16\u8f91\u5f53\u524d\u7684\u533a\u5757\n? - \u8f93\u51fa\u5e2e\u52a9<\/code><\/pre>\n<h2>\u56db\u3001Switch and Restore<\/h2>\n<h3>4.1 git checkout branch<\/h3>\n<p>or <code>git switch branch<\/code><\/p>\n<ol>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u7528\u4e8e\u5207\u6362\u5230\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u5206\u652f(branch)\uff1b<\/li>\n<li>\u5b83\u4f1a\u5c06\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u66f4\u65b0\u5230\u8be5\u5206\u652f\u7684\u6700\u65b0\u63d0\u4ea4\u72b6\u6001\uff1b<\/li>\n<li>\u4f8b\u5982\uff0c<code>git checkout master<\/code>\u4f1a\u5207\u6362\u5230\u4e3b\u5206\u652f(master branch)\u3002<\/li>\n<\/ol>\n<h3>4.2 git checkout -b branch<\/h3>\n<p>or <code>git switch -c branch<\/code><\/p>\n<ol>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5206\u652f\uff0c\u5e76\u7acb\u5373\u5207\u6362\u5230\u8be5\u5206\u652f\uff1b<\/li>\n<li>-b \u9009\u9879\u8868\u793a\u521b\u5efa(branch)\u4e00\u4e2a\u65b0\u5206\u652f\uff1b<\/li>\n<li>\u4f8b\u5982\uff0c<code>git checkout -b feature<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a&quot;feature&quot;\u7684\u65b0\u5206\u652f\uff0c\u5e76\u5207\u6362\u5230\u8be5\u5206\u652f\u3002<\/li>\n<\/ol>\n<h3>4.3 git checkout file.txt<\/h3>\n<p>or <code>git restore file.txt<\/code><\/p>\n<ol>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u7528\u4e8e\u64a4\u9500\u5bf9\u6587\u4ef6file.txt\u7684\u4fee\u6539,\u5c06\u5176\u6062\u590d\u5230\u6700\u8fd1\u4e00\u6b21\u63d0\u4ea4\u7684\u72b6\u6001\uff1b<\/li>\n<li>\u5b83\u4f1a\u4e22\u5f03\u5728\u5de5\u4f5c\u76ee\u5f55\u4e2d\u5bf9file.txt\u6240\u505a\u7684\u4efb\u4f55\u66f4\u6539\uff1b<\/li>\n<li>\u8fd9\u5728\u4f60\u60f3\u8981\u653e\u5f03\u5bf9\u6587\u4ef6\u7684\u4fee\u6539\u65f6\u5f88\u6709\u7528\u3002<\/li>\n<\/ol>\n<h3>4.4 git checkout -p file.txt<\/h3>\n<p>or <code>git restore -p file.txt<\/code><\/p>\n<ol>\n<li>\u8fd9\u4e2a\u547d\u4ee4\u7528\u4e8e\u4ea4\u4e92\u5f0f\u5730\u9009\u62e9\u8981\u6062\u590d\u7684file.txt\u6587\u4ef6\u7684\u90e8\u5206\u4fee\u6539\uff1b<\/li>\n<li>-p \u9009\u9879\u8868\u793a\u4ea4\u4e92\u5f0f\u5730\u8865\u4e01(patch)\u6a21\u5f0f\uff1b<\/li>\n<li>Git\u4f1a\u9010\u4e2a\u663e\u793afile.txt\u7684\u53d8\u66f4\u533a\u5757(hunk)\uff0c\u4f60\u53ef\u4ee5\u9009\u62e9\u662f\u5426\u6062\u590d\u6bcf\u4e2a\u533a\u5757\uff1b<\/li>\n<li>\u8fd9\u5728\u4f60\u53ea\u60f3\u6062\u590d\u6587\u4ef6\u7684\u90e8\u5206\u4fee\u6539\u65f6\u5f88\u6709\u7528\u3002<\/li>\n<\/ol>\n<h3>4.5 git checkout branch -- file.txt<\/h3>\n<p>or <code>git restore --source branch file.txt<\/code><\/p>\n<p>&emsp;&emsp;\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u4ece\u6307\u5b9a\u7684\u5206\u652f(branch)\u4e2d\u83b7\u53d6\u6587\u4ef6(file.txt)\u7684\u7248\u672c\uff0c\u5e76\u5c06\u5176\u590d\u5236\u5230\u5f53\u524d\u5206\u652f\u7684\u5de5\u4f5c\u76ee\u5f55\u4e2d\u3002\u5b83\u901a\u5e38\u7528\u4e8e\uff1a<\/p>\n<ol>\n<li>\u5f53\u4f60\u5728\u4e00\u4e2a\u5206\u652f\u4e0a\u5de5\u4f5c\uff0c\u4f46\u9700\u8981\u4ece\u53e6\u4e00\u4e2a\u5206\u652f\u83b7\u53d6\u7279\u5b9a\u6587\u4ef6\u7684\u66f4\u65b0\u7248\u672c\u65f6\uff1b<\/li>\n<li>\u5f53\u4f60\u8bef\u5220\u4e86\u5f53\u524d\u5206\u652f\u7684\u6587\u4ef6\uff0c\u4f46\u8be5\u6587\u4ef6\u5728\u53e6\u4e00\u4e2a\u5206\u652f\u4e0a\u4ecd\u7136\u5b58\u5728\u65f6\uff0c\u4f60\u53ef\u4ee5\u4ece\u90a3\u4e2a\u5206\u652f\u4e2d\u6062\u590d\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<h2>\u4e94\u3001Hooks &amp; Husky<\/h2>\n<ol>\n<li>Commit<\/li>\n<\/ol>\n<blockquote>\n<p>pre-commit<br \/>\nprepare-commit-msg<br \/>\ncommit-msg<br \/>\npost-commit<\/p>\n<\/blockquote>\n<ol start=\"2\">\n<li>Merging<\/li>\n<\/ol>\n<blockquote>\n<p>post-merge<br \/>\npre-merge-commit<\/p>\n<\/blockquote>\n<ol start=\"3\">\n<li>Rewriting<\/li>\n<\/ol>\n<blockquote>\n<p>pre-rebase<br \/>\npost-rewrite<\/p>\n<\/blockquote>\n<ol start=\"4\">\n<li>Switching\/Pushing<\/li>\n<\/ol>\n<blockquote>\n<p>post-checkout<br \/>\nreference-transaction<br \/>\npre-push<\/p>\n<\/blockquote>\n<h2>\u516d\u3001Attributes<\/h2>\n<h3>6.1 \u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<p>&emsp;&emsp;\u4e00\u4e2a\u5178\u578b\u7684\u6bd4\u8f83\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sh\">echo &#039;*.png diff=exif&#039; &gt;&gt; .gitattributes\n\ngit config diff.exif.textconv exiftool\n\ndiff --git a\/image.png b\/image.png\nindex 88839c4..4afcb7c 100644\n--- a\/image.png\n+++ b\/image.png\n@@ -1,12 +1,12 @@\n ExifTool Version Number         : 7.74\n-File Size                       : 70 kB\n-File Modification Date\/Time     : 2009:04:21 07:02:45-07:00\n+File Size                       : 94 kB\n+File Modification Date\/Time     : 2009:04:21 07:02:43-07:00\n File Type                       : PNG\n MIME Type                       : image\/png\n-Image Width                     : 1058\n-Image Height                    : 889\n+Image Width                     : 1056\n+Image Height                    : 827\n Bit Depth                       : 8\n Color Type                      : RGB with Alpha<\/code><\/pre>\n<h3>6.2 \u5173\u952e\u5b57\u5c55\u5f00<\/h3>\n<p>&emsp;&emsp;\u5bf9\u4e8e\u4e60\u60ef\u4f7f\u7528SVN\u6216\u8005CVS\u7684\u4f7f\u7528\u8005\u6765\u8bf4\uff0c\u5982\u679c\u8981\u826f\u597d\u7684\u4f7f\u7528keywords\uff0c\u5219\u5efa\u8bae\u4f7f\u7528<code>smudge<\/code>\u548c<code>clean<\/code>\u6765\u5b9e\u73b0\u4e00\u4e2afilter\uff1a<\/p>\n<figure style=\"width: 1278px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/05.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"1278\" height=\"615\" alt=\"\u56fe5\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/05.png\" width=\"1278\" height=\"615\" alt=\"\u56fe5\" class=\"size-full\" \/><\/noscript> \u56fe5\uff1aSumdge<\/figcaption><\/figure>\n<figure style=\"width: 1275px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\"   class=\"lazyload\" data-src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/06.png\" src=\"https:\/\/cdn.jsdelivr.net\/gh\/moezx\/cdn@3.0.2\/img\/svg\/loader\/trans.ajax-spinner-preloader.svg\" onerror=\"imgError(this)\"  width=\"1275\" height=\"612\" alt=\"\u56fe6\" class=\"size-full\" \/ ><figcaption class=\"wp-caption-text\"><noscript><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/cdn.swordofmorning.com\/SwordofMorning\/Article%20Images\/Git\/AGT_01\/06.png\" width=\"1275\" height=\"612\" alt=\"\u56fe6\" class=\"size-full\" \/><\/noscript> \u56fe6\uff1aClean<\/figcaption><\/figure>\n<pre><code class=\"language-sh\"># in .gitattributes file\n*.c filter=indent\n\n# then config\n$ git config --global filter.indent.clean indent\n$ git config --global filter.indent.smudge cat<\/code><\/pre>\n<p>&emsp;&emsp;<code>smudge<\/code>\u548c<code>clean<\/code>\u7684\u53e6\u4e00\u4e2a\u4f5c\u7528\u662f\u914d\u5408LFS\u8fdb\u884c\u5927\u6587\u4ef6\u7684\u8fc7\u6ee4\u3002\u4f8b\u5982\u6211\u6709\u4e00\u4e2a2G\u7684<code>.img<\/code>\u6587\u4ef6\uff0c\u6211\u5bf9\u5176\u914d\u7f6efilter\u6765\u4f7f\u5f97git\u4e0d\u4f1a\u8ddf\u8e2a\u8fd9\u4e2a\u6587\u4ef6\uff0c\u800c\u662f\u4fdd\u5b58\u5b83\u7684\u6307\u9488\uff0c\u6307\u5411LFS\u670d\u52a1\u5668\u3002<\/p>\n<h2>\u4e03\u3001Fixup Commit<\/h2>\n<pre><code class=\"language-sh\">git commit --fixup=&lt;commit&gt;\ngit rebase --autosquash<\/code><\/pre>\n<p>&emsp;&emsp;\u5047\u8bbe\u6211\u4eec\u73b0\u5728\u6709\u5982\u4e0b\u7684\u63d0\u4ea4\u8bb0\u5f55\uff1a<\/p>\n<pre><code class=\"language-sh\">c1: Add feature A\nc2: Fix typo in feature A\nc3: Add feature B\nc4: Refactor feature A<\/code><\/pre>\n<p>\u4f60\u53d1\u73b0\u5728\u63d0\u4ea4<code>c2<\/code>\u4e2d\u6709\u4e00\u4e2a\u5c0f\u9519\u8bef\u9700\u8981\u4fee\u590d\u3002\u4f60\u8fdb\u884c\u4e86\u5fc5\u8981\u7684\u66f4\u6539\uff0c\u7136\u540e\u4f7f\u7528 <code>git commit --fixup<\/code>\u63d0\u4ea4\u8fd9\u4e2a\u4fee\u590d:<\/p>\n<pre><code class=\"language-sh\">git add .\ngit commit --fixup=c2<\/code><\/pre>\n<p>\u73b0\u5728\u4f60\u7684\u63d0\u4ea4\u5386\u53f2\u770b\u8d77\u6765\u50cf\u8fd9\u6837:<\/p>\n<pre><code class=\"language-sh\">c1: Add feature A\nc2: Fix typo in feature A\nc3: Add feature B\nc4: Refactor feature A\nc5: fixup! Fix typo in feature A<\/code><\/pre>\n<p>\u73b0\u5728\u8fd0\u884c<code>git rebase -i --autosquash HEAD~5<\/code>\u547d\u4ee4\u3002Git\u4f1a\u6253\u5f00\u4e00\u4e2a\u4ea4\u4e92\u5f0frebase\u7f16\u8f91\u5668\uff0c\u5b83\u5df2\u7ecf\u5c06<code>fixup<\/code>\u63d0\u4ea4(c5)\u79fb\u52a8\u5230\u4e86\u5b83\u6240\u5f15\u7528\u7684\u63d0\u4ea4(c2)\u4e4b\u540e\uff0c\u5e76\u5c06\u5176\u6807\u8bb0\u4e3a&quot;fixup&quot;:<\/p>\n<pre><code class=\"language-sh\">pick c1 Add feature A\npick c2 Fix typo in feature A\nfixup c5 fixup! Fix typo in feature A\npick c3 Add feature B\npick c4 Refactor feature A<\/code><\/pre>\n<p>\u4f60\u4fdd\u5b58\u5e76\u5173\u95ed\u7f16\u8f91\u5668\u3002Git\u4f1a\u81ea\u52a8\u5c06<code>fixup<\/code>\u63d0\u4ea4(c5)\u5408\u5e76\u5230\u5b83\u6240\u5f15\u7528\u7684\u63d0\u4ea4(c2)\u4e2d\uff0c\u5e76\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u63d0\u4ea4\u5386\u53f2:<\/p>\n<pre><code class=\"language-sh\">c1: Add feature A\nc2: Fix typo in feature A\nc3: Add feature B\nc4: Refactor feature A<\/code><\/pre>\n<p>&emsp;&emsp;\u8fd9\u6837\uff0c<code>fixup<\/code>\u63d0\u4ea4\u5c31\u88ab\u65e0\u7f1d\u5730\u5408\u5e76\u5230\u4e86\u5b83\u6240\u4fee\u590d\u7684\u63d0\u4ea4\u4e2d\uff0c\u4f7f\u63d0\u4ea4\u5386\u53f2\u4fdd\u6301\u7b80\u6d01\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5de5\u4f5c\u6d41\u7a0b &emsp;&emsp;\u4e3a\u4e86\u4e86\u89e3git\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u77e5\u9053\u4ee3\u7801\u5728\u4ec0\u4e48\u5730\u65b9\u3002\u5bf9\u4e8e\u4f7f\u7528git\u7ba1\u7406\u7684\u9879\u76ee\uff0c\u4e3b\u8981\u4f1a &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1478"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=1478"}],"version-history":[{"count":3,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1478\/revisions"}],"predecessor-version":[{"id":1481,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/1478\/revisions\/1481"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=1478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=1478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=1478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}