{"id":21055,"date":"2022-10-24T17:01:22","date_gmt":"2022-10-24T09:01:22","guid":{"rendered":"http:\/\/lrxjmw.cn\/?p=21055"},"modified":"2022-10-24T17:01:38","modified_gmt":"2022-10-24T09:01:38","slug":"git-command","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/git-command.html","title":{"rendered":"\u4f60\u5e94\u8be5\u77e5\u9053\u7684\u57fa\u7840 Git \u547d\u4ee4"},"content":{"rendered":"
\u6211\u4eec\u5728\u65e9\u5148\u4e00\u7bc7\u6587\u7ae0\u4e2d\u5df2\u7ecf\u5feb\u901f\u4ecb\u7ecd\u8fc7 Vi \u901f\u67e5\u8868\u4e86\u3002\u5728\u8fd9\u7bc7\u6587\u7ae0\u91cc\uff0c\u6211\u4eec\u5c06\u4f1a\u4ecb\u7ecd\u5f00\u59cb\u4f7f\u7528 Git \u65f6\u6240\u9700\u8981\u7684\u57fa\u7840\u547d\u4ee4\u3002<\/p>\n
Git \u662f\u4e00\u4e2a\u5206\u5e03\u5f0f\u7248\u672c\u63a7\u5236\u7cfb\u7edf\uff0c\u5b83\u88ab\u7528\u5728\u5927\u91cf\u5f00\u6e90\u9879\u76ee\u4e2d\u3002\u5b83\u662f\u5728 2005 \u5e74\u7531 Linux \u521b\u59cb\u4eba Linus Torvalds \u5199\u5c31\u7684\u3002\u8fd9\u4e2a\u7a0b\u5e8f\u5141\u8bb8\u975e\u7ebf\u6027\u7684\u9879\u76ee\u5f00\u53d1\uff0c\u5e76\u4e14\u80fd\u591f\u901a\u8fc7\u5b58\u50a8\u5728\u672c\u5730\u670d\u52a1\u5668\u9ad8\u6548\u5904\u7406\u5927\u91cf\u6570\u636e\uff1b\u5728\u8fd9\u4e2a\u6559\u7a0b\u91cc\uff0c\u6211\u4eec\u5c06\u8981\u548c Git \u6109\u5feb\u73a9\u800d\u5e76\u5b66\u4e60\u5982\u4f55\u5f00\u59cb\u4f7f\u7528\u5b83\u3002<\/p>\n
\u6211\u5728\u8fd9\u4e2a\u6559\u7a0b\u91cc\u4f7f\u7528 Ubuntu\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u4f60\u9009\u62e9\u7684\u4efb\u4f55\u53d1\u884c\u7248\u3002\u9664\u4e86\u5b89\u88c5\u4ee5\u5916\uff0c\u5269\u4e0b\u7684\u6240\u6709\u547d\u4ee4\u5728\u4efb\u4f55 Linux \u53d1\u884c\u7248\u4e0a\u90fd\u662f\u4e00\u6837\u7684\u3002<\/p>\n
\u8981\u5b89\u88c5 git \u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n
sudo apt-get install git-core<\/pre>\n\u5728\u5b83\u5b8c\u6210\u4e0b\u8f7d\u4e4b\u540e\uff0c\u4f60\u5c31\u5b89\u88c5\u597d\u4e86 Git \u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528\u4e86\u3002<\/p>\n
\u8bbe\u7f6e Git<\/strong><\/div>\n\u5728 Git \u5b89\u88c5\u4e4b\u540e\uff0c\u4e0d\u8bba\u662f\u4ece apt-get \u8fd8\u662f\u4ece\u6e90\u7801\u5b89\u88c5\uff0c\u4f60\u9700\u8981\u5c06\u4f60\u7684\u7528\u6237\u540d\u548c\u90ae\u7bb1\u5730\u5740\u590d\u5236\u5230 gitconfig \u6587\u4ef6\u3002\u4f60\u53ef\u4ee5\u8bbf\u95ee ~\/.gitconfig \u8fd9\u4e2a\u6587\u4ef6\u3002<\/p>\n
\u5168\u65b0\u5b89\u88c5 Git \u4e4b\u540e\u6253\u5f00\u5b83\u4f1a\u662f\u5b8c\u5168\u7a7a\u767d\u7684\uff1a<\/p>\n
sudo vim ~\/.gitconfig<\/pre>\n\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6dfb\u52a0\u6240\u9700\u7684\u4fe1\u606f\u3002\u5c06\u201cuser\u201d\u66ff\u6362\u6210\u4f60\u7684\u7528\u6237\u540d\uff0c\u201cuser@example.com\u201d\u66ff\u6362\u6210\u4f60\u7684\u90ae\u7bb1\u3002<\/p>\n
git config --global user.name \"User\"\r\ngit config --global user.email user@example.com\r\n<\/pre>\n\u7136\u540e\u4f60\u5c31\u5b8c\u6210\u8bbe\u7f6e\u4e86\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u5f00\u59cb Git\u3002<\/p>\n
\u4ed3\u5e93<\/strong><\/div>\n\u521b\u5efa\u4e00\u4e2a\u65b0\u76ee\u5f55\uff0c\u6253\u5f00\u5b83\u5e76\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n
git init<\/pre>\n<\/p>\n
\u8fd9\u4e2a\u547d\u4ee4\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 Git \u4ed3\u5e93\uff08repository\uff09\u3002\u4f60\u7684\u672c\u5730\u4ed3\u5e93\u7531\u4e09\u4e2a Git \u7ef4\u62a4\u7684\u201c\u6811\u201d\u7ec4\u6210\u3002<\/p>\n
\u7b2c\u4e00\u4e2a\u662f\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\uff08Working Directory\uff09\uff0c\u4fdd\u5b58\u5b9e\u9645\u7684\u6587\u4ef6\u3002\u7b2c\u4e8c\u4e2a\u662f\u7d22\u5f15\uff0c\u5b9e\u9645\u4e0a\u626e\u6f14\u7684\u662f\u6682\u5b58\u533a\uff08staging area\uff09\uff0c\u6700\u540e\u4e00\u4e2a\u662f HEAD\uff0c\u5b83\u6307\u5411\u4f60\u6700\u540e\u4e00\u4e2a commit \u63d0\u4ea4\uff0c\u4f7f\u7528 git clone \/path\/to\/repository \u7b7e\u51fa\u4f60\u7684\u4ed3\u5e93\uff08\u4ece\u4f60\u521a\u521b\u5efa\u7684\u4ed3\u5e93\u6216\u670d\u52a1\u5668\u4e0a\u5df2\u5b58\u5728\u7684\u4ed3\u5e93\uff09\u3002<\/p>\n
\u6dfb\u52a0\u6587\u4ef6\u5e76\u63d0\u4ea4<\/strong><\/div>\n\u4f60\u53ef\u4ee5\u7528\u4ee5\u4e0b\u547d\u4ee4\u6dfb\u52a0\u6539\u52a8\uff1a<\/p>\n
git add <filename><\/pre>\n\u8fd9\u4f1a\u6dfb\u52a0\u4e00\u4e2a\u65b0\u6587\u4ef6\u5230\u6682\u5b58\u533a\u4ee5\u63d0\u4ea4\u3002\u5982\u679c\u4f60\u60f3\u6dfb\u52a0\u6bcf\u4e2a\u65b0\u6587\u4ef6\uff0c\u8f93\u5165\uff1a<\/p>\n
git add --all\r\n<\/pre>\n\u6dfb\u52a0\u6587\u4ef6\u4e4b\u540e\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5\u72b6\u6001\uff1a<\/p>\n
git status<\/pre>\n<\/p>\n
\u6b63\u5982\u4f60\u770b\u5230\u7684\uff0c\u90a3\u91cc\u5df2\u7ecf\u6709\u4e00\u4e9b\u53d8\u5316\u4f46\u8fd8\u6ca1\u6709\u63d0\u4ea4\u3002\u73b0\u5728\u4f60\u9700\u8981\u63d0\u4ea4\u8fd9\u4e9b\u53d8\u5316\uff0c\u4f7f\u7528\uff1a<\/p>\n
git commit -m \"\u63d0\u4ea4\u4fe1\u606f\"<\/pre>\n<\/p>\n
\u4f60\u4e5f\u53ef\u4ee5\u8fd9\u4e48\u505a\uff08\u9996\u9009\uff09\uff1a<\/p>\n
git commit -a<\/pre>\n\u7136\u540e\u5199\u4e0b\u4f60\u7684\u63d0\u4ea4\u4fe1\u606f\u3002\u73b0\u5728\u4f60\u7684\u6587\u4ef6\u63d0\u4ea4\u5230\u4e86 HEAD\uff0c\u4f46\u8fd8\u4e0d\u5728\u4f60\u7684\u8fdc\u7a0b\u4ed3\u5e93\u4e2d\u3002<\/p>\n
\u63a8\u9001\u4f60\u7684\u6539\u52a8<\/strong><\/div>\n\u4f60\u7684\u6539\u52a8\u5728\u4f60\u672c\u5730\u5de5\u4f5c\u526f\u672c\u7684 HEAD \u4e2d\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u4ece\u4e00\u4e2a\u5df2\u5b58\u5728\u7684\u4ed3\u5e93\u514b\u9686\uff0c\u6216\u60f3\u5c06\u4f60\u7684\u4ed3\u5e93\u8fde\u63a5\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u4f60\u9700\u8981\u5148\u6dfb\u52a0\u5b83\uff1a<\/p>\n
git remote add origin <\u670d\u52a1\u5668\u5730\u5740><\/pre>\n\u73b0\u5728\u4f60\u53ef\u4ee5\u5c06\u6539\u52a8\u63a8\u9001\u5230\u6307\u5b9a\u7684\u8fdc\u7a0b\u670d\u52a1\u5668\u3002\u8981\u5c06\u6539\u52a8\u53d1\u9001\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u8fd0\u884c\uff1a<\/p>\n
git push -u origin master<\/pre>\n\u5206\u652f<\/strong><\/div>\n\u5206\u652f\u7528\u4e8e\u5f00\u53d1\u7279\u6027\uff0c\u5206\u652f\u4e4b\u95f4\u662f\u4e92\u76f8\u72ec\u7acb\u7684\u3002\u4e3b\u5206\u652f master \u662f\u4f60\u521b\u5efa\u4e00\u4e2a\u4ed3\u5e93\u65f6\u7684\u201c\u9ed8\u8ba4\u201d\u5206\u652f\u3002\u4f7f\u7528\u5176\u5b83\u5206\u652f\u7528\u4e8e\u5f00\u53d1\uff0c\u5728\u5b8c\u6210\u65f6\u5c06\u5b83\u5408\u5e76\u56de\u4e3b\u5206\u652f\u3002<\/p>\n
\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a\u201cmybranch\u201d\u7684\u5206\u652f\u5e76\u5207\u6362\u5230\u5b83\u4e4b\u4e0a\uff1a<\/p>\n
git checkout -b mybranch<\/pre>\n<\/p>\n
\u4f60\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u547d\u4ee4\u5207\u6362\u56de\u4e3b\u5206\u652f\uff1a<\/p>\n
\u5982\u679c\u4f60\u60f3\u5220\u9664\u8fd9\u4e2a\u5206\u652f\uff0c\u6267\u884c\uff1a<\/p>\n
git branch -d mybranch<\/pre>\n<\/p>\n
\u9664\u975e\u4f60\u5c06\u5206\u652f\u63a8\u9001\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u4e0a\uff0c\u5426\u5219\u8be5\u5206\u652f\u5bf9\u5176\u4ed6\u4eba\u662f\u4e0d\u53ef\u7528\u7684\uff0c\u6240\u4ee5\u53ea\u9700\u628a\u5b83\u63a8\u9001\u4e0a\u53bb\uff1a<\/p>\n
git push origin <\u5206\u652f\u540d><\/pre>\n\u66f4\u65b0\u548c\u5408\u5e76<\/strong><\/div>\n\u8981\u5c06\u4f60\u672c\u5730\u4ed3\u5e93\u66f4\u65b0\u5230\u6700\u65b0\u7684\u63d0\u4ea4\u4e0a\uff0c\u8fd0\u884c\uff1a<\/p>\n
git pull<\/pre>\n\u5728\u4f60\u7684\u5de5\u4f5c\u76ee\u5f55\u83b7\u53d6\u5e76\u5408\u5e76\u8fdc\u7a0b\u53d8\u52a8\u3002\u8981\u5408\u5e76\u5176\u5b83\u5206\u652f\u5230\u4f60\u7684\u6d3b\u52a8\u5206\u652f\uff08\u5982 master\uff09\uff0c\u4f7f\u7528\uff1a<\/p>\n
git merge <\u5206\u652f><\/pre>\n\u5728\u8fd9\u4e24\u79cd\u60c5\u51b5\u4e0b\uff0cgit \u4f1a\u5c1d\u8bd5\u81ea\u52a8\u5408\u5e76\uff08auto-merge\uff09\u6539\u52a8\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u8fd9\u4e0d\u603b\u662f\u53ef\u80fd\u7684\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u51b2\u7a81\u3002\u4f60\u9700\u8981\u901a\u8fc7\u7f16\u8f91 git \u6240\u663e\u793a\u7684\u6587\u4ef6\uff0c\u624b\u52a8\u5408\u5e76\u90a3\u4e9b\u51b2\u7a81\u3002\u6539\u52a8\u4e4b\u540e\uff0c\u4f60\u9700\u8981\u7528\u4ee5\u4e0b\u547d\u4ee4\u5c06\u5b83\u4eec\u6807\u8bb0\u4e3a\u5df2\u5408\u5e76\uff1a<\/p>\n
git add <\u6587\u4ef6\u540d><\/pre>\n\u5728\u5408\u5e76\u6539\u52a8\u4e4b\u524d\uff0c\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u9884\u89c8\uff1a<\/p>\n
git diff <\u6e90\u5206\u652f> <\u76ee\u6807\u5206\u652f><\/pre>\nGit \u65e5\u5fd7<\/strong><\/div>\n\u4f60\u53ef\u4ee5\u8fd9\u4e48\u67e5\u770b\u4ed3\u5e93\u5386\u53f2\uff1a<\/p>\n
git log<\/pre>\n\u8981\u4ee5\u6bcf\u4e2a\u63d0\u4ea4\u4e00\u884c\u7684\u6837\u5f0f\u67e5\u770b\u65e5\u5fd7\uff0c\u4f60\u53ef\u4ee5\u7528\uff1a<\/p>\n
git log --pretty=oneline<\/pre>\n\u6216\u8005\u4e5f\u8bb8\u4f60\u60f3\u8981\u770b\u4e00\u4e2a\u6240\u6709\u5206\u652f\u7684 ASCII \u827a\u672f\u6811\uff0c\u5e26\u6709\u6807\u7b7e\u548c\u5206\u652f\u540d\uff1a<\/p>\n
git log --graph --oneline --decorate --all<\/pre>\n\u5982\u679c\u4f60\u53ea\u60f3\u770b\u54ea\u4e9b\u6587\u4ef6\u6539\u52a8\u8fc7\uff1a<\/p>\n
git log --name-status<\/pre>\n\u5728\u8fd9\u6574\u4e2a\u8fc7\u7a0b\u4e2d\u5982\u679c\u4f60\u9700\u8981\u4efb\u4f55\u5e2e\u52a9\uff0c\u4f60\u53ef\u4ee5\u7528 git --help\u3002<\/p>\n
Git \u68d2\u4e0d\u68d2\uff1f\uff01\u795d\u8d3a\u4f60\u4f60\u5df2\u7ecf\u4f1a Git \u57fa\u7840\u4e86\u3002\u5982\u679c\u4f60\u613f\u610f\u7684\u8bdd\uff0c\u4f60\u53ef\u4ee5\u4ece\u4e0b\u9762\u8fd9\u4e2a\u94fe\u63a5\u4e0b\u8f7d\u8fd9\u4e9b\u57fa\u7840 Git \u547d\u4ee4\u4f5c\u4e3a\u5feb\u901f\u53c2\u8003\uff1a<\/p>\n
<\/i>\u4e0b\u8f7d Git \u901f\u67e5\u8868<\/a><\/p>\n
\n\u539f\u6587\u6765\u81ea\uff1ahttps:\/\/linux.cn\/article-7552-1.html<\/a><\/p>\n