{"id":19135,"date":"2024-11-19T13:29:11","date_gmt":"2024-11-19T05:29:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/19135\/"},"modified":"2024-11-19T13:29:11","modified_gmt":"2024-11-19T05:29:11","slug":"%e5%b8%ae%e4%bd%a0%e5%88%a0%e9%99%a4-git-%e4%b8%ad%e7%9a%84%e5%8e%86%e5%8f%b2%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/19135\/","title":{"rendered":"\u5e2e\u4f60\u5220\u9664 Git \u4e2d\u7684\u5386\u53f2\u8bb0\u5f55"},"content":{"rendered":"<p>\u5bf9\u4e8e\u4f7f\u7528 git \u8fdb\u884c\u9879\u76ee\u7ba1\u7406\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u7684\u60c5\u51b5\u3002\u6bd4\u5982\u63d0\u4ea4\u4e86\u654f\u611f\u4fe1\u606f\u6216\u8005\u6cd5\u5f8b\u8981\u6c42\u5220\u9664\u67d0\u4e9b\u5185\u5bb9\u3002\u4f46\u662f\uff0cgit \u7684\u8bbe\u8ba1\u521d\u8877\u662f\u4fdd\u7559\u6240\u6709\u4fee\u6539\u8bb0\u5f55\uff0c\u56e0\u6b64\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u5e76\u4e0d\u662f\u4e00\u4ef6\u5bb9\u6613\u7684\u4e8b\u60c5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5220\u9664 git \u4e2d\u7684\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528 Git \u547d\u4ee4<\/li>\n<\/ol>\n<p>\u53ef\u4ee5\u4f7f\u7528 Git \u547d\u4ee4\u4e2d\u7684 git reset \u548c git push \u5220\u9664\u5386\u53f2\u8bb0\u5f55\u3002\u9996\u5148\u4f7f\u7528 git log \u547d\u4ee4\u67e5\u770b\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<pre>$ git log --oneline<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u5217\u51fa\u5f53\u524d\u5206\u652f\u4e0a\u7684\u6240\u6709\u63d0\u4ea4\u8bb0\u5f55\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u90fd\u6709\u4e00\u4e2a SHA-1 \u6807\u8bc6\u7b26\uff08\u7b2c\u4e00\u5217\uff09\u548c\u4e00\u4e2a\u6d88\u606f\uff08\u540e\u9762\u7684\u5217\uff09\u3002<\/p>\n<p>\u7136\u540e\uff0c\u4f7f\u7528 git reset \u547d\u4ee4\u5c06\u5f53\u524d\u5206\u652f\u91cd\u7f6e\u5230\u8981\u5220\u9664\u7684\u63d0\u4ea4\u4e4b\u524d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5220\u9664\u6700\u8fd1\u7684\u4e09\u4e2a\u63d0\u4ea4\u8bb0\u5f55\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\uff1a<\/p>\n<pre>$ git reset HEAD~3<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u5c31\u5c06\u5f53\u524d\u5206\u652f\u91cd\u7f6e\u5230\u4e86\u524d\u4e09\u4e2a\u63d0\u4ea4\u8bb0\u5f55\u4e4b\u5904\uff0c\u5386\u53f2\u8bb0\u5f55\u4e2d\u7684\u6700\u540e\u4e09\u4e2a\u63d0\u4ea4\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<p>\u6700\u540e\uff0c\u4f7f\u7528 git push -f \u547d\u4ee4\u5f3a\u5236\u63a8\u9001\u5230\u8fdc\u7a0b\u4ed3\u5e93\uff0c\u8986\u76d6\u5386\u53f2\u8bb0\u5f55\u3002\u8fd9\u4f1a\u4f7f\u5176\u4ed6\u4eba\u5728\u62c9\u53d6\u66f4\u65b0\u65f6\u9047\u5230\u95ee\u9898\uff0c\u56e0\u6b64\u9700\u8981\u786e\u4fdd\u6240\u6709\u6709\u6743\u9650\u7684\u4eba\u77e5\u9053\u6b64\u64cd\u4f5c\u3002<\/p>\n<pre>$ git push -f<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528\u6b64\u65b9\u6cd5\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u65f6\uff0c\u6211\u4eec\u5efa\u8bae\u5728\u6267\u884c\u4e4b\u524d\u5907\u4efd\u4ee3\u7801\u5e93\uff0c\u4ee5\u9632\u4e07\u4e00\u3002<\/p>\n<ol>\n<li>\u4f7f\u7528 BFG Repo-Cleaner<\/li>\n<\/ol>\n<p>\u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u662f BFG Repo-Cleaner\u3002\u5b83\u662f\u4e00\u4e2a Java \u7a0b\u5e8f\uff0c\u53ef\u4ee5\u5728\u672c\u5730\u8fd0\u884c\uff0c\u5220\u9664 Git \u4ed3\u5e93\u4e2d\u7684\u5386\u53f2\u8bb0\u5f55\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5b89\u88c5 BFG Repo-Cleaner\u3002\u53ef\u4ee5\u4f7f\u7528 Homebrew \u5728 Mac \u4e0a\u5b89\u88c5\uff1a<\/p>\n<pre>$ brew install bfg<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6216\u8005\u4ece\u5b98\u7f51\u4e0b\u8f7d\u5e76\u5b89\u88c5\u5230\u8ba1\u7b97\u673a\u4e0a\uff1a<\/p>\n<pre>$ wget http:\/\/search.maven.org\/remotecontent?filepath=com\/madgag\/bfg\/1.14.0\/bfg-1.14.0.jar<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u5728 Git \u4ed3\u5e93\u7684\u6839\u76ee\u5f55\u4e0b\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff08\u8bf7\u786e\u4fdd\u5728\u5220\u9664\u524d\u5907\u4efd\u4ee3\u7801\u5e93\uff09\uff1a<\/p>\n<pre>$ java -jar \/path\/to\/bfg.jar --delete-files filename.txt.git\n$ git reflog expire --expire=now --all &amp;&amp; git gc --prune=now --aggressive\n$ git push<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cfilename.txt.git \u662f\u5305\u542b\u8981\u5220\u9664\u5185\u5bb9\u7684\u6587\u4ef6\u540d\uff08\u4f8b\u5982 .git-credentials\uff09\u3002\u8fd9\u91cc\u4f7f\u7528 BFG Repo-Cleaner \u5220\u9664\u63d0\u4ea4\u5386\u53f2\u3001refs \u548c tags\uff0c\u7136\u540e\u91cd\u65b0\u538b\u7f29\u5b58\u50a8\u5e93\u3002<\/p>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0cBFG Repo-Cleaner \u53ef\u4ee5\u5220\u9664\u4ed3\u5e93\u4e2d\u7684\u4efb\u4f55\u5185\u5bb9\uff0c\u56e0\u6b64\u9700\u8981\u5c0f\u5fc3\u64cd\u4f5c\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Git \u4ed3\u5e93<\/li>\n<\/ol>\n<p>\u6700\u540e\u4e00\u79cd\u65b9\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Git \u4ed3\u5e93\uff0c\u53ea\u5305\u542b\u8981\u4fdd\u7559\u7684\u63d0\u4ea4\u8bb0\u5f55\u3002\u8fd9\u79cd\u65b9\u6cd5\u9700\u8981\u4e00\u4e9b\u624b\u52a8\u64cd\u4f5c\uff0c\u4f46\u53ef\u4ee5\u4fdd\u7559\u4ed3\u5e93\u7684\u5b8c\u6574\u6027\uff0c\u5e76\u4e14\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u4eba\u5728\u4ed3\u5e93\u4e0a\u7684\u5de5\u4f5c\u3002<\/p>\n<p>\u9996\u5148\uff0c\u5728\u539f\u59cb Git \u4ed3\u5e93\u7684\u6839\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c\u7528\u4e8e\u4fdd\u5b58\u65b0\u7684 Git \u4ed3\u5e93\uff1a<\/p>\n<pre>$ mkdir newrepo &amp;&amp; cd newrepo<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\uff0c\u5c06\u539f\u59cb Git \u4ed3\u5e93\u7684\u9009\u62e9\u6027\u63d0\u4ea4\u8bb0\u5f55\u590d\u5236\u5230\u65b0\u76ee\u5f55\u4e2d\uff1a<\/p>\n<pre>$ git clone \/path\/to\/oldrepo --depth 1\n$ cd oldrepo &amp;&amp; git log --oneline<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u539f\u59cb Git \u4ed3\u5e93\u7684\u6700\u540e\u4e00\u4e2a\u63d0\u4ea4\u8bb0\u5f55\u590d\u5236\u5230\u65b0\u7684 Git \u4ed3\u5e93\u4e2d\u3002\u66f4\u65b0\u65b0\u4ed3\u5e93\u7684\u8fdc\u7a0b URL\uff0c\u7136\u540e\u5c06\u5176\u63a8\u9001\u5230\u8fdc\u7a0b\u4ed3\u5e93\u3002<\/p>\n<pre>$ git remote set-url origin git:\/\/new\/repo.git\n$ git push -u origin master<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u65b0\u7684 Git \u4ed3\u5e93\uff0c\u53ea\u5305\u542b\u9009\u62e9\u6027\u63d0\u4ea4\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u603b\u7ed3<\/p>\n<p>\u5728 Git \u4e2d\uff0c\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u662f\u4e00\u4e2a\u654f\u611f\u7684\u95ee\u9898\uff0c\u9700\u8981\u5c0f\u5fc3\u8c28\u614e\u64cd\u4f5c\u3002\u4f7f\u7528 git reset \u547d\u4ee4\u53ef\u4ee5\u5220\u9664\u6700\u8fd1\u7684\u4e00\u4e9b\u63d0\u4ea4\u8bb0\u5f55\u3002\u4f7f\u7528 BFG Repo-Cleaner \u53ef\u4ee5\u5220\u9664\u7279\u5b9a\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939\u7684\u63d0\u4ea4\u5386\u53f2\u3002\u6700\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Git \u4ed3\u5e93\u6765\u9009\u62e9\u6027\u5730\u4fdd\u7559\u63d0\u4ea4\u8bb0\u5f55\u3002\u4e0d\u7ba1\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u5728\u5220\u9664\u4e4b\u524d\u5907\u4efd\u4ee3\u7801\u5e93\uff0c\u4ee5\u9632\u4e07\u4e00\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5e2e\u4f60\u5220\u9664 Git \u4e2d\u7684\u5386\u53f2\u8bb0\u5f55\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5bf9\u4e8e\u4f7f\u7528 git \u8fdb\u884c\u9879\u76ee\u7ba1\u7406\u7684\u5f00\u53d1\u8005\u6765\u8bf4\uff0c\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u7684\u60c5\u51b5\u3002\u6bd4\u5982\u63d0\u4ea4\u4e86\u654f\u611f\u4fe1\u606f\u6216\u8005\u6cd5\u5f8b\u8981\u6c42\u5220\u9664\u67d0\u4e9b\u5185\u5bb9\u3002\u4f46\u662f\uff0cgit \u7684\u8bbe\u8ba1\u521d\u8877\u662f\u4fdd\u7559\u6240\u6709\u4fee\u6539\u8bb0\u5f55\uff0c\u56e0\u6b64\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u5e76\u4e0d\u662f\u4e00\u4ef6\u5bb9\u6613\u7684\u4e8b\u60c5\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u4e00\u4e9b\u65b9\u6cd5\uff0c\u5e2e\u52a9\u4f60\u5220\u9664 git \u4e2d\u7684\u5386\u53f2\u8bb0\u5f55\u3002 \u4f7f\u7528 Git \u547d\u4ee4 \u53ef\u4ee5\u4f7f\u7528 Git \u547d\u4ee4\u4e2d\u7684 git reset \u548c git push \u5220\u9664\u5386\u53f2\u8bb0\u5f55\u3002\u9996\u5148\u4f7f\u7528 git log \u547d\u4ee4\u67e5\u770b\u5386\u53f2\u8bb0\u5f55\u3002 $ git log &#8211;oneline \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u4f1a\u5217\u51fa\u5f53\u524d\u5206\u652f\u4e0a\u7684\u6240\u6709\u63d0\u4ea4\u8bb0\u5f55\uff0c\u6bcf\u4e2a\u8bb0\u5f55\u90fd\u6709\u4e00\u4e2a SHA-1 \u6807\u8bc6\u7b26\uff08\u7b2c\u4e00\u5217\uff09\u548c\u4e00\u4e2a\u6d88\u606f\uff08\u540e\u9762\u7684\u5217\uff09\u3002 \u7136\u540e\uff0c\u4f7f\u7528 git reset \u547d\u4ee4\u5c06\u5f53\u524d\u5206\u652f\u91cd\u7f6e\u5230\u8981\u5220\u9664\u7684\u63d0\u4ea4\u4e4b\u524d\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8981\u5220\u9664\u6700\u8fd1\u7684\u4e09\u4e2a\u63d0\u4ea4\u8bb0\u5f55\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528\uff1a $ git reset HEAD~3 \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\u5c31\u5c06\u5f53\u524d\u5206\u652f\u91cd\u7f6e\u5230\u4e86\u524d\u4e09\u4e2a\u63d0\u4ea4\u8bb0\u5f55\u4e4b\u5904\uff0c\u5386\u53f2\u8bb0\u5f55\u4e2d\u7684\u6700\u540e\u4e09\u4e2a\u63d0\u4ea4\u4f1a\u88ab\u5220\u9664\u3002 \u6700\u540e\uff0c\u4f7f\u7528 git push -f \u547d\u4ee4\u5f3a\u5236\u63a8\u9001\u5230\u8fdc\u7a0b\u4ed3\u5e93\uff0c\u8986\u76d6\u5386\u53f2\u8bb0\u5f55\u3002\u8fd9\u4f1a\u4f7f\u5176\u4ed6\u4eba\u5728\u62c9\u53d6\u66f4\u65b0\u65f6\u9047\u5230\u95ee\u9898\uff0c\u56e0\u6b64\u9700\u8981\u786e\u4fdd\u6240\u6709\u6709\u6743\u9650\u7684\u4eba\u77e5\u9053\u6b64\u64cd\u4f5c\u3002 $ git push -f \u767b\u5f55\u540e\u590d\u5236 \u4f7f\u7528\u6b64\u65b9\u6cd5\u5220\u9664\u5386\u53f2\u8bb0\u5f55\u65f6\uff0c\u6211\u4eec\u5efa\u8bae\u5728\u6267\u884c\u4e4b\u524d\u5907\u4efd\u4ee3\u7801\u5e93\uff0c\u4ee5\u9632\u4e07\u4e00\u3002 \u4f7f\u7528 BFG Repo-Cleaner \u53e6\u4e00\u4e2a\u5f3a\u5927\u7684\u5de5\u5177\u662f BFG [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-19135","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/19135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=19135"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/19135\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=19135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=19135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=19135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}