{"id":5182,"date":"2021-04-25T17:54:08","date_gmt":"2021-04-25T09:54:08","guid":{"rendered":"https:\/\/damogame.cn\/wordpress\/?p=5182"},"modified":"2021-04-25T17:54:08","modified_gmt":"2021-04-25T09:54:08","slug":"git-pull%e5%a6%82%e4%bd%95%e5%bc%ba%e5%88%b6%e8%a6%86%e7%9b%96%e6%9c%ac%e5%9c%b0%e6%96%87%e4%bb%b6%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5182","title":{"rendered":"\u201cgit pull\u201d\u5982\u4f55\u5f3a\u5236\u8986\u76d6\u672c\u5730\u6587\u4ef6\uff1f"},"content":{"rendered":"<header>\n<h1><a href=\"https:\/\/vimsky.com\/article\/3679.html\">\u539f\u6587\u5730\u5740<\/a><\/h1>\n<div class=\"info\"><\/div>\n<\/header>\n<article class=\"text\"><code>git pull<\/code>\u5982\u4f55\u5f3a\u5236\u8986\u76d6\u672c\u5730\u6587\u4ef6\u5462\uff1f<\/p>\n<p>\u60c5\u666f\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u56e2\u961f\u6210\u5458\u6b63\u5728\u4fee\u6539\u6211\u4eec\u6b63\u5728\u5904\u7406\u7684\u7f51\u7ad9\u7684\u6a21\u677f<\/li>\n<li>\u4ed6\u4eec\u6b63\u5728\u6dfb\u52a0\u4e00\u4e9b\u56fe\u50cf\u5230\u56fe\u50cf\u76ee\u5f55(\u4f46\u5fd8\u8bb0\u5728\u6e90\u4ee3\u7801\u63a7\u5236\u4e0b\u6dfb\u52a0\u5b83\u4eec)<\/li>\n<li>\u4ed6\u4eec\u901a\u8fc7\u90ae\u4ef6\u5c06\u56fe\u50cf\u53d1\u9001\u7ed9\u6211<\/li>\n<li>\u6211\u5728\u6e90\u4ee3\u7801\u63a7\u5236\u4e0b\u6dfb\u52a0\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4e0e\u5176\u4ed6\u66f4\u6539\u4e00\u8d77\u63a8\u9001\u5230GitHub<\/li>\n<li>\u4ed6\u4eec\u4e0d\u80fd\u4eceGitHub\u63d0\u53d6\u66f4\u65b0\uff0c\u56e0\u4e3aGit\u4e0d\u53ef\u8986\u76d6\u4ed6\u4eec\u7684\u6587\u4ef6\u3002<\/li>\n<\/ul>\n<p>\u5f97\u5230\u7684\u9519\u8bef\u662f\uff1a<\/p>\n<p>error: Untracked working tree file &#8216;public\/images\/icon.gif&#8217; would be overwritten by merge.<\/p>\n<p>\u6211\u5982\u4f55\u5f3a\u5236Git\u8986\u76d6\u5b83\u4eec\uff1f<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6700\u4f73\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<h3>\u91cd\u8981\u63d0\u793a\uff1a\u5982\u679c\u60a8\u6709\u4efb\u4f55\u672c\u5730\u66f4\u6539\uff0c\u5c06\u4f1a\u4e22\u5931\u3002\u65e0\u8bba\u662f\u5426\u6709<code>--hard<\/code>\u9009\u9879\uff0c\u4efb\u4f55\u672a\u88ab\u63a8\u9001\u7684\u672c\u5730\u63d0\u4ea4\u90fd\u5c06\u4e22\u5931\u3002<\/h3>\n<p>\u5982\u679c\u60a8\u6709\u4efb\u4f55\u672a\u88abGit\u8ddf\u8e2a\u7684\u6587\u4ef6(\u4f8b\u5982\u4e0a\u4f20\u7684\u7528\u6237\u5185\u5bb9)\uff0c\u8fd9\u4e9b\u6587\u4ef6\u5c06\u4e0d\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<\/p>\n<hr \/>\n<p>\u4e0b\u9762\u662f\u6b63\u786e\u7684\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"hljs nginx\"><span class=\"hljs-attribute\">git<\/span> fetch --all\r\n<\/code><\/pre>\n<p>\u7136\u540e\uff0c\u4f60\u6709\u4e24\u4e2a\u9009\u62e9\uff1a<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard origin\/master<\/span>\r\n<\/code><\/pre>\n<p>\u6216\u8005\u5982\u679c\u4f60\u5728\u5176\u4ed6\u5206\u652f\u4e0a\uff1a<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard origin\/&lt;branch_name&gt;<\/span>\r\n<\/code><\/pre>\n<h3>\u8bf4\u660e\uff1a<\/h3>\n<p><code>git fetch<\/code>\u4ece\u8fdc\u7a0b\u4e0b\u8f7d\u6700\u65b0\u7684\uff0c\u800c\u4e0d\u5c1d\u8bd5\u5408\u5e76\u6216rebase\u4efb\u4f55\u4e1c\u897f\u3002<\/p>\n<p>\u7136\u540e<code>git reset<\/code>\u5c06\u4e3b\u5206\u652f\u91cd\u7f6e\u4e3a\u60a8\u521a\u521a\u83b7\u53d6\u7684\u5185\u5bb9\u3002\u00a0<code>--hard<\/code>\u9009\u9879\u66f4\u6539\u5de5\u4f5c\u6811\u4e2d\u7684\u6240\u6709\u6587\u4ef6\u4ee5\u5339\u914d<code>origin\/master<\/code>\u4e2d\u7684\u6587\u4ef6<\/p>\n<hr \/>\n<p>[*]\uff1a\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u5728\u91cd\u7f6e\u4e4b\u524d\u53ef\u4ee5\u901a\u8fc7\u4ece<code>master<\/code>\u521b\u5efa\u4e00\u4e2a\u5206\u652f\u6765\u7ef4\u62a4\u5f53\u524d\u7684\u672c\u5730\u63d0\u4ea4\uff1a<\/p>\n<pre><code class=\"hljs sql\">git checkout master\r\ngit branch new-branch-to-save-current-commits\r\ngit fetch <span class=\"hljs-comment\">--all<\/span>\r\ngit <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard origin\/master<\/span>\r\n<\/code><\/pre>\n<p>\u5728\u6b64\u4e4b\u540e\uff0c\u6240\u6709\u65e7\u7684\u63d0\u4ea4\u90fd\u5c06\u4fdd\u5b58\u5728<code>new-branch-to-save-current-commits<\/code>\u4e2d\u3002\u7136\u800c\uff0c\u6ca1\u6709\u63d0\u4ea4\u7684\u66f4\u6539(\u5373\u4f7fstaged)\u5c06\u4f1a\u4e22\u5931\u3002\u786e\u4fdd\u5b58\u50a8\u548c\u63d0\u4ea4\u4efb\u4f55\u4f60\u9700\u8981\u7684\u4e1c\u897f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u6b21\u4f73\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<p>\u5c1d\u8bd5\u8fd9\u4e2a\uff1a<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard HEAD<\/span>\r\ngit pull\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>\u7b2c\u4e09\u79cd\u89e3\u51b3\u65b9\u6cd5<\/h2>\n<p><strong>\u8b66\u544a\uff1a<code>git clean<\/code>\u5220\u9664\u6240\u6709\u672a\u8ddf\u8e2a\u7684\u6587\u4ef6\/\u76ee\u5f55\uff0c\u4e0d\u80fd\u64a4\u6d88\u3002<\/strong><\/p>\n<hr \/>\n<p>\u6709\u7684\u65f6\u5019\u53ea\u6709<code>clean -f<\/code>\u65e0\u6548\u3002\u5982\u679c\u4f60\u6ca1\u6709\u8ddf\u8e2aDIRECTORIES\uff0c-d\u9009\u9879\u4e5f\u9700\u8981\uff1a<\/p>\n<pre><code class=\"hljs sql\">git <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard HEAD<\/span>\r\ngit clean -f -d\r\ngit pull\r\n<\/code><\/pre>\n<p><strong>\u8b66\u544a\uff1a<code>git clean<\/code>\u5220\u9664\u6240\u6709\u672a\u8ddf\u8e2a\u7684\u6587\u4ef6\/\u76ee\u5f55\uff0c\u4e0d\u80fd\u64a4\u6d88\u3002<\/strong><\/p>\n<p>&nbsp;<\/p>\n<h2>\u7b2c\u56db\u79cd\u65b9\u6cd5<\/h2>\n<p>\u53ef\u884c\u7684\u65b9\u5f0f\u662f\u901a\u8fc7\u4f7f\u7528&#8221;fetch&#8221;\u548c&#8221;merge&#8221;\u5b9a\u4e49\u7684\u7b56\u7565\u3002\u8fd9\u5e94\u8be5\u80fd\u4f7f\u4f60\u7684\u672c\u5730\u4fee\u6539\u4fdd\u7559\u4e0b\u6765\uff0c\u53ea\u8981\u5b83\u4eec\u4e0d\u662f\u4f60\u8bd5\u56fe\u5f3a\u5236\u8986\u76d6\u7684\u6587\u4ef6\u4e4b\u4e00\u3002<\/p>\n<h3>\u9996\u5148\u505a\u4e00\u4e2a\u4f60\u7684\u6539\u53d8<\/h3>\n<pre><code class=\"hljs nginx\"> <span class=\"hljs-attribute\">git<\/span> add *\r\n git commit -a -m <span class=\"hljs-string\">\"local file server commit message\"<\/span>\r\n<\/code><\/pre>\n<h3>\u7136\u540e\u83b7\u53d6\u66f4\u6539\u5e76\u8986\u76d6\uff0c\u5982\u679c\u6709\u51b2\u7a81<\/h3>\n<pre><code class=\"hljs sql\"> git fetch origin master\r\n git <span class=\"hljs-keyword\">merge<\/span> -s <span class=\"hljs-keyword\">recursive<\/span> -X theirs origin\/<span class=\"hljs-keyword\">master<\/span>\r\n<\/code><\/pre>\n<p>&#8220;-X&#8221;\u662f\u9009\u9879\u540d\u79f0\uff0c&#8221;theirs&#8221;\u662f\u8be5\u9009\u9879\u7684\u503c\u3002\u5982\u679c\u5b58\u5728\u51b2\u7a81\uff0c\u5219\u9009\u62e9\u4f7f\u7528&#8221;their&#8221;\u66f4\u6539\uff0c\u800c\u4e0d\u662f&#8221;your&#8221;\u66f4\u6539\u3002<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7b2c\u4e94\u79cd\u65b9\u6cd5<\/h2>\n<p>\u8fd9\u662f\u53e6\u5916\u7684\u601d\u8def\uff0c\u4e0d\u5efa\u8bae\u4e0a\u6587\u7684\u505a\u6cd5\uff1a<\/p>\n<pre><code class=\"hljs sql\">git fetch <span class=\"hljs-comment\">--all<\/span>\r\ngit <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard origin\/master<\/span>\r\n<\/code><\/pre>\n<p>\u800c\u662f\u5efa\u8bae\u8fd9\u6837\u505a\uff1a<\/p>\n<pre><code class=\"hljs sql\">git fetch origin master\r\ngit <span class=\"hljs-keyword\">reset<\/span> <span class=\"hljs-comment\">--hard origin\/master<\/span>\r\n<\/code><\/pre>\n<p>\u5982\u679c\u4f60\u6253\u7b97\u91cd\u65b0\u8bbe\u7f6e\u539f\u70b9\/\u4e3b\u5206\u652f\uff0c\u6ca1\u6709\u5fc5\u8981\u83b7\u53d6\u6240\u6709\u7684\u9065\u63a7\u5668\u548c\u5206\u652f<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7b2c\u516d\u79cd\u65b9\u6cd5<\/h2>\n<p>\u770b\u8d77\u6765\u6700\u597d\u7684\u529e\u6cd5\u662f\u5148\u505a\uff1a<\/p>\n<pre><code class=\"hljs nginx\"><span class=\"hljs-attribute\">git<\/span> clean\r\n<\/code><\/pre>\n<p>\u5220\u9664\u6240\u6709\u672a\u8ddf\u8e2a\u7684\u6587\u4ef6\uff0c\u7136\u540e\u7ee7\u7eed\u4f7f\u7528\u901a\u5e38\u7684<code>git pull<\/code>\u00a0&#8230;<\/p>\n<p>&nbsp;<\/p>\n<h2>\u7b2c\u4e03\u79cd\u65b9\u6cd5<\/h2>\n<p>\u6240\u6709\u8fd9\u4e9b\u89e3\u51b3\u65b9\u6848\u7684\u95ee\u9898\u662f\uff0c\u5b83\u4eec\u90fd\u662f\u592a\u590d\u6742\uff0c\u6216\u8005\u66f4\u5927\u7684\u95ee\u9898\u662f\uff0c\u4ed6\u4eec\u4eceWeb\u670d\u52a1\u5668\u4e2d\u5220\u9664\u6240\u6709\u672a\u8ddf\u8e2a\u7684\u6587\u4ef6\uff0c\u8fd9\u662f\u6211\u4eec\u4e0d\u60f3\u8981\u7684\uff0c\u56e0\u4e3a\u603b\u662f\u6709\u9700\u8981\u7684\u914d\u7f6e\u6587\u4ef6\u670d\u52a1\u5668\uff0c\u800c\u4e0d\u662f\u5728Git\u4ed3\u5e93\u3002<\/p>\n<p>\u8fd9\u662f\u6211\u4eec\u6b63\u5728\u4f7f\u7528\u7684\u6700\u5e72\u51c0\u7684\u89e3\u51b3\u65b9\u6848\uff1a<\/p>\n<pre><code class=\"hljs bash\"><span class=\"hljs-comment\"># Fetch the newest code<\/span>\r\ngit fetch\r\n\r\n<span class=\"hljs-comment\"># Delete all files which are being added, so there<\/span>\r\n<span class=\"hljs-comment\"># are no conflicts with untracked files<\/span>\r\n<span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> `git diff HEAD..origin\/master --name-status | awk <span class=\"hljs-string\">'\/^A\/ {print $2}'<\/span>`\r\n<span class=\"hljs-keyword\">do<\/span>\r\n    rm -f -- <span class=\"hljs-string\">\"<span class=\"hljs-variable\">$file<\/span>\"<\/span>\r\n<span class=\"hljs-keyword\">done<\/span>\r\n\r\n<span class=\"hljs-comment\"># Checkout all files which were locally modified<\/span>\r\n<span class=\"hljs-keyword\">for<\/span> file <span class=\"hljs-keyword\">in<\/span> `git diff --name-status | awk <span class=\"hljs-string\">'\/^[CDMRTUX]\/ {print $2}'<\/span>`\r\n<span class=\"hljs-keyword\">do<\/span>\r\n    git checkout -- <span class=\"hljs-string\">\"<span class=\"hljs-variable\">$file<\/span>\"<\/span>\r\n<span class=\"hljs-keyword\">done<\/span>\r\n\r\n<span class=\"hljs-comment\"># Finally pull all the changes<\/span>\r\n<span class=\"hljs-comment\"># (you could merge as well e.g. 'merge origin\/master')<\/span>\r\ngit pull\r\n<\/code><\/pre>\n<ul>\n<li>\u7b2c\u4e00\u4e2a\u547d\u4ee4\u83b7\u53d6\u6700\u65b0\u7684\u6570\u636e\u3002<\/li>\n<li>\u7b2c\u4e8c\u4e2a\u547d\u4ee4\u68c0\u67e5\u662f\u5426\u6709\u4efb\u4f55\u6b63\u5728\u6dfb\u52a0\u5230\u5b58\u50a8\u5e93\u7684\u6587\u4ef6\uff0c\u5e76\u4ece\u672c\u5730\u5b58\u50a8\u5e93\u4e2d\u5220\u9664\u90a3\u4e9b\u4f1a\u5bfc\u81f4\u51b2\u7a81\u7684\u672a\u8ddf\u8e2a\u6587\u4ef6\u3002<\/li>\n<li>\u7b2c\u4e09\u4e2a\u547d\u4ee4checks-out\u6240\u6709\u5728\u672c\u5730\u4fee\u6539\u7684\u6587\u4ef6\u3002<\/li>\n<li>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u66f4\u65b0\u5230\u6700\u65b0\u7248\u672c\uff0c\u4f46\u662f\u8fd9\u6b21\u6ca1\u6709\u4efb\u4f55\u51b2\u7a81\uff0c\u56e0\u4e3arepo\u4e2d\u7684\u672a\u8ddf\u8e2a\u6587\u4ef6\u4e0d\u518d\u5b58\u5728\uff0c\u6240\u6709\u672c\u5730\u4fee\u6539\u7684\u6587\u4ef6\u5df2\u7ecf\u4e0e\u5b58\u50a8\u5e93\u4e2d\u7684\u76f8\u540c\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tuku.vimsky.com\/images\/2017\/12\/git_pull_x.jpg\" width=\"1100\" height=\"481\" \/><\/p>\n<h2>\u53c2\u8003\u6587\u732e<\/h2>\n<ul>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/1125968\/how-do-i-force-git-pull-to-overwrite-local-files\" target=\"_blank\" rel=\"nofollow noopener\">How do I force \u201cgit pull\u201d to overwrite local files?<\/a><\/li>\n<\/ul>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 git pull\u5982\u4f55\u5f3a\u5236\u8986\u76d6\u672c<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-5182","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5182"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5182\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}