{"id":2622,"date":"2010-10-23T01:09:01","date_gmt":"2010-10-22T17:09:01","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/?p=2622"},"modified":"2024-04-21T18:49:08","modified_gmt":"2024-04-21T10:49:08","slug":"git-part3-reset-rebase","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/2622-git-part3-reset-rebase","title":{"rendered":"Git \u7248\u672c\u63a7\u5236\u7cfb\u7d71(3) \u9084\u6c92 push \u524d\u53ef\u4ee5\u505a\u7684\u4e8b"},"content":{"rendered":"<blockquote><p>\n  \u95dc\u65bc Git \u53ef\u4ee5\u53c3\u8003\u6211\u7684 <a href=\"https:\/\/ihower.tw\/git\/\">Git \u7248\u672c\u63a7\u5236<\/a> \u8ab2\u7a0b\u8cc7\u6599\n<\/p><\/blockquote>\n<p>\u9019\u4e00\u96c6\u8981\u8b1b\u7684\u662f\uff1a\u9084\u6c92 push \u524d\u53ef\u4ee5\u505a\u7684<del>\u58de<\/del>\u4e8b\uff0c\u4e5f\u5c31\u662f reset \u8ddf rebase\u3002<\/p>\n<p>\u76f8\u8f03\u65bc SVN \u9019\u7a2e commit \u5c31\u662f\u9001\u5230\u9060\u7aef\u4f3a\u670d\u5668\uff0cGit \u7684 commit \u5176\u5be6\u6771\u897f\u90fd\u9084\u662f\u5728\u672c\u5730\u7aef\uff0c\u6240\u4ee5\u53ea\u8981\u4f60\u9084\u6c92 push \u51fa\u53bb\u5206\u4eab\u7d66\u5225\u4eba\uff0c\u4f60\u7684 commit logs \u662f\u53ef\u4ee5\u4fee\u6539\u7684!! \u9019\u7a2e\u529f\u80fd\u975e\u5e38\u7684 powerful\uff0c\u53ef\u4ee5\u8b93\u4f60 undo \u548c rewrite commit history\u3002\u5982\u679c\u4f60\u7528 Git \u53ea\u6703 git commit \u7136\u5f8c\u63a5\u8457\u99ac\u4e0a git push\uff0c\u90a3\u4f60\u5c31\u6c92\u6709\u5b78\u5230\u7cbe\u96a8\u554a!~<\/p>\n<blockquote><p>\u4f7f\u7528\u544a\u8aa1\uff1a\u5982\u679c\u4f60\u5df2\u7d93 push \u51fa\u53bb\u4e86\uff0c\u8acb\u5343\u842c\u4e0d\u8981\u505a rewrite history \u7684\u52d5\u4f5c\uff0c\u6703\u5929\u4e0b\u5927\u4e82\u554a\u3002\u6b63\u78ba\u5584\u7528 undo changes\/rewrite history \u7684\u529f\u80fd\uff0c\u6211\u5011\u53ef\u4ee5\u8b93\u6771\u897f\u6e96\u5099\u597d\u5f04\u7684\u6574\u6574\u9f4a\u9f4a\u624d push \u51fa\u53bb\u7d66\u5225\u4eba pull\u3002<\/p><\/blockquote>\n<p>\u8b93\u6211\u5011\u4f86\u5b78\u5e7e\u62db\u5427\uff1a<\/p>\n<h3>amend<\/h3>\n<p>git commit -C HEAD -a &#8211;amend \u5feb\u901f\u4fee\u6b63\u524d\u4e00\u6b21 commit \u7684\u932f\u8aa4\uff0c\u53ea\u8981\u4fee typo \u4e4b\u5f8c\u6253\u9019\u884c\uff0c\u5c31\u6703\u66ff\u63db\u6389\u524d\u4e00\u6b21\u7684 commit<\/p>\n<h3>reset<\/h3>\n<p>git reset \u780d\u6389 commit \u91cd\u4f86\uff0c\u4f46\u662f\u4fee\u6539\u7684\u7a0b\u5f0f\u9084\u662f\u7559\u5728 working tree\u3002\u4f8b\u5982\uff1a<\/p>\n<p>git reset HEAD^ \u5c31\u6703\u56de\u5230\u524d\u4e00\u7248\u672c(\u4e00\u500b^\u8868\u793a\u662f\u524d\u4e00\u7248)\uff0c\u4e26\u628a\u5176\u4e2d\u7684 changes \u7e7c\u7e8c\u7559\u5728 working tree \u4e2d\u3002\u9069\u5408\u767c\u73fe\u524d\u4e00\u6b21 commit \u6709\u554f\u984c\u6216\u662f\u60f3\u8981\u4fee\u6539 commit log\uff0c\u53ef\u4ee5\u4fee\u6539\u5f8c\u518d\u91cd\u65b0 commit\u3002<\/p>\n<p>git reset \u5982\u679c\u52a0\u4e0a &#8211;soft \u53c3\u6578\u5247\u6703\u628a changes \u76f4\u63a5\u52a0\u5230 staging area\u3002<\/p>\n<p>\u52a0\u4e0a &#8211;hard \u53c3\u6578\u8868\u793a\u4e0d\u7559 staging area \u4e5f\u4e0d\u7559 working tree(\u5b8c\u5168\u522a\u9664\u4efb\u4f55\u4fee\u6539\u8a18\u9304)\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>git reset &#8211;hard HEAD^ \u5247\u6703\u5b8c\u5168\u62b9\u6389\u524d\u4e00\u6b21\u7684 commit\u3002<\/p>\n<p>\u53e6\u500b\u5e38\u7528\u7684\u60c5\u5883\u662f\u8981\u628a\u4fee\u6539\u7684\u6a94\u6848\u5f9e staging area \u79fb\u8d70\uff0c\u6307\u4ee4\u5c31\u662f git reset HEAD filename (\u4f46\u9084\u662f\u7559\u5728working tree)\u3002BTW\uff0c\u8981\u56de\u5fa9 working tree \u4e2d\u4fee\u6539\u7684\u6a94\u6848\u6210\u672c\u4f86\u7684\u6a23\u5b50\uff0c\u6307\u4ee4\u662f git checkout filename (\u91cd\u65b0\u62ff\u51fa\u672c\u4f86\u7684\u6a94\u6848)<\/p>\n<p>\u9806\u9053\u4e00\u63d0\uff0c\u5982\u679c\u6771\u897f\u5df2\u7d93 push \u51fa\u4f86\u4e86\uff0c\u8981\u600e\u9ebc\u56de\u5fa9? \u9019\u6642\u5019\u5c31\u5f97\u7528 revert \u4e86\u3002<\/p>\n<p>git revert \u6703\u7528\u4e00\u500b\u65b0\u7684 commit \u4f86\u56de\u5fa9\u6240\u6709\u7684\u8b8a\u66f4(\u9069\u5408\u5df2\u7d93push\u51fa\u53bb\u7d66\u5225\u4eba\u7684\u60c5\u5883)\u3002\u52a0\u4e0a -n \u53ef\u4ee5\u4e0d\u5148 commit\uff0c\u9019\u6a23\u53ef\u4ee5\u591a revert \u5e7e\u6b21\u5f8c\u518d\u4e00\u6b21 commit\u3002<\/p>\n<p>\u6307\u4ee4\u6703\u6709\u5dee\u5225\u90fd\u662f\u56e0\u70ba remote repo.&lt;-&gt; local repo.&lt;-&gt;staging area&lt;-&gt;working tree \u5206\u4e86\u56db\u5c64\u79fb\u4f86\u79fb\u53bb\u7684\u95dc\u4fc2\uff0c\u4f60\u53ea\u8981\u60f3\u6e05\u695a\u9019\u5e7e\u5c64\u5c31\u878d\u6703\u8cab\u901a\u4e86 (\u8acb\u53c3\u8003<a href=\"\/blog\/archives\/2591\">\u7b2c\u4e00\u96c6<\/a>\u6709\u89e3\u91cb staging area \u548c working tree)\u3002<\/p>\n<h3>rebase<\/h3>\n<p>\u6211\u5728 <a href=\"\/blog\/archives\/3843\">\u4f7f\u7528 rebase \u907f\u514d\u7121\u8b02\u7684 merge<\/a> \u4e00\u6587\u4e2d\u6709\u89e3\u91cb\u904e rebase \u7684\u5176\u4e2d\u4e00\u500b\u7528\u9014\u3002\u800c rebase \u7684\u771f\u6b63\u6f5b\u529b\u662f\uff0c\u6211\u5011\u53ef\u4ee5\u5f9e\u6307\u5b9a\u7684\u7248\u865f\u4e4b\u5f8c\uff0c\u91cd\u65b0\u96a8\u4f60\u610f commit \u4e00\u6b21\u4f86\u91cd\u5efa history\uff0c\u8d85\u5a01\u7684\u3002\u9996\u5148\u8f38\u5165 git rebase -i \u7248\u865f \u5c31\u6703\u53ef\u4ee5\u8df3\u51fa editor \u53ef\u4ee5\u7de8\u8f2f\uff0c\u6211\u5011\u53ef\u4ee5<\/p>\n<p>a. \u8b8a\u66f4 commit \u9806\u5e8f<br \/>\nb. \u5c07\u591a\u500b commit \u5408\u4f75 squash<br \/>\nc. \u5c07\u4e00\u500b commit \u6253\u6563 (edit \u6703\u505c\u8457\u8b93\u4f60\u53ef\u4ee5 git reset HEAD^ \u6253\u6563\u91cd\u65b0 commit\uff0c\u5b8c\u6210\u5f8c git rebase &#8211;continue )<\/p>\n<p>\u53e6\u4e00\u7a2e rebase \u7528\u6cd5\u662f\u4e0d\u9700\u8981\u6253 -i \uff0c\u76f4\u63a5\u6307\u5b9a\u53e6\u4e00\u500b branch \u6216 tag\uff0c\u9019\u6a23\u5c31\u6703\u91cd\u65b0 commit \u53e6\u4e00\u500b branch \u7684\u6771\u897f\uff0c\u7136\u5f8c\u624d commit \u81ea\u5df1\u7684 (\u4e5f\u5c31\u662f <a href=\"\/blog\/archives\/3843\">\u4f7f\u7528 rebase \u907f\u514d\u7121\u8b02\u7684 merge<\/a> \u7684\u7528\u6cd5)\u3002<\/p>\n<p>git rebase \u82e5\u6709 conflict \u5c31\u6703\u505c\u4e0b\u4f86, \u8ddf merge \u4e00\u6a23\u8655\u7406\u5b8c add\uff0c\u7136\u5f8c git rebase &#8211;continue \u5c31\u6703\u7e7c\u7e8c commit (\u4e5f\u53ef\u4ee5 &#8211;skip \u6216 &#8211;abort \u653e\u68c4\u5566)<\/p>\n<p>rebase \u6709\u500b onto \u53c3\u6578\u7528\u6cd5\uff0c\u4f7f\u7528\u7684\u60c5\u5883\u662f\uff1a\u5047\u8a2d\u4f60\u6709\u4e09\u500b\u6709 dependency \u7684 branch \u5206\u5225\u53eb\u505a master\/contact\/search\uff0c\u5f8c\u4f86\u767c\u73fe search branch \u53ea\u6709 depend on matser\uff0c\u65bc\u662f\u4f60\u53ef\u4ee5\u8f38\u5165 git rebase &#8211;onto master contact search \u9019\u6a23\u5c31\u6703\u8b93 search branch \u5f9e master \u7684\u5730\u65b9\u958b\u59cb\u91cd\u65b0 commit\u3002<\/p>\n<p>\u518d\u6b21\u63d0\u9192\uff0crebase \u5343\u842c\u53ea\u80fd\u9069\u5408\u6771\u897f\u9084\u6c92 push \u7684\u60c5\u5883\uff0c\u6216\u662f\u4f60\u81ea\u5df1\u7684 local \u5c08\u7528\u79c1\u4eba branch\u3002rebase \u4e00\u500b\u5df2\u7d93 push \u51fa\u53bb\u7684 repository\uff0c\u7136\u5f8c\u4f60\u53c8\u628a\u4fee\u6539\u7684 history push \u51fa\u53bb\uff0c\u662f\u6703\u9020\u6210\u8d85\u7d1a\u5927\u707d\u96e3\u7684\u3002<\/p>\n<p>\u5728\u5b78\u7fd2 rebase \u7684\u904e\u7a0b\u4e2d\uff0c\u5f88\u5bb9\u6613\u62ff\u4f86\u8ddf merge \u6bd4\u8f03\u4e00\u4e0b\u3002\u6211\u767c\u73fe\u4e00\u500b\u6709\u8da3\u7684\u4e0d\u540c\u9ede\uff1a\u5982\u679c\u6709\u6a94\u6848\u5728\u8981\u88ab merged \u7684 branch \u4e2d\u88ab\u522a\u9664\uff0c\u5982\u679c\u7528 rebase \u6a94\u6848\u6700\u5f8c\u6703\u4e0d\u5b58\u5728\uff0c\u4f46\u662f\u7528 merge \u7684\u8a71\u6a94\u6848\u6700\u5f8c\u9084\u5728 XDXD<\/p>\n<p>\u53e6\u5916\uff0c\u5728 rebase branch \u4e4b\u5f8c\uff0c\u5982\u679c\u518d\u505a merge\uff0c\u5c31\u6703\u767c\u73fe\u56e0\u70ba master \u76f4\u63a5\u5c31\u662f\u88ab merge \u7684\u7956\u5148\uff0c\u6240\u4ee5\u7dda\u5716\u76f4\u63a5\u8b8a\u6210\u4e00\u689d\u7dda\uff0c\u800c\u6709\u9019\u7a2e parent \u95dc\u4fc2\u7684 merge \u5c31\u53eb\u505a fast-forward\u3002\u63db\u53e5\u8a71\u8aaa\uff0c\u56e0\u70ba\u6c92\u6709\u767c\u751f\u4efb\u4f55 merge commit\uff0c\u4e5f\u4e0d\u6703\u767c\u751f conflict\uff0cGit \u5167\u90e8\u55ae\u7d14\u53ea\u662f\u8b8a\u66f4 reference \u53c3\u7167\uff0c\u6240\u4ee5\u8b02\u4e4b fast-forward\u3002<\/p>\n<p>\u597d\u5fc3\u63d0\u9192\uff0c\u56e0\u70ba\u958b local branch \u662f\u5982\u6b64\u4fbf\u5b9c\u7121\u5bb3\uff0c\u6240\u4ee5\u8981\u505a rebase \u6642\u5efa\u8b70\u60a8\u53ef\u4ee5\u5148\u958b\u4e00\u500b local branch \u4f86\u5be6\u9a57 rebase\u3002\u8001\u5be6\u8aaa\uff0crebase \u9084\u633a\u5371\u96aa\u7684 XDXD<\/p>\n<p>\u6700\u5f8c\uff0c<a href=\"http:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/git-rebase.html\">rebase<\/a> \u6211\u8a8d\u70ba\u7b97\u662f Git \u521d\u5b78\u8005\u6700\u96e3\u7406\u89e3\u7684\u529f\u80fd\u5427\uff0c\u4f46\u662f\u5982\u679c\u4e0d\u77e5\u9053\u4ec0\u9ebc\u662f rebase\uff0c\u5c31\u4e0d\u80fd\u8aaa\u662f\u61c2 Git \u554a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95dc\u65bc Git \u53ef\u4ee5\u53c3\u8003\u6211\u7684 Git \u7248\u672c\u63a7\u5236 \u8ab2\u7a0b\u8cc7\u6599 \u9019\u4e00\u96c6\u8981\u8b1b\u7684\u662f\uff1a\u9084\u6c92 push \u524d\u53ef\u4ee5\u505a\u7684\u58de\u4e8b\uff0c\u4e5f\u5c31\u662f &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/2622-git-part3-reset-rebase\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008Git \u7248\u672c\u63a7\u5236\u7cfb\u7d71(3) \u9084\u6c92 push \u524d\u53ef\u4ee5\u505a\u7684\u4e8b\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[48],"tags":[],"class_list":["post-2622","post","type-post","status-publish","format-standard","hentry","category-git","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-Gi","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/2622","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=2622"}],"version-history":[{"count":28,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/2622\/revisions"}],"predecessor-version":[{"id":12073,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/2622\/revisions\/12073"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=2622"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=2622"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=2622"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}