{"id":58954,"date":"2023-10-26T22:50:57","date_gmt":"2023-10-26T14:50:57","guid":{"rendered":"https:\/\/docs.pingcode.com\/?p=58954"},"modified":"2023-10-28T15:33:21","modified_gmt":"2023-10-28T07:33:21","slug":"git-%e6%80%8e%e4%b9%88%e6%81%a2%e5%a4%8d%e6%9c%ac%e5%9c%b0%e5%88%a0%e9%99%a4%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/58954.html","title":{"rendered":"git \u600e\u4e48\u6062\u590d\u672c\u5730\u5220\u9664\u6587\u4ef6"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\">\n<p>\u200b\u4f7f\u7528 git \u65f6\uff0c\u6211\u4eec\u6709\u65f6\u4e0d\u5c0f\u5fc3\u5220\u9664\u4e86\u91cd\u8981\u7684\u672c\u5730\u6587\u4ef6\u3002\u5982\u4f55\u6062\u590d\u5b83\u4eec\u5462\uff1f\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a1. \u4f7f\u7528 <strong>git log<\/strong> \u67e5\u627e\u5220\u9664\u7684\u63d0\u4ea4\uff1b2. \u5229\u7528 <strong>git checkout<\/strong> \u6216 <strong>git restore<\/strong> \u6062\u590d\u6587\u4ef6\uff1b3. \u4f7f\u7528 <strong>git reflog<\/strong> \u5f53\u5176\u4ed6\u65b9\u6cd5\u5931\u8d25\uff1b4. \u5bf9\u5df2\u63d0\u4ea4\u4f46\u672a\u63a8\u9001\u7684\u66f4\u6539\u8fdb\u884c\u6062\u590d\uff1b5. \u5bf9\u4e8e\u672a\u63d0\u4ea4\u7684\u5220\u9664\u64cd\u4f5c\u8fdb\u884c\u6062\u590d\u3002\u6062\u590d\u6587\u4ef6\u7684\u7b2c\u4e00\u6b65\u662f\u786e\u5b9a\u6211\u4eec\u4f55\u65f6\u5220\u9664\u4e86\u5b83\u3002<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1.\u4f7f\u7528 git log \u67e5\u627e\u5220\u9664\u7684\u63d0\u4ea4<\/h2>\n\n\n\n<p>\u5f53\u6587\u4ef6\u88ab\u5220\u9664\u5e76\u63d0\u4ea4\u540e\uff0c\u6211\u4eec\u9700\u8981\u627e\u5230\u5bf9\u5e94\u7684\u63d0\u4ea4\u8bb0\u5f55\u3002\u901a\u8fc7\u8fd0\u884c <strong>git log &#8211;diff-filter=D &#8211;summary<\/strong>\uff0c\u53ef\u4ee5\u5217\u51fa\u6240\u6709\u5220\u9664\u6587\u4ef6\u7684\u63d0\u4ea4\u8bb0\u5f55\u3002\u4ece\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u627e\u5230\u5220\u9664\u6587\u4ef6\u7684\u63d0\u4ea4\u54c8\u5e0c\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2\uff0e\u5229\u7528 git checkout \u6216 git restore \u6062\u590d\u6587\u4ef6<\/h2>\n\n\n\n<p>\u5bf9\u4e8e\u65e9\u671f\u7248\u672c\u7684 git\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <strong>git checkout [commit_hash] &#8212; [file_path]<\/strong> \u6765\u6062\u590d\u6587\u4ef6\u3002\u5bf9\u4e8e\u8f83\u65b0\u7248\u672c\uff0c\u63a8\u8350\u4f7f\u7528 <strong>git restore &#8211;source=[commit_hash] [file_path]<\/strong> \u6765\u5b9e\u73b0\u3002\u5176\u4e2d <strong>[commit_hash]<\/strong> \u662f\u5220\u9664\u6587\u4ef6\u7684\u63d0\u4ea4\u54c8\u5e0c\u503c\u7684\u524d\u51e0\u4f4d\uff0c<strong>[file_path]<\/strong> \u662f\u88ab\u5220\u9664\u6587\u4ef6\u7684\u5b8c\u6574\u8def\u5f84\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3.\u4f7f\u7528 git reflog \u5f53\u5176\u4ed6\u65b9\u6cd5\u5931\u8d25<\/h2>\n\n\n\n<p>\u5982\u679c\u4e0a\u8ff0\u65b9\u6cd5\u4e0d\u8d77\u4f5c\u7528\uff0c\u6216\u8005\u6211\u4eec\u5fd8\u8bb0\u4e86\u5220\u9664\u6587\u4ef6\u7684\u63d0\u4ea4\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>git reflog<\/strong>\u3002\u5b83\u663e\u793a\u4e86 HEAD \u7684\u5386\u53f2\u8bb0\u5f55\u3002\u627e\u5230\u8981\u6062\u590d\u7684\u6587\u4ef6\u5bf9\u5e94\u7684\u6761\u76ee\uff0c\u7136\u540e\u6309\u7167\u4e0a\u8ff0\u65b9\u6cd5\u4f7f\u7528\u76f8\u5e94\u7684\u54c8\u5e0c\u503c\u8fdb\u884c\u6062\u590d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4.\u5bf9\u5df2\u63d0\u4ea4\u4f46\u672a\u63a8\u9001\u7684\u66f4\u6539\u8fdb\u884c\u6062\u590d<\/h2>\n\n\n\n<p>\u5982\u679c\u5220\u9664\u4e86\u6587\u4ef6\u3001\u63d0\u4ea4\u4e86\u66f4\u6539\uff0c\u4f46\u5c1a\u672a\u63a8\u9001\uff0c\u53ef\u4ee5\u4f7f\u7528 <strong>git reset HEAD^<\/strong>\u3002\u8fd9\u5c06\u64a4\u9500\u4e0a\u4e00\u6b21\u7684\u63d0\u4ea4\uff0c\u5e76\u5c06\u66f4\u6539\u653e\u56de\u6682\u5b58\u533a\u3002\u7136\u540e\u53ef\u4ee5\u4f7f\u7528 <strong>git checkout [file_path]<\/strong> \u6216 <strong>git restore [file_path]<\/strong> \u6062\u590d\u6587\u4ef6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5.\u5bf9\u4e8e\u672a\u63d0\u4ea4\u7684\u5220\u9664\u64cd\u4f5c\u8fdb\u884c\u6062\u590d<\/h2>\n\n\n\n<p>\u5982\u679c\u4ec5\u4ec5\u5728\u5de5\u4f5c\u76ee\u5f55\u4e2d\u5220\u9664\u4e86\u6587\u4ef6\uff0c\u800c\u6ca1\u6709\u8fdb\u884c\u4efb\u4f55\u63d0\u4ea4\u64cd\u4f5c\uff0c\u7b80\u5355\u5730\u4f7f\u7528 <strong>git checkout [file_path]<\/strong> \u6216 <strong>git restore [file_path]<\/strong> \u5373\u53ef\u6062\u590d\u3002<\/p>\n\n\n\n<p>\u5728 git \u4e2d\uff0c\u51e0\u4e4e\u6240\u6709\u7684\u64cd\u4f5c\u90fd\u662f\u53ef\u9006\u7684\u3002\u91cd\u8981\u7684\u662f\uff0c\u5f53\u610f\u8bc6\u5230\u9519\u8bef\u65f6\uff0c\u4e0d\u8981\u8fdb\u4e00\u6b65\u63d0\u4ea4\u6216\u64cd\u4f5c\uff0c\u4ee5\u514d\u8986\u76d6\u5386\u53f2\u8bb0\u5f55\u3002\u719f\u7ec3\u638c\u63e1\u4e0a\u8ff0\u547d\u4ee4\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6709\u6548\u5730\u5e94\u5bf9\u7a81\u53d1\u72b6\u51b5\uff0c\u6062\u590d\u91cd\u8981\u7684\u6587\u4ef6\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u5728\u65e5\u5e38\u4f7f\u7528 git \u65f6\uff0c\u5efa\u8bae\u7ecf\u5e38\u63d0\u4ea4\u66f4\u6539\u5e76\u4fdd\u6301\u826f\u597d\u7684\u63d0\u4ea4\u4e60\u60ef\uff0c\u8fd9\u6837\u5373\u4f7f\u53d1\u751f\u9519\u8bef\uff0c\u4e5f\u53ef\u4ee5\u8f7b\u677e\u6062\u590d\u3002\u6b64\u5916\uff0c\u5b9a\u671f\u5907\u4efd\u4ed3\u5e93\u548c\u4e0e\u56e2\u961f\u6210\u5458\u4fdd\u6301\u6c9f\u901a\uff0c\u4e5f\u662f\u907f\u514d\u91cd\u5927\u95ee\u9898\u7684\u5173\u952e\u3002<\/p>\n\n\n\n<p>\u603b\u4e4b\uff0cgit \u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u6765\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u548c\u6062\u590d\u6587\u4ef6\u3002\u53ea\u9700\u6df1\u5165\u5b66\u4e60\u548c\u5b9e\u8df5\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u9a7e\u9a6d\u8fd9\u4e00\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u786e\u4fdd\u6211\u4eec\u7684\u4ee3\u7801\u5b89\u5168\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img fetchpriority=\"high\" decoding=\"async\" width=\"484\" height=\"300\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2023\/10\/\u5982\u4f55\u5728\u9e92\u9e9f\u64cd\u4f5c\u7cfb\u7edf\u4e0a\u5b89\u88c5\u548c\u7ba1\u7406\u5b57\u4f53-46.png?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\" class=\"wp-image-58955\" \/><\/figure><\/div>\n\n\n<p><strong>\u5e38\u89c1\u95ee\u7b54\uff1a<\/strong><\/p>\n\n\n\n<ul>\n<li><strong>\u95ee\uff1a<\/strong> \u5982\u679c\u6211\u4e0d\u786e\u5b9a\u6587\u4ef6\u5728\u54ea\u6b21\u63d0\u4ea4\u88ab\u5220\u9664\u7684\uff0c\u6211\u600e\u4e48\u627e\u5230\u5b83\uff1f<\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong> \u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>git log &#8211;full-history &#8212; [file path]<\/strong> \u547d\u4ee4\u6765\u67e5\u770b\u6307\u5b9a\u6587\u4ef6\u7684\u4fee\u6539\u5386\u53f2\u3002\u8fd9\u5c06\u5217\u51fa\u6240\u6709\u66f4\u6539\u4e86\u8be5\u6587\u4ef6\u7684\u63d0\u4ea4\uff0c\u5305\u62ec\u5220\u9664\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>\u95ee\uff1a<\/strong> \u5728\u4f7f\u7528 <strong>git restore<\/strong> \u6062\u590d\u6587\u4ef6\u65f6\uff0c\u662f\u5426\u8fd8\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u7279\u5b9a\u7684\u63d0\u4ea4\uff1f<\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong> \u662f\u7684\uff0c\u4f60\u53ef\u4ee5\u6307\u5b9a\u4e00\u4e2a\u7279\u5b9a\u7684\u63d0\u4ea4\u6765\u6062\u590d\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u4f7f\u7528 <strong>git restore &#8211;source=[commit hash] &#8212; [file path]<\/strong> \u53ef\u4ee5\u4ece\u6307\u5b9a\u7684\u63d0\u4ea4\u4e2d\u6062\u590d\u6587\u4ef6\u3002<\/li>\n<\/ul>\n\n\n\n<ul>\n<li><strong>\u95ee\uff1a<\/strong> \u5982\u679c\u6211\u6062\u590d\u4e86\u4e00\u4e2a\u9519\u8bef\u7248\u672c\u7684\u6587\u4ef6\uff0c\u6211\u53ef\u4ee5\u518d\u6b21\u56de\u9000\u5417\uff1f<\/li>\n\n\n\n<li><strong>\u7b54\uff1a<\/strong> \u662f\u7684\uff0c\u53ea\u8981\u4f60\u8fd8\u6ca1\u6709\u63d0\u4ea4\u66f4\u6539\uff0c\u4f60\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528 <strong>git restore<\/strong> \u547d\u4ee4\u6765\u6062\u590d\u5230\u4f60\u60f3\u8981\u7684\u7248\u672c\u3002\u5982\u679c\u4f60\u5df2\u7ecf\u63d0\u4ea4\u4e86\u66f4\u6539\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <strong>git reset<\/strong> \u6216\u8005\u5176\u4ed6git\u547d\u4ee4\u6765\u56de\u9000\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"\u200b\u4f7f\u7528 git \u65f6\uff0c\u6211\u4eec\u6709\u65f6\u4e0d\u5c0f\u5fc3\u5220\u9664\u4e86\u91cd\u8981\u7684\u672c\u5730\u6587\u4ef6\u3002\u5982\u4f55\u6062\u590d\u5b83\u4eec\u5462\uff1f\u672c\u6587\u4e3b\u8981\u4ecb\u7ecd\u4e86\u4ee5\u4e0b\u51e0\u4e2a\u6b65\u9aa4\uff1a1. \u4f7f\u7528 [&hellip;]","protected":false},"author":7,"featured_media":58955,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/58954"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=58954"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/58954\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/58955"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=58954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=58954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=58954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}