{"id":1947,"date":"2023-11-28T16:00:54","date_gmt":"2023-11-28T08:00:54","guid":{"rendered":"http:\/\/zhang.mba\/?p=1947"},"modified":"2023-11-28T16:05:33","modified_gmt":"2023-11-28T08:05:33","slug":"python-cao-zuo-git-dai-magitpython","status":"publish","type":"post","link":"https:\/\/zhang.mba\/index.php\/2023\/11\/28\/16\/00\/54\/1947\/python-cao-zuo-git-dai-magitpython\/python\/zhangzhiqi\/","title":{"rendered":"GitPython Python \u64cd\u4f5c Git"},"content":{"rendered":"<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/mmbiz.qpic.cn\/sz_mmbiz_jpg\/REwWx1OBrphxna2PAicyHSahZjfFx5ia197ZA1JialEsOdxDCI7L1Oql3zezBJNWCoXrz1Tshbqj9cqnP7X9C6WGA\/640?wx_fmt=jpeg&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/mmbiz.qpic.cn\/sz_mmbiz_jpg\/REwWx1OBrphxna2PAicyHSahZjfFx5ia197ZA1JialEsOdxDCI7L1Oql3zezBJNWCoXrz1Tshbqj9cqnP7X9C6WGA\/640?wx_fmt=jpeg&amp;from=appmsg&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\u56fe\u7247\" \/><\/div><\/p>\n<p>\u6709\u65f6\uff0c\u9700\u8981\u505a\u590d\u6742\u7684 Git \u64cd\u4f5c\uff0c\u5e76\u4e14\u6709\u5f88\u591a\u4e2d\u95f4\u903b\u8f91\u3002\u7528 Shell \u505a\u590d\u6742\u7684\u903b\u8f91\u8fd0\u7b97\u4e0e\u6d41\u7a0b\u63a7\u5236\u5c31\u662f\u4e00\u4e2a\u707e\u96be\u3002\u6240\u4ee5\uff0c\u7528 Python \u6765\u5b9e\u73b0\u662f\u4e00\u4e2a\u6109\u5feb\u7684\u9009\u62e9\u3002\u8fd9\u65f6\uff0c\u5c31\u9700\u8981\u5728 Python \u4e2d\u64cd\u4f5c Git \u7684\u5e93\u3002<\/p>\n<p><strong>GitPython \u7b80\u4ecb<\/strong><\/p>\n<p>GitPython\u662f\u4e00\u4e2a\u4e0eGit\u5e93\u4ea4\u4e92\u7684Python\u5e93\uff0c\u5305\u62ec\u5e95\u5c42\u547d\u4ee4\uff08Plumbing\uff09\u4e0e\u9ad8\u5c42\u547d\u4ee4\uff08Porcelain\uff09\u3002\u5b83\u53ef\u4ee5\u5b9e\u73b0\u7edd\u5927\u90e8\u5206\u7684Git\u8bfb\u5199\u64cd\u4f5c\uff0c\u907f\u514d\u4e86\u9891\u7e41\u4e0eShell\u4ea4\u4e92\u7684\u7578\u5f62\u4ee3\u7801\u3002\u5b83\u5e76\u975e\u662f\u4e00\u4e2a\u7eaf\u7cb9\u7684Python\u5b9e\u73b0\uff0c\u800c\u662f\u6709\u4e00\u90e8\u5206\u4f9d\u8d56\u4e8e\u76f4\u63a5\u6267\u884c<code>git<\/code>\u547d\u4ee4\uff0c\u53e6\u4e00\u90e8\u5206\u4f9d\u8d56\u4e8eGitDB\u3002<\/p>\n<p>GitDB\u4e5f\u662f\u4e00\u4e2aPython\u5e93\u3002\u5b83\u4e3a<code>.git\/objects<\/code>\u5efa\u7acb\u4e86\u4e00\u4e2a\u6570\u636e\u5e93\u6a21\u578b\uff0c\u53ef\u4ee5\u5b9e\u73b0\u76f4\u63a5\u7684\u8bfb\u5199\u3002\u7531\u4e8e\u91c7\u7528\u6d41\u5f0f\uff08stream\uff09\u8bfb\u5199\uff0c\u6240\u4ee5\u8fd0\u884c\u9ad8\u6548\u3001\u5185\u5b58\u5360\u7528\u4f4e\u3002<\/p>\n<h2><a id=\"gitpython%E5%AE%89%E8%A3%85\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>GitPython\u5b89\u88c5<\/strong><\/h2>\n<p>\u5176\u4f9d\u8d56GitDB\u4f1a\u81ea\u52a8\u5b89\u88c5\uff0c\u4e0d\u8fc7\u53ef\u6267\u884c\u7684<code>git<\/code>\u547d\u4ee4\u9700\u8981\u989d\u5916\u5b89\u88c5\u3002<\/p>\n<h2><a id=\"%E5%9F%BA%E6%9C%AC%E7%94%A8%E6%B3%95\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>\u57fa\u672c\u7528\u6cd5<\/strong><\/h2>\n<h3><a id=\"init\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>init<\/strong><\/h3>\n<pre class=\"line-numbers\"><code class=\"language-python\">import git\nrepo = git.Repo.init(path='.')\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u5728\u5f53\u524d\u76ee\u5f55\u521b\u5efa\u4e86\u4e00\u4e2aGit\u5e93\u3002\u5f53\u7136\uff0c\u8def\u5f84\u53ef\u4ee5\u81ea\u5b9a\u4e49\u3002<\/p>\n<p>\u7531\u4e8e<code>git.Repo<\/code>\u5b9e\u73b0\u4e86<code>__enter__<\/code>\u4e0e<code>__exit__<\/code>\uff0c\u6240\u4ee5\u53ef\u4ee5\u4e0e<code>with<\/code>\u8054\u5408\u4f7f\u7528\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">with git.Repo.init(path='.') as repo:\n    # do sth with repo\n<\/code><\/pre>\n<p>\u4e0d\u8fc7\uff0c\u7531\u4e8e\u53ea\u662f\u5b9e\u73b0\u4e86\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\uff0c\u5173\u95ed\u540e\u4ecd\u7136\u53ef\u4ee5\u8bfb\u5199\uff0c\u6240\u4ee5\u4f7f\u7528\u8fd9\u79cd\u5f62\u5f0f\u7684\u5fc5\u8981\u6027\u4e0d\u9ad8\u3002<\/p>\n<h3><a id=\"clone\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>clone<\/strong><\/h3>\n<p>clone\u5206\u4e24\u79cd\u3002\u4e00\u662f\u4ece\u5f53\u524d\u5e93clone\u5230\u53e6\u4e00\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">new_repo = repo.clone(path='..\/new')\n<\/code><\/pre>\n<p>\u4e8c\u662f\u4ece\u67d0\u4e2aURL\u90a3\u91ccclone\u5230\u672c\u5730\u67d0\u4e2a\u4f4d\u7f6e\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">po = git.Repo.clone_from(url='git@github.com:USER\/REPO.git', to_path='..\/new')\n<\/code><\/pre>\n<h3><a id=\"commit\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>commit<\/strong><\/h3>\n<pre class=\"line-numbers\"><code class=\"language-python\">with open('test.file', 'w') as fobj:\n    fobj.write('1st line\\n')\nrepo.index.add(items=['test.file'])\nrepo.index.commit('write a line into test.file')\n\nwith open('test.file', 'aw') as fobj:\n    fobj.write('2nd line\\n')\nrepo.index.add(items=['test.file'])\nrepo.index.commit('write another line into test.file')\n<\/code><\/pre>\n<h3><a id=\"status\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>status<\/strong><\/h3>\n<p>GitPython\u5e76\u672a\u5b9e\u73b0\u539f\u7248<code>git status<\/code>\uff0c\u800c\u662f\u7ed9\u51fa\u4e86\u90e8\u5206\u7684\u4fe1\u606f\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">&gt;&gt;&gt; repo.is_dirty()\nFalse\n&gt;&gt;&gt; with open('test.file', 'aw') as fobj:\n&gt;&gt;&gt;     fobj.write('dirty line\\n')\n&gt;&gt;&gt; repo.is_dirty()\nTrue\n<\/code><\/pre>\n<pre class=\"line-numbers\"><code class=\"language-python\">&gt;&gt;&gt; repo.untracked_files\n[]\n&gt;&gt;&gt; with open('untracked.file', 'w') as fobj:\n&gt;&gt;&gt;     fobj.write('')\n&gt;&gt;&gt; repo.untracked_files\n['untracked.file']\n<\/code><\/pre>\n<h3><a id=\"checkout%EF%BC%88%E6%B8%85%E7%90%86%E6%89%80%E6%9C%89%E4%BF%AE%E6%94%B9%EF%BC%89\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>checkout\uff08\u6e05\u7406\u6240\u6709\u4fee\u6539\uff09<\/strong><\/h3>\n<pre class=\"line-numbers\"><code class=\"language-python\">&gt;&gt;&gt; repo.is_dirty()\nTrue\n&gt;&gt;&gt; repo.index.checkout(force=True)\n&lt;generator object &lt;genexpr&gt; at 0x7f2bf35e6b40&gt;\n&gt;&gt;&gt; repo.is_dirty()\nFalse\n<\/code><\/pre>\n<h3><a id=\"branch\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>branch<\/strong><\/h3>\n<p>\u83b7\u53d6\u5f53\u524d\u5206\u652f\uff1a<\/p>\n<p>\u65b0\u5efa\u5206\u652f\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">new_head = repo.create_head('new_head', 'HEAD^')\n<\/code><\/pre>\n<p>\u5207\u6362\u5206\u652f\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">new_head.checkout()\nhead.checkout()\n<\/code><\/pre>\n<p>\u5220\u9664\u5206\u652f\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">git.Head.delete(repo, new_head)\n# or\ngit.Head.delete(repo, 'new_head')\n<\/code><\/pre>\n<h3><a id=\"merge\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>merge<\/strong><\/h3>\n<p>\u4ee5\u4e0b\u6f14\u793a\u5982\u4f55\u5728\u4e00\u4e2a\u5206\u652f\uff08<code>other<\/code>\uff09\uff0cmerge\u53e6\u4e00\u4e2a\u5206\u652f\uff08<code>master<\/code>\uff09\u3002<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">master = repo.heads.master\nother = repo.create_head('other', 'HEAD^')\nother.checkout()\nrepo.index.merge_tree(master)\nrepo.index.commit('Merge from master to other')\n<\/code><\/pre>\n<h3><a id=\"remote-fetch-pull-push\" class=\"anchor\" aria-hidden=\"true\"><span class=\"octicon octicon-link\"><\/span><\/a><strong>remote, fetch, pull, push<\/strong><\/h3>\n<p>\u521b\u5efaremote\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">remote = repo.create_remote(name='gitlab', url='git@gitlab.com:USER\/REPO.git')\n<\/code><\/pre>\n<p>\u8fdc\u7a0b\u4ea4\u4e92\u64cd\u4f5c\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">remote = repo.remote()\nremote.fetch()\nremote.pull()\nremote.push()\n<\/code><\/pre>\n<p>\u5220\u9664remote\uff1a<\/p>\n<pre class=\"line-numbers\"><code class=\"language-python\">repo.delete_remote(remote)\n# or\nrepo.delete_remote('gitlab')\n<\/code><\/pre>\n<p>\u6765\u6e90&#8212;&#8211;\u84dd\u80d6\u5b50\u4e4b\u5bb6<\/p>\n<!--CusAds0-->\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u6709\u65f6\uff0c\u9700\u8981\u505a\u590d\u6742\u7684 Git \u64cd\u4f5c\uff0c\u5e76\u4e14\u6709\u5f88\u591a\u4e2d\u95f4\u903b\u8f91\u3002\u7528 Shell \u505a\u590d\u6742\u7684\u903b\u8f91\u8fd0\u7b97\u4e0e\u6d41\u7a0b\u63a7\u5236\u5c31\u662f\u4e00\u4e2a\u707e\u96be\u3002\u6240\u4ee5\uff0c\u7528 Python \u6765\u5b9e\u73b0\u662f\u4e00\u4e2a\u6109\u5feb\u7684\u9009\u62e9\u3002\u8fd9\u65f6\uff0c\u5c31\u9700\u8981\u5728 Python \u4e2d\u64cd\u4f5c Git \u7684\u5e93\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0},"categories":[67,12],"tags":[],"_links":{"self":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1947"}],"collection":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/comments?post=1947"}],"version-history":[{"count":0,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/posts\/1947\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/media?parent=1947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/categories?post=1947"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhang.mba\/index.php\/wp-json\/wp\/v2\/tags?post=1947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}