{"id":946,"date":"2023-06-12T10:01:38","date_gmt":"2023-06-12T02:01:38","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=946"},"modified":"2025-10-09T13:55:19","modified_gmt":"2025-10-09T05:55:19","slug":"git-01","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2023\/06\/12\/git-01\/","title":{"rendered":"Git 01 Basic Tutorial"},"content":{"rendered":"<p>&emsp;&emsp;\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u4ecegithub\u4e0a\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u5e76\u4f7f\u7528clone-&gt;push\u6216\u8005push remote\u7684\u65b9\u5f0f\u6765\u63a8\u9001\u5b83\u3002\u5e76\u4e14\u6211\u4eec\u5df2\u7ecf\u4e86\u89e3\u4e86\u4ee5\u4e0b\u57fa\u672c\u8bed\u6cd5\uff1a<\/p>\n<ol>\n<li>git add<\/li>\n<li>git commit<\/li>\n<li>git push<\/li>\n<\/ol>\n<h2>\u4e00\u3001Branching<\/h2>\n<h3>1.1 Create Branch<\/h3>\n<p>&emsp;&emsp;\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a\u4ed3\u5e93\uff0c\u5176\u6709\u4e2areadme\u6587\u4ef6\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>git branch<\/code>\u6765\u67e5\u770b\u5206\u652f\u3002\u6b64\u65f6\u5e94\u8be5\u53ea\u6709\u4e00\u4e2amaster\u5206\u652f\u3002<\/p>\n<p>&emsp;&emsp;\u6211\u4eec\u4f7f\u7528<code>git checkout -b branch_test_01<\/code>\u5728<strong>\u672c\u5730<\/strong>\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<em>branch_test_01<\/em>\u7684\u5206\u652f\uff0c\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u53d1\u73b0\u5df2\u7ecf\u81ea\u52a8\u5207\u6362\u5230\u4e86<em>branch_test_01<\/em>\u5206\u652f\uff1a<\/p>\n<pre><code class=\"language-shell\">xjt@u16:~\/DCIR_TEST_01$ git branch\n* branch_test_01\n  master<\/code><\/pre>\n<p>\u4f7f\u7528<code>git checkout master<\/code>\u5219\u5141\u8bb8\u6211\u4eec\u5207\u6362\u56de<em>master<\/em>\u5206\u652f\u3002<\/p>\n<p>\u5f53\u6211\u4eec\u5728<em>branch_test_01<\/em>\u4e0b\u4fee\u6539\u4e86\u4e00\u4e9b\u5185\u5bb9\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>git push --set-upstream origin branch_test_01<\/code>\u6765\u5728\u539f\u521b\u4ed3\u5e93\u4e0a\u521b\u5efa\u4e00\u4e2a<strong>\u8fdc\u7a0b<\/strong>\u540c\u540d\u7684\u5206\u652f\uff08<code>--set-upstream<\/code> == <code>-u<\/code>\uff09\u3002<\/p>\n<h3>1.2 PR Conflict<\/h3>\n<p>&emsp;&emsp;\u5bf9\u4e8e\u4e00\u822c\u60c5\u51b5\uff0c\u5408\u5e76\u4e0d\u540c\u7684\u5206\u652f\uff1a<\/p>\n<pre><code>master_v1 -&gt; master_v1\n        |\n        ---&gt; branch_01_v1<\/code><\/pre>\n<p>\u6b64\u65f6\u60f3\u8981\u5408\u5e76\u7684\u4e24\u4e2a\u5206\u652f<em>master_v1<\/em>\u548c<em>branch_01_v1<\/em>\u76f8\u6bd4\uff0c\u53ea\u6709<strong>\u65b0\u589e<\/strong>\uff0c\u76f4\u63a5\u5408\u5e76\u5373\u53ef\u3002<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u662f\u8fd9\u79cd\u60c5\u51b5\uff1a<\/p>\n<pre><code>master_v1 -&gt; master_v2\n        |\n        ---&gt; branch_01_v1<\/code><\/pre>\n<p>\u6b64\u65f6\u8981\u5408\u5e76\u7684<em>master_v2<\/em>\u548c<em>branch_01_v1<\/em>\u76f8\u6bd4\uff0c\u5bf9\u540c\u4e00\u6587\u4ef6\u9664\u4e86<strong>\u65b0\u589e<\/strong>\u4e4b\u5916\u8fd8\u53ef\u80fd\u5b58\u5728<strong>\u5220\u9664<\/strong>\uff0c\u5bf9\u540c\u4e00\u6bb5\u4ee3\u7801\u53ef\u80fd\u6709\u5220\u9664\u3001\u8986\u5199\u7b49\u64cd\u4f5c\uff0c\u5219\u9700\u8981\u624b\u52a8\u5730\u5408\u5e76\u3002\u8fd9\u65f6\uff0c\u5982\u679c\u662f\u8fdc\u7a0b\u4ed3\u5e93\u5df2\u7ecf\u6539\u52a8\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6240\u6709\u5185\u5bb9pull\u5230\u672c\u5730\u3002\u5982\u679c\u662f\u672c\u5730\u4ed3\u5e93\u7684\u5185\u5bb9\u6539\u52a8\uff0c\u6211\u4eec\u5219\u9700\u8981commit\u3002<\/p>\n<p>&emsp;&emsp;\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u7684<em>master_v1<\/em>\u3001<em>master_v2<\/em>\u548c<em>branch_01_v1<\/em>\u5206\u652f\u7684md\u6587\u4ef6\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-md\">&lt;!-- master_v1 --&gt;\nHello\n\n&lt;!-- master_v2 --&gt;\nHello\n1\n\n&lt;!-- branch_01_v1 --&gt;\nHello\n2<\/code><\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u5207\u6362\u5230<em>master_v2<\/em>\u5206\u652f\uff0c\u6211\u4eec\u4f7f\u7528<code>git merge branch_01_v1<\/code>\u5c06<em>branch_01_v1<\/em>\u7684\u5185\u5bb9\u5408\u5e76\u5230<em>master_v2<\/em>\u4e2d\uff0c\u6b64\u65f6\u6211\u4eec\u67e5\u770breadme\u6587\u4ef6\uff1a<\/p>\n<pre><code>Hello\n&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD\n2\n>&gt;&gt;&gt;&gt;&gt;&gt; master\n1<\/code><\/pre>\n<p>\u73b0\u5728\u6211\u4eec\u53ef\u4ee5\u5728\u624b\u52a8\u7684\u4fee\u6539\u5b83\uff0c\u7136\u540e\u518d\u63d0\u4ea4\u3002\u4f7f\u7528vscode\u6216\u8005\u5176\u4ed6\u542b\u6709git\u63d2\u4ef6\u7684\u7f16\u8f91\u5668\uff0c\u80fd\u5f88\u5bb9\u6613\u7684\u5bf9\u6bd4\u3002<\/p>\n<h3>1.3 Del Branch<\/h3>\n<ol>\n<li><code>git branch -d local_branch_name<\/code>\uff1a\u5220\u9664\u672c\u5730\u5206\u652f\u3002<\/li>\n<li><code>git push remote_name --delete remote_branch_name<\/code>\uff1a\u8fdc\u5904\u8fdc\u7a0b\u5206\u652f\u3002<\/li>\n<li><code>git branch -a<\/code>\uff1a\u67e5\u770b\u6240\u6709\u5206\u652f\uff08\u672c\u5730\u548c\u8fdc\u7a0b\uff09\uff0c<code>-t<\/code>\u67e5\u770b\u8fdc\u7a0b\u5206\u652f\u3002<\/li>\n<\/ol>\n<h3>1.4 Fetch Branch<\/h3>\n<p><code>git fetch origin remote_branch_name:local_branch_name<\/code><\/p>\n<h2>\u4e8c\u3001Undoing<\/h2>\n<h3>2.1 Undo File<\/h3>\n<p>&emsp;&emsp;\u5982\u679c\u662f\u4fee\u6539\u4e86\u4e00\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>git reset filename<\/code>\u6765\u6062\u590d\u5230\u4e0a\u6b21\u540c\u6b65\u7684\u72b6\u6001\uff0c\u6216\u662f\u4f7f\u7528<code>git reset<\/code>\u6765\u6062\u590d\u6240\u6709\u6587\u4ef6\u5230\u4e0a\u6b21\u540c\u6b65\u7684\u72b6\u6001\u3002<\/p>\n<h3>2.1 Undo Commit<\/h3>\n<p>&emsp;&emsp;\u5982\u679c\u60f3\u8981\u64a4\u9500\u4e0a\u4e00\u6b21commit\uff0c\u4f7f\u7528<code>git reset HEAD~1<\/code>\uff0cHead\u8868\u793a\u6700\u8fd1\u7684commit\u3002<\/p>\n<p>&emsp;&emsp;\u5982\u679c\u60f3\u64a4\u56de\u5230\u6307\u5b9a\u7684commit\uff0c<code>git reset commit_log_number<\/code>\u3002\u901a\u8fc7<code>git log<\/code>\u67e5\u770b\u65e5\u5fd7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&emsp;&emsp;\u73b0\u5728\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u77e5\u9053\u4e86\u5982\u4f55\u4ecegithub\u4e0a\u521b\u5efa\u4e00\u4e2a\u9879\u76ee\uff0c\u5e76\u4f7f\u7528clone-&gt;push\u6216\u8005push r &#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\/946"}],"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=946"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/946\/revisions"}],"predecessor-version":[{"id":947,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/946\/revisions\/947"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=946"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=946"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=946"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}