{"id":5351,"date":"2022-11-02T20:15:13","date_gmt":"2022-11-02T12:15:13","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=5351"},"modified":"2022-11-03T16:46:20","modified_gmt":"2022-11-03T08:46:20","slug":"%e4%bb%8e-git-credentials-%e6%96%87%e4%bb%b6%e8%af%b4%e8%b5%b7","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/5351.html","title":{"rendered":"\u4ece .git-credentials \u6587\u4ef6\u8bf4\u8d77"},"content":{"rendered":"\n<p>=Start=<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u7f18\u7531\uff1a<\/h4>\n\n\n\n<p>\u7531\u524d\u6bb5\u65f6\u95f4\u53d1\u73b0\u7684\u4e00\u4e2a\u5185\u90e8case\u5f15\u51fa\u7684\u5bf9git credential\u64cd\u4f5c\u7684\u6d4b\u8bd5\u3002\u8fd9\u91cc\u7b80\u5355\u8bb0\u5f55\u4e00\u4e0b git-credential \u7684\u76f8\u5173\u914d\u7f6e\u548c\u95ee\u9898\uff0c\u4ee5\u53ca\u6211\u4e2a\u4eba\u7684\u4e00\u4e9b\u60f3\u6cd5\uff0c\u65b9\u4fbf\u4ee5\u540e\u53c2\u8003\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6b63\u6587\uff1a<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5f53\u4f60\u5728\u4e00\u53f0\u516c\u7528\u7684\u5f00\u53d1\u673a\u4e0a\u6267\u884c git pull\/push \u547d\u4ee4\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u4e00\u4e0b\u673a\u5668\u7684 git config \u914d\u7f6e\uff0c\u907f\u514d\u4f60\u7684\u8d26\u53f7\u5bc6\u7801\uff08\u88ab\u52a8\uff09\u6cc4\u9732\n# \u6216\u8005\u6362\u79cd\u8bf4\u6cd5\u5c31\u662f\u2014\u2014\u5bf9\u4e8e\u4efb\u4f55\u9700\u8981\u4f60\u8f93\u5165\uff08\u91cd\u8981\uff09\u8d26\u53f7\u5bc6\u7801\u7684\u64cd\u4f5c\u90fd\u9700\u8981\u975e\u5e38\u8c28\u614e\uff08\u80fd\u4e0d\u8f93\u5165\u5c3d\u91cf\u4e0d\u8f93\u5165\uff0c\u64cd\u4f5c\u5b8c\u4e86\u4e4b\u540e\u7acb\u5373\u6539\u5bc6\u7801\uff09\uff0c\u5c24\u5176\u662f\u5728\u73af\u5883\u4e0d\u592a\u53ef\u63a7\u7684\u516c\u5171\u673a\u5668\u4e0a\n\n# Attention: This method saves the credentials in plaintext on your PC's disk. Everyone on your computer can access it, e.g. malicious NPM modules.\n\n# \u7528\u4e0b\u9762\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u5728\u672c\u5730\u78c1\u76d8(\u9ed8\u8ba4\u662f~\/.git-credentials\u6587\u4ef6)\u4e0a\u660e\u6587\u5b58\u50a8\u4f60\u7684\u8d26\u53f7\u5bc6\u7801\uff0c\u4e14\u6587\u4ef6\u80fd\u88ab\u673a\u5668\u4e0a\u7684\u5176\u5b83\u4eba\u8fdb\u884c\u8bfb\u53d6\uff08git\u5168\u5c40\u914d\u7f6e\uff0c\u5bf9\u6240\u6709\u4eba\u751f\u6548\uff09\n$ git config --global credential.helper store\n\n# \u518d\u6267\u884c\u62c9\u53d6\u64cd\u4f5c\uff0c\u4f1a\u63d0\u793a\u4f60\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\uff0c\u5f53\u4f60\u8f93\u5165\u6b63\u786e\u7684\u8d26\u53f7\u5bc6\u7801\u4e4b\u540e\u4f1a\u8fdb\u884c\u4ee3\u7801\u62c9\u53d6\u5e76\u628a\u8d26\u53f7\u5bc6\u7801\u660e\u6587\u5b58\u5728\u672c\u5730\u7684 ~\/.git-credentials \u6587\u4ef6\u4e2d\n$ git pull\n\n# \u5bf9\u4e8e\u4e0d\u60f3\u6bcf\u6b21\u90fd\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\u4f46\u53c8\u4e0d\u60f3\u628a\u8d26\u53f7\u5bc6\u7801\u660e\u6587\u5199\u5165\u6587\u4ef6\u7684\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u7f13\u5b58\u5230\u5185\u5b58\u4e2d\u7684\u547d\u4ee4\n$ git config credential.helper cache\n$ git config --global credential.helper cache\n\n# \u8bbe\u7f6e\u8d26\u53f7\u5bc6\u7801\u7f13\u5b58\u8fc7\u671f\u65f6\u95f4\u4e3a2\u5c0f\u65f6\uff08\u8d85\u8fc7\u8fd9\u6bb5\u65f6\u95f4\u4e4b\u540e\u5c31\u9700\u8981\u91cd\u65b0\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\uff09\n$ git config --global credential.helper 'cache --timeout 7200'\n\n# Remove a matching credential, if any, from the helper\u2019s storage.\uff08\u5b9e\u6d4b\u5e76\u4e0d\u4f1a\u4ece\u6587\u4ef6\u4e2d\u5220\u9664\u5df2\u4fdd\u5b58\u7684\u8d26\u53f7\u5bc6\u7801\uff0c\u53ea\u662f\u8ba9\u5df2\u5b58\u50a8\u7684\u8d26\u53f7\u5bc6\u7801\u4e0d\u80fd\u7528\u4e8e\u8ba4\u8bc1\u2014\u2014\u91cd\u65b0\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\uff0c\u628aerase\u6362\u6210forget\u4e5f\u4e00\u6837\uff09\n$ git config --global credential.helper erase\n\n# \u5220\u9664\u5df2\u4fdd\u5b58\u7684\u8d26\u53f7\u5bc6\u7801\u6587\u4ef6\n$ rm -rf ~\/.git-credentials<\/code><\/pre>\n\n\n\n<p>\u8865\u5145\uff0c\u5728\u672c\u5730\u7684macOS\u7535\u8111\u4e0a\u6d4b\u8bd5\u7684\u65f6\u5019\u53d1\u73b0\u2014\u2014\u5f53\u4f60\u8f93\u5165\u8fc7\u4e00\u6b21\u6b63\u786e\u7684git\u8d26\u53f7\u5bc6\u7801\u4e4b\u540e\uff0c\u5373\u4fbf\u4f60\u624b\u52a8\u5220\u9664\u4e86 ~\/.git-credentials \u6587\u4ef6\uff0c\u5f53\u4f60\u518d\u6267\u884c git clone \u7684\u65f6\u5019\u8fd8\u662f\u53ef\u4ee5\u514d\u5bc6\u62c9\u53d6\uff0c\u5e76\u4e14clone\u5b8c\u4e86\u4e4b\u540e\u53c8\u4f1a\u81ea\u52a8\u751f\u6210 ~\/.git-credentials \u6587\u4ef6\uff08\u5f53\u4f60\u7684\u914d\u7f6e\u8fd8\u662fcredential.helper=store\u65f6\uff09\u3002<\/p>\n\n\n\n<p>\u7a76\u5176\u539f\u56e0\u5728\u4e8emacOS\u4e0a\u7684keychain(\u94a5\u5319\u4e32)\u529f\u80fd\uff0c\u5982\u679c\u4f60\u628a\u4f60\u7684git-config\u914d\u7f6e\u5217\u51fa\u6765\u4f1a\u53d1\u73b0\uff08\u9ed8\u8ba4\uff1f\uff09\u6709\u4e00\u4e2a credential.helper=osxkeychain \u914d\u7f6e\uff0c\u5b83\u4f1a\u628a\u4f60\u8f93\u5165\u8fc7\u7684\u8d26\u53f7\u5bc6\u7801\u5b58\u5165\u94a5\u5319\u4e32\u4e2d\uff0c\u5f53\u4f60\u4e0b\u6b21\u6267\u884cgit\u547d\u4ee4\u9700\u8981\u8ba4\u8bc1\u7684\u65f6\u5019\u5b83\u4f1a\u5e2e\u4f60\u63d0\u4f9b\u8fd9\u4e9b\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>$ git &#8211;version<br>git version 2.37.0 (Apple Git-136)<br>$<br>$ git config &#8211;list | head<br>credential.helper=osxkeychain<br>init.defaultbranch=main<br>\u2026<br>$<\/p>\n\n\n\n<p>\u6253\u5f00\u3010\u94a5\u5319\u4e32\u8bbf\u95ee\u3011\uff0c\u5728\u300c\u9ed8\u8ba4\u94a5\u5319\u4e32-\u767b\u5f55\u300d\u7684\u300c\u5bc6\u7801\u300d\u4e2d\u5373\u53ef\u770b\u5230\u5bf9\u5e94\u540d\u79f0\u7684\u94a5\u5319\u4e32\u7c7b\u578b\u548c\u6dfb\u52a0\u65f6\u95f4\uff0c\u5982\u679c\u4f60\u6709macOS\u7535\u8111\u5bc6\u7801\u7684\u8bdd\uff0c\u8fd8\u53ef\u4ee5\u770b\u5230\u660e\u6587\u5bc6\u7801\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u8fd9\u4e2a\u95ee\u9898\u7b80\u5355\u6765\u8bf4\u662f\u4e00\u4e2a\u56e0\u4e3a\u5458\u5de5\u4e2a\u4eba\u914d\u7f6e\u4e0d\u5f53\u5f15\u8d77\u7684\u5b89\u5168\u98ce\u9669\uff0c\u4e00\u65b9\u9762\u662f\u4f01\u4e1a\u5185\u5458\u5de5\u7684\u5b89\u5168\u610f\u8bc6\u4e0d\u591f\uff08\u52a0\u5f3a\u8fd9\u65b9\u9762\u7684\u5b89\u5168\u5ba3\u5bfc\u548c\u57f9\u8bad\uff09\uff0c\u53e6\u4e00\u65b9\u9762\u4e5f\u9700\u8981\u5b89\u5168\u56e2\u961f\u5177\u5907\u68c0\u6d4b\u548c\u54cd\u5e94\u80fd\u529b\uff08\u4e3b\u52a8\u76d1\u6d4b\uff0c\u505a\u597d\u515c\u5e95\uff09\uff0c\u56e0\u4e3a\u4e0d\u7ba1\u662f\u4ec0\u4e48\u539f\u56e0\u5f15\u8d77\u7684\u8d26\u53f7\u5bc6\u7801\u6cc4\u6f0f\uff0c\u6700\u7ec8\u90fd\u4f1a\u5f52\u7ed3\u5230\u8d26\u53f7\u5b89\u5168\u95ee\u9898\u4e0a\u6765\uff0c\u8981\u60f3\u4e0d\u80cc\u9505\uff0c\u5c31\u8981\u63d0\u524d\u60f3\u597d\u529e\u6cd5\u3002<\/p>\n\n\n\n<p>\u8fd8\u6709\u5c31\u662f\u8fd9\u4e2a\u70b9\u53ef\u80fd\u88ab\u9ed1\u5ba2\u77e5\u6653\u7528\u4e8e\u5feb\u901f\u65e0\u611f\u6536\u96c6\u6709\u6548\u8d26\u53f7\u5bc6\u7801\uff0c\u9700\u8981\u65e9\u505a\u51c6\u5907\u3002git-config\u5168\u5c40\u8bbe\u7f6e\u98ce\u9669\u547d\u4ee4\u68c0\u6d4b\uff0c\u5f15\u7533\u51fa\u7684~\/.git-credentials\u6587\u4ef6\u53ca\u5176\u5185\u5bb9\u76d1\u6d4b\u3002\u8fd8\u6709\u5c31\u662f\u516c\u5171\u673a\u5668\u7684\u5b89\u5168\u6cbb\u7406\u548c\u6743\u8d23\u5212\u5206\u7b49\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u518d\u8865\u5145\u4e00\u4e0b\u6d4b\u8bd5\u9a8c\u8bc1\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e9b\u547d\u4ee4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git help -a | grep credential\n   credential           Retrieve and store user credentials\n   credential-cache     Helper to temporarily store passwords in memory\n   credential-store     Helper to store credentials on disk\n\n$ git config --global credential.helper 'cache --timeout 120'\n\n$ ls -alt ~\/.git*\n$ cat ~\/.gitconfig\n\n$ git clone https:\/\/github.com\/user\/reponame.git\n\n$ ls -alt ~\/.git\n.gitconfig             .git-credential-cache\/ .git-credentials\n\n# git\u7684 credential-cache \u547d\u4ee4\u4f1a\u5728 HOME \u76ee\u5f55\u4e0a\u65b0\u5efa\u4e00\u4e2a .git-credential-cache\/ \u76ee\u5f55\uff0c\u91cc\u9762\u6709\u4e00\u4e2a socket \u6587\u4ef6\n$ ls -alt ~\/.git-credential-cache\/\ntotal 8\ndrwx------  2 ixyzero ixyzero 4096 Nov  2 14:55 .\ndrwx------ 15 ixyzero ixyzero 4096 Nov  2 14:55 ..\nsrwxrwxr-x  1 ixyzero ixyzero    0 Nov  2 14:55 socket\n\n\u6d4b\u8bd5\u53d1\u73b0\u4e0d\u8bba\u662f forget \u8fd8\u662f erase \u90fd\u4e0d\u4f1a\u6e05\u9664 ~\/.git-credentials \u6587\u4ef6\u6216\u8005\u5176\u5df2\u6709\u7684\u5185\u5bb9\u3002\n\n\u4f46\u662f\u8bbe\u7f6e\u4e3a forget\/erase \u4e4b\u540e\uff0c\u5373\u4fbf ~\/.git-credentials \u91cc\u9762\u5b58\u4e86\u8d26\u53f7\u5bc6\u7801\uff0c\u4f46\u662f\u4f9d\u65e7\u9700\u8981\u624b\u52a8\u8f93\u5165\u8d26\u53f7\u5bc6\u7801\u8fdb\u884c clone\/push \u64cd\u4f5c\u3002\u57fa\u672c\u53ef\u4ee5\u8ba4\u4e3a\u53ea\u8981\u4e0d\u662fstore\u5c31\u9700\u8981\u624b\u52a8\u8f93\u5165\u3002\n\ngit: 'credential-erase' is not a git command. See 'git --help'.\n\ngit: 'credential-forget' is not a git command. See 'git --help'.\n\n$ man git-credential-store\n$ man gitcredentials\n\n$ git config credential.helper store\n\n$ git config credential.helper 'store --store=\/path\/to\/git-credentials-file'\n\nThe .git-credentials file is stored in plaintext. Each credential is stored on its own line as a URL like:\nhttps:&#47;&#47;user:pass@example.com<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n\n\n\n<p>How can I save username and password in Git?<br><a href=\"https:\/\/stackoverflow.com\/questions\/35942754\/how-can-i-save-username-and-password-in-git\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/35942754\/how-can-i-save-username-and-password-in-git<\/a><\/p>\n\n\n\n<p>gitcredentials &#8211; Providing usernames and passwords to Git<br><a href=\"https:\/\/git-scm.com\/docs\/gitcredentials\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/git-scm.com\/docs\/gitcredentials<\/a><\/p>\n\n\n\n<p>git-credential-store &#8211; Helper to store credentials on disk<br><a href=\"https:\/\/git-scm.com\/docs\/git-credential-store\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/git-scm.com\/docs\/git-credential-store<\/a><\/p>\n\n\n\n<p>Git push requires username and password<br><a href=\"https:\/\/stackoverflow.com\/questions\/6565357\/git-push-requires-username-and-password\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/stackoverflow.com\/questions\/6565357\/git-push-requires-username-and-password<\/a><\/p>\n\n\n\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u7531\u524d\u6bb5\u65f6\u95f4\u53d1\u73b0\u7684\u4e00\u4e2a\u5185\u90e8case\u5f15\u51fa\u7684\u5bf9git credential\u64cd\u4f5c\u7684\u6d4b\u8bd5\u3002\u8fd9\u91cc\u7b80 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,25],"tags":[468,1867,1868,724,1869],"class_list":["post-5351","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-security","tag-git","tag-git-config","tag-git-credential","tag-724","tag-1869"],"views":1861,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5351","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=5351"}],"version-history":[{"count":1,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions"}],"predecessor-version":[{"id":5352,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/5351\/revisions\/5352"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=5351"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=5351"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=5351"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}