{"id":6054,"date":"2025-06-03T11:53:27","date_gmt":"2025-06-03T03:53:27","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=6054"},"modified":"2025-06-03T11:53:42","modified_gmt":"2025-06-03T03:53:42","slug":"gitpull","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=6054","title":{"rendered":"Git\u76842\u79cd\u5f3a\u5236Pull"},"content":{"rendered":"\n<p>\u5728\u7248\u672c\u673a\u4e0a\uff0c\u6709\u53ef\u80fd\u672c\u5730\u6587\u4ef6\u88ab\u4fee\u6539\uff0c\u62c9\u53d6git\u65f6\uff0c\u67092\u79cd\u5904\u7406\u65b9\u6cd5<\/p>\n\n\n\n<p>1\u3001\u5168\u91cf\u91cd\u7f6e<\/p>\n\n\n\n<p>\u4f7f\u7528git reset &#8211;hard HEAD<\/p>\n\n\n\n<p>2\u3001\u4ec5\u5bf9\u8fdc\u7a0b\u6709\u4fee\u6539\u7684\u6587\u4ef6\u8fdb\u884c\u8986\u76d6\u548c\u66f4\u65b0\uff0c\u672a\u4fee\u6539\u7684\u4fdd\u6301\u672c\u5730\u7248\u672c<\/p>\n\n\n\n<p>\u53ef\u4ee5\u5148\u7528git pull\uff0c\u83b7\u5f97\u8f93\u51fa\u540e\uff0c\u518d\u7528git checkout filename \u8fdb\u884c\u56de\u6eda<\/p>\n\n\n\n<p>\u4ee3\u7801\u5982\u4e0b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System;\r\n\r\nnamespace GitPull\r\n{\r\n    internal class Program\r\n    {\r\n        static void Main(string&#91;] args)\r\n        {\r\n            var workDir = System.Environment.CurrentDirectory;\r\n\r\n            string fetchError = string.Empty;\r\n            Execute(workDir, \"git\", \"fetch\", ref fetchError);\r\n\r\n            for (int i = 0; i &lt; 2; ++i)\r\n            {\r\n                string pullError = string.Empty;\r\n                var pullOutput = Execute(workDir, \"git\", \"pull\", ref pullError);\r\n                if (pullError.Length > 0)\r\n                {\r\n                    Checkouts(workDir,pullError);\r\n                }\r\n            }\r\n        }\r\n\r\n        private static void Checkouts(string workDir, string pullError)\r\n        {\r\n            var lines = pullError.Split('\\n');\r\n            if (lines.Length == 0)\r\n            {\r\n                return;\r\n            }\r\n\r\n            if (lines&#91;0].Contains(\"Your local changes to the following files would be overwritten by merge:\"))\r\n            {\r\n                for (int i = 1; i &lt; lines.Length; i++)\r\n                {\r\n                    var line = lines&#91;i];\r\n                    if (line.StartsWith(\"Please commit your changes or stash them before you merge\"))\r\n                    {\r\n                        break;\r\n                    }\r\n\r\n                    var filename = line.Trim();\r\n                    GitCheckoutFile(workDir, filename);\r\n                }\r\n            }\r\n        }\r\n\r\n        private static void GitCheckoutFile(string workDir, string filename)\r\n        {\r\n            string err = string.Empty;\r\n            Execute(workDir, \"git\", $\"checkout {filename}\", ref err);\r\n        }\r\n\r\n        private static string Execute(string workDir, string cmd, string args, ref string errInfo)\r\n        {\r\n            Console.WriteLine($\"&#91;Execute]{cmd} {args}\");\r\n            string output = string.Empty;\r\n            var myProcess = new System.Diagnostics.Process();\r\n            try\r\n            {\r\n                myProcess.StartInfo.UseShellExecute = false;\r\n                myProcess.StartInfo.FileName = cmd;\r\n                myProcess.StartInfo.CreateNoWindow = true;\r\n                myProcess.StartInfo.Arguments = args;\r\n                myProcess.StartInfo.RedirectStandardInput = true;\r\n                myProcess.StartInfo.RedirectStandardOutput = true;\r\n                myProcess.StartInfo.RedirectStandardError = true;\r\n                myProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;\r\n                myProcess.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;\r\n                myProcess.Start();\r\n                var pout = myProcess.StandardOutput.ReadToEnd();\r\n                var perr = myProcess.StandardError.ReadToEnd();\r\n                if (pout.Length > 0)\r\n                {\r\n                    output = pout;\r\n                }\r\n                if (perr.Length > 0)\r\n                {\r\n                    errInfo = perr;\r\n                }\r\n                myProcess.WaitForExit();\r\n                myProcess.Close();\r\n            }\r\n            catch (System.Exception e)\r\n            {\r\n                output = e.ToString();\r\n            }\r\n\r\n            if (errInfo.Length > 0)\r\n            {\r\n                Console.WriteLine($\"&#91;Info]{errInfo}\");\r\n            }\r\n\r\n            if (output.Length > 0)\r\n            {\r\n                Console.WriteLine($\"&#91;Info]{output}\");\r\n            }\r\n\r\n            return output;\r\n        }\r\n    }\r\n}\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u7248\u672c\u673a\u4e0a\uff0c\u6709\u53ef\u80fd\u672c\u5730\u6587\u4ef6\u88ab\u4fee\u6539\uff0c\u62c9\u53d6git\u65f6\uff0c\u67092\u79cd\u5904\u7406\u65b9\u6cd5 1\u3001\u5168\u91cf\u91cd\u7f6e \u4f7f\u7528git reset &#038;#82 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6054"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6054"}],"version-history":[{"count":2,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6054\/revisions"}],"predecessor-version":[{"id":6056,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/6054\/revisions\/6056"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}