{"id":107,"date":"2024-05-31T20:19:59","date_gmt":"2024-05-31T12:19:59","guid":{"rendered":"http:\/\/www.heartwarming.online:8001\/?p=107"},"modified":"2024-05-31T20:36:51","modified_gmt":"2024-05-31T12:36:51","slug":"git%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/blog.heartwarming.online\/index.php\/2024\/05\/31\/git%e7%9a%84%e5%9f%ba%e6%9c%ac%e4%bd%bf%e7%94%a8\/","title":{"rendered":"Git\u7684\u57fa\u672c\u4f7f\u7528"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">\u4e00\u3001\u5b89\u88c5git<\/h3>\n\n\n\n<p>\u6b64\u5904\u5ffd\u7565<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e8c\u3001\u521b\u5efassh key\u3001\u914d\u7f6egit<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u3001\u8bbe\u7f6e<code>username<\/code>\u548c<code>email<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ git config --global user.name \"John\"\n  $ git config --global user.email \"xxxxxxx@qq.com\"<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>2\u3001\u901a\u8fc7\u7ec8\u7aef\u547d\u4ee4\u521b\u5efa<code>ssh key<\/code><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ ssh-keygen -t rsa -C \"xxxxxxx@qq.com\"<\/code><\/pre>\n\n\n\n<p>\u8fd9\u65f6\u4f1a\u8981\u6c42\u786e\u8ba4\u8def\u5f84\u548c\u8f93\u5165\u5bc6\u7801\uff0c\u6211\u4eec\u8fd9\u4f7f\u7528\u9ed8\u8ba4\u7684\u4e00\u8def\u56de\u8f66\u5c31\u884c\u3002\u6210\u529f\u7684\u8bdd\u4f1a\u5728\u63d0\u793a\u7684\u76ee\u5f55\u4e0b\u751f\u6210<code>.ssh<\/code>\u6587\u4ef6\u5939\uff0c\u8fdb\u53bb\uff0c\u6253\u5f00<code>id_rsa.pub<\/code>\uff0c\u590d\u5236\u91cc\u9762\u7684<code>key<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>3\u3001\u767b\u5f55GitHub\uff0c\u6dfb\u52a0<code>ssh key<\/code>\uff0c\u70b9\u51fb<strong>Settings<\/strong>\u2013<strong>SSH and GPG keys<\/strong>\u2013<strong>New SSH key<\/strong>\uff0c\u5c06\u590d\u5236\u7684key\u7c98\u8d34\u5230<strong>Key<\/strong>\u4e2d\uff0c\u5e76\u914d\u7f6e\u4e00\u4e2a<strong>Title<\/strong>\uff0c\u70b9\u51fb<strong>Add SSH key<\/strong><\/li>\n\n\n\n<li>4\u3001\u8bbe\u7f6egit\u4ee3\u7406\uff08\u79d1\u5b66\u4e0a\u7f51\uff09<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u8bbe\u7f6ess\n  $ git config --global http.proxy 'socks5:\/\/127.0.0.1:1080'\n  $ git config --global https.proxy 'socks5:\/\/127.0.0.1:1080'\n\n  # \u8bbe\u7f6e\u4ee3\u7406\n  $ git config --global https.proxy http:\/\/127.0.0.1:1080\n  $ git config --global https.proxy https:\/\/127.0.0.1:1080\n\n  # \u53d6\u6d88\u4ee3\u7406\n  $ git config --global --unset http.proxy\n  $ git config --global --unset https.proxy<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>5\u3001\u94fe\u63a5\u9a8c\u8bc1<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ ssh -T git@github.com\n\n  # \u51fa\u73b0\u4e0b\u65b9\u63d0\u793a\u8bf4\u660e\u5df2\u7ecf\u94fe\u63a5\u6210\u529f\n  Hi John! You've successfully authenticated, but GitHub does not provide shell access.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e09\u3001\u5ffd\u7565\u4e0d\u60f3\u63d0\u4ea4\u7684\u6587\u4ef6<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0<code>.gitignore<\/code>\u6587\u4ef6\uff0c\u586b\u5199\u8981\u5ffd\u7565\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u4e00\u4e9b\u5e38\u7528\u7684\u5ffd\u7565\u6587\u4ef6\n  .idea\/\n  target\/\n  .gitignore\n\n  # \u7b80\u5355\u7684\u5ffd\u7565\u5339\u914d\u89c4\u5219\n  *.a       # \u5ffd\u7565\u6240\u6709 .a \u7ed3\u5c3e\u7684\u6587\u4ef6\n  \/TODO     # \u4ec5\u4ec5\u5ffd\u7565\u9879\u76ee\u6839\u76ee\u5f55\u4e0b\u7684 TODO \u6587\u4ef6\uff0c\u4e0d\u5305\u62ec subdir\/TODO\n  build\/    # \u5ffd\u7565 build\/ \u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\n  doc\/*.txt # \u4f1a\u5ffd\u7565 doc\/notes.txt \u4f46\u4e0d\u5305\u62ec doc\/server\/arch.txt<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728push\u9879\u76ee\uff0c\u8fd9\u6837\u4e0d\u4f1a\u628a.idea\u6587\u4ef6\u5939\u63d0\u4ea4\u5230\u8fdc\u7aef<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5982\u679c\u5df2\u7ecf\u628a.idea\u6587\u4ef6\u5939\u4e00\u8d77\u63d0\u4ea4\u4e86\uff0c\u9700\u8981\u5c06\u8fdc\u7aef\u63d0\u4ea4\u7684\u6587\u4ef6\u7ed9\u5220\u6389<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u52a0\u4e0a -n \u8fd9\u4e2a\u53c2\u6570\uff0c\u6267\u884c\u547d\u4ee4\u65f6\uff0c\u662f\u4e0d\u4f1a\u5220\u9664\u4efb\u4f55\u6587\u4ef6\uff0c\u800c\u662f\u5c55\u793a\u6b64\u547d\u4ee4\u8981\u5220\u9664\u7684\u6587\u4ef6\u5217\u8868\u9884\u89c8\u3002\n  $ git rm -r -n --cached \u6587\u4ef6\/\u6587\u4ef6\u5939\u540d\u79f0 \n\n  # \u786e\u5b9a\u65e0\u8bef\u540e\u5220\u9664\u6587\u4ef6\n  $ git rm -r --cached .idea<\/code><\/pre>\n\n\n\n<p>\u5220\u9664\u540e\uff0c\u518d\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0 <code>.gitignore<\/code>\u6587\u4ef6\uff0c\u5e76\u8bbe\u7f6e\u8981\u5ffd\u7565\u7684\u6587\u4ef6\u6216\u6587\u4ef6\u5939<\/p>\n\n\n\n<p>\u6700\u540e\u518dpush\uff0c\u628a\u5220\u9664\u548c<code>.gitignore<\/code>\u90fd\u63d0\u4ea4\u8fdc\u7aef\uff0c\u8fdc\u7aef\u7684<code>.idea<\/code>\u4f1a\u88ab\u5220\u9664\u3002\u540c\u65f6\u4ee5\u540e\u518dpush\u65f6\u5c31\u4e0d\u4f1a\u63d0\u4ea4<code>.idea<\/code>\u4e86<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u56db\u3001\u63a8\u9001\u4ee3\u7801\u5230\u8fdc\u7a0b\u4ed3\u5e93<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u3001\u8fdb\u5165\u60f3\u63a8\u9001\u5230\u4ed3\u5e93\u7684\u6587\u4ef6\u5939\u4e2d<\/li>\n\n\n\n<li>2\u3001\u4f9d\u6b21\u6267\u884c\u547d\u4ee4<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ git init   # \u521d\u59cb\u5316\u7248\u672c\u5e93\n\n  $ git add .  # \u6dfb\u52a0\u6587\u4ef6\u5230\u7248\u672c\u5e93\uff08\u53ea\u662f\u6dfb\u52a0\u5230\u7f13\u5b58\u533a\uff09\uff0c.\u4ee3\u8868\u6dfb\u52a0\u6587\u4ef6\u5939\u4e0b\u6240\u6709\u6587\u4ef6 \n\n  $ git commit -m \"first commit\"     # \u628a\u6dfb\u52a0\u7684\u6587\u4ef6\u63d0\u4ea4\u5230\u7248\u672c\u5e93\uff0c\u5e76\u586b\u5199\u63d0\u4ea4\u5907\u6ce8<\/code><\/pre>\n\n\n\n<p>\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u5b8c\u6210\u4e86\u4ee3\u7801\u5e93\u7684\u521d\u59cb\u5316\uff0c\u4f46\u4ee3\u7801\u662f\u5728\u672c\u5730\uff0c\u8fd8\u6ca1\u6709\u63d0\u4ea4\u5230\u8fdc\u7a0b\u670d\u52a1\u5668<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>3\u3001\u628a\u5f53\u524d\u5206\u652fmaster\u63a8\u9001\u5230\u8fdc\u7a0b\u4ee3\u7801\u670d\u52a1\u5668<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  $ git remote add origin https:\/\/github.com\/xxxx\/xxxx.git  # \u628a\u672c\u5730\u5e93\u4e0e\u8fdc\u7a0b\u5e93\u5173\u8054\n\n  $ git push -u origin master   # \u7b2c\u4e00\u6b21\u63a8\u9001\u65f6,\u5148\u5c06\u672c\u5730\u5206\u652f\u4e0e\u8fdc\u7a0b\u540c\u540d\u5206\u652f\u76f8\u5173\u8054\n\n  $ git push origin master       # \u7b2c\u4e00\u6b21\u63a8\u9001\u540e\uff0c\u76f4\u63a5\u4f7f\u7528\u8be5\u547d\u4ee4\u5373\u53ef\u63a8\u9001\u4fee\u6539<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>4\u3001push \u547d\u4ee4\u8be6\u89e3<\/li>\n\n\n\n<li>\u672c\u5730\u5f53\u524d\u5206\u652f \u63a8\u9001\u5230 \u8fdc\u7a0b\u6307\u5b9a\u5206\u652f\u4e0a <code>$ git push origin &lt;\u672c\u5730\u5206\u652f\u540d>:&lt;\u8fdc\u7a0b\u5206\u652f\u540d><\/code><\/li>\n\n\n\n<li>\u672c\u5730\u5f53\u524d\u5206\u652f \u63a8\u9001\u5230 \u4e0e\u672c\u5730\u5f53\u524d\u5206\u652f\u540c\u540d\u7684\u8fdc\u7a0b\u5206\u652f\u4e0a <code>$ git push origin &lt;\u672c\u5730\u5206\u652f\u540d><\/code><\/li>\n\n\n\n<li>\u672c\u5730\u5f53\u524d\u5206\u652f \u63a8\u9001\u5230 \u4e0e\u672c\u5730\u5f53\u524d\u5206\u652f\u540c\u540d\u7684\u8fdc\u7a0b\u5206\u652f\u4e0a\uff08\u9700\u5148\u5173\u8054\u8fdc\u7a0b\u5206\u652f\uff0c\u65b9\u6cd5\u89c1\u7b2c\u4e00\u6b21\u63a8\u9001\uff09 <code>$ git push<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e94\u3001\u4ece\u8fdc\u7a0b\u4ed3\u5e93\u62c9\u53d6\u4ee3\u7801<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4e00\u822c\u9879\u76ee\u4e2d\uff0c\u9700\u8981\u5148\u5c06\u6700\u65b0\u4ee3\u7801\u62c9\u53d6\u5230\u672c\u5730\uff0c\u518dpush\u4e0a\u53bb<\/li>\n\n\n\n<li>pull \u547d\u4ee4\u8be6\u89e3<\/li>\n\n\n\n<li>\u8fdc\u7a0b\u6307\u5b9a\u5206\u652f \u62c9\u53d6\u5230 \u672c\u5730\u6307\u5b9a\u5206\u652f\u4e0a <code>git pull origin &lt;\u8fdc\u7a0b\u5206\u652f\u540d>:&lt;\u672c\u5730\u5206\u652f\u540d><\/code><\/li>\n\n\n\n<li>\u8fdc\u7a0b\u6307\u5b9a\u5206\u652f \u62c9\u53d6\u5230 \u672c\u5730\u5f53\u524d\u5206\u652f\u4e0a <code>git pull origin &lt;\u8fdc\u7a0b\u5206\u652f\u540d><\/code><\/li>\n\n\n\n<li>\u5c06\u4e0e\u672c\u5730\u5f53\u524d\u5206\u652f\u540c\u540d\u7684\u8fdc\u7a0b\u5206\u652f \u62c9\u53d6\u5230 \u672c\u5730\u5f53\u524d\u5206\u652f\u4e0a\uff08\u9700\u5148\u5173\u8054\u8fdc\u7a0b\u5206\u652f\uff0c\u65b9\u6cd5\u89c1\u7b2c\u4e00\u6b21\u63a8\u9001\uff09 <code>git pull<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u516d\u3001Git\u6062\u590d\u4e4b\u524d\u7248\u672c\u7684\u65b9\u6cd5<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>1\u3001git reset\uff08\u56de\u9000\uff09\uff1a\u4fee\u6539HEAD\u7684\u4f4d\u7f6e\uff0c\u5373\u5c06HEAD\u6307\u5411\u7684\u4f4d\u7f6e\u6539\u53d8\u4e3a\u4e4b\u524d\u5b58\u5728\u7684\u67d0\u4e2a\u7248\u672c\u3002\u5982\u4ece\u7248\u672c\u4e09\u56de\u9000\u5230\u7248\u672c\u4e00\uff0c\u90a3\u4e48\u7248\u672c\u4e8c\u3001\u7248\u672c\u4e09\u90fd\u4e0d\u89c1\u4e86<\/li>\n\n\n\n<li>\u67e5\u770b\u7248\u672c\u53f7 <code># \u663e\u793a\u6240\u6709\u63d0\u4ea4\u8fc7\u7684\u7248\u672c\u4fe1\u606f $ git log # \u67e5\u770b\u6240\u6709\u5206\u652f\u7684\u6240\u6709\u64cd\u4f5c\u8bb0\u5f55\uff08\u5305\u62ec\u5df2\u7ecf\u88ab\u5220\u9664\u7684 commit \u8bb0\u5f55\u548c reset \u7684\u64cd\u4f5c\uff09 $ git reflog<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528<code>git reset --hard \u76ee\u6807\u7248\u672c\u53f7<\/code>\u547d\u4ee4\u5c06\u7248\u672c\u56de\u9000\u5230\u76ee\u6807\u7248\u672c <code>$ git reset --hard f42aa0e<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528<code>git push -f<\/code>\u63d0\u4ea4\u66f4\u6539 \u6b64\u65f6\u5982\u679c\u7528\u201cgit push\u201d\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a\u6211\u4eec\u672c\u5730\u5e93HEAD\u6307\u5411\u7684\u7248\u672c\u6bd4\u8fdc\u7a0b\u5e93\u7684\u8981\u65e7 <code>hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.<\/code> \u6240\u4ee5\u6211\u4eec\u5f3a\u5236\u63a8\u4e0a\u53bb\u5c31\u53ef\u4ee5\u4e86 <code>$ git push -f<\/code><\/li>\n\n\n\n<li>2\u3001git revert\uff08\u53cd\u505a\uff09\uff1a\u53cd\u505a\u67d0\u4e00\u4e2a\u7248\u672c\uff0c\u4ee5\u8fbe\u5230\u64a4\u9500\u8be5\u7248\u672c\u7684\u4fee\u6539\u7684\u76ee\u7684\u3002\u5982\u53d1\u73b0\u7248\u672c\u4e8c\u6709bug\uff0c\u60f3\u8981\u64a4\u9500\u7248\u672c\u4e8c\uff0c\u4f46\u53c8\u4e0d\u60f3\u5f71\u54cd\u64a4\u9500\u7248\u672c\u4e09\u7684\u63d0\u4ea4\uff0c\u5c31\u53ef\u4ee5\u7528 git revert \u547d\u4ee4\u6765\u53cd\u505a\u7248\u672c\u4e8c\uff0c\u751f\u6210\u65b0\u7684\u7248\u672c\u56db\u3002\u8fd9\u4e2a\u7248\u672c\u56db\u91cc\u4f1a\u4fdd\u7559\u7248\u672c\u4e09\u7684\u4e1c\u897f\uff0c\u4f46\u64a4\u9500\u4e86\u7248\u672c\u4e8c\u7684\u4e1c\u897f<\/li>\n\n\n\n<li>\u67e5\u770b\u7248\u672c\u53f7 <code># \u663e\u793a\u6240\u6709\u63d0\u4ea4\u8fc7\u7684\u7248\u672c\u4fe1\u606f $ git log # \u67e5\u770b\u6240\u6709\u5206\u652f\u7684\u6240\u6709\u64cd\u4f5c\u8bb0\u5f55\uff08\u5305\u62ec\u5df2\u7ecf\u88ab\u5220\u9664\u7684 commit \u8bb0\u5f55\u548c reset \u7684\u64cd\u4f5c\uff09 $ git reflog<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528<code>git revert -n \u7248\u672c\u53f7<\/code>\u53cd\u505a <code>$ git revert -n 8b89621019c9adc6fc4d242cd41daeb13aeb9861 # \u53cd\u505a\u7248\u672c\u53f7\u4e3a8b89621\u7684\u7248\u672c # \u8fd9\u91cc\u53ef\u80fd\u4f1a\u51fa\u73b0\u51b2\u7a81\uff0c\u90a3\u4e48\u9700\u8981\u624b\u52a8\u4fee\u6539\u51b2\u7a81\u7684\u6587\u4ef6\u3002\u800c\u4e14\u8981git add \u6587\u4ef6\u540d<\/code><\/li>\n\n\n\n<li>\u4f7f\u7528<code>git commit -m \u7248\u672c\u540d<\/code>\u63d0\u4ea4 <code>$ git commit -m \"revert add text.txt\"<\/code><\/li>\n\n\n\n<li>\u63a8\u4e0a\u8fdc\u7a0b\u5e93 <code>$ git push<\/code><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5b89\u88c5git \u6b64\u5904\u5ffd\u7565 \u4e8c\u3001\u521b\u5efassh key\u3001\u914d\u7f6egit \u8fd9\u65f6\u4f1a\u8981\u6c42\u786e\u8ba4\u8def\u5f84\u548c\u8f93\u5165\u5bc6\u7801\uff0c\u6211\u4eec\u8fd9\u4f7f\u7528\u9ed8\u8ba4\u7684\u4e00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":108,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[17,4],"tags":[22,21],"class_list":["post-107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-front","category-blog","tag-git","tag-tools"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/blog.heartwarming.online\/wp-content\/uploads\/2024\/05\/pexels-photo-11035539.jpeg","_links":{"self":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/107","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/comments?post=107"}],"version-history":[{"count":1,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":109,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/posts\/107\/revisions\/109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/media\/108"}],"wp:attachment":[{"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.heartwarming.online\/index.php\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}