{"id":1878,"date":"2012-06-10T16:21:00","date_gmt":"2012-06-10T08:21:00","guid":{"rendered":""},"modified":"2017-08-29T11:04:29","modified_gmt":"2017-08-29T03:04:29","slug":"git%e5%9f%ba%e6%9c%ac%e7%94%a8%e6%b3%95","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/1878.html","title":{"rendered":"Git\u57fa\u672c\u7528\u6cd5"},"content":{"rendered":"<p>\u8bbe\u7f6e\u7528\u6237\u540d\u4e0e\u90ae\u7bb1<br \/>\ngit config &#8211;global user.name &#8220;My Name&#8221;<br \/>\ngit config &#8211;global user.email &#8220;my@email.com&#8221;<\/p>\n<p>\u4ece\u5df2\u6709\u7684git\u5e93\u4e2d\u63d0\u53d6\u4ee3\u7801<br \/>\ngit clone git@server:app.git myrepo<\/p>\n<p>\u6bcf\u6b21\u66f4\u6539\u4ee3\u7801\u7684\u64cd\u4f5c<\/p>\n<p>\u66f4\u65b0\u672c\u5730\u4ee3\u7801\u5230\u6700\u65b0\u7248\u672c\uff08\u9700\u8981merge\u624d\u80fd\u5408\u5230\u672c\u5730\u4ee3\u7801\u4e2d\uff09<br \/>\ngit fetch<\/p>\n<p>\u5408\u5e76\u66f4\u65b0\u540e\u7684\u4ee3\u7801\u5230\u672c\u5730<br \/>\ngit merge<\/p>\n<p>\u66f4\u65b0\u4ee3\u7801\u65b9\u5f0f\u7684\u53e6\u4e00\u79cd\u65b9\u6cd5(git pull\u662fgit fetch\u548cgit merge\u547d\u4ee4\u7684\u4e00\u4e2a\u7ec4\u5408)<br \/>\ngit pull<\/p>\n<p>\u4fee\u6539\u4ee3\u7801\u540e\uff0c\u67e5\u770b\u5df2\u4fee\u6539\u7684\u5185\u5bb9<br \/>\ngit diff &#8211;cached<\/p>\n<p>\u5c06\u65b0\u589e\u52a0\u6587\u4ef6\u52a0\u5165\u5230git\u4e2d<br \/>\ngit add file1 file2 file3<\/p>\n<p>\u4ecegit\u4e2d\u5220\u9664\u6587\u4ef6<br \/>\ngit rm file1<br \/>\ngit rm -r dir1<\/p>\n<p>\u63d0\u4ea4\u4fee\u6539<br \/>\ngit commit -m &#8216;this is memo&#8217;<\/p>\n<p>\u5982\u679c\u60f3\u7701\u6389\u63d0\u4ea4\u4e4b\u524d\u7684 git add \u547d\u4ee4\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528<br \/>\ngit commit -a -m &#8216;this is memo&#8217;<br \/>\ncommit\u548ccommit -a\u7684\u533a\u522b, commit -a\u76f8\u5f53\u4e8e\uff1a<br \/>\n\u7b2c\u4e00\u6b65\uff1a\u81ea\u52a8\u5730add\u6240\u6709\u6539\u52a8\u7684\u4ee3\u7801\uff0c\u4f7f\u5f97\u6240\u6709\u7684\u5f00\u53d1\u4ee3\u7801\u90fd\u5217\u4e8eindex file\u4e2d<br \/>\n\u7b2c\u4e8c\u6b65\uff1a\u81ea\u52a8\u5730\u5220\u9664\u90a3\u4e9b\u5728index file\u4e2d\u4f46\u4e0d\u5728\u5de5\u4f5c\u6811\u4e2d\u7684\u6587\u4ef6<br \/>\n\u7b2c\u4e09\u6b65\uff1a\u6267\u884ccommit\u547d\u4ee4\u6765\u63d0\u4ea4<\/p>\n<p>\u63d0\u4ea4\u6240\u6709\u4fee\u6539\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\uff0c\u8fd9\u6837\uff0c\u5176\u5b83\u56e2\u961f\u6210\u5458\u624d\u80fd\u66f4\u65b0\u5230\u8fd9\u4e9b\u4fee\u6539<br \/>\ngit push<\/p>\n<p>\u5176\u5b83\u5e38\u7528\u547d\u4ee4<\/p>\n<p>\u663e\u793acommit\u65e5\u5fd7<br \/>\ngit log<\/p>\n<p>\u4e0d\u4ec5\u663e\u793acommit\u65e5\u5fd7\uff0c\u800c\u4e14\u540c\u65f6\u663e\u793a\u6bcf\u6b21commit\u7684\u4ee3\u7801\u6539\u53d8\u3002<br \/>\ngit log -p<\/p>\n<p>\u56de\u6eda\u4ee3\u7801\uff1a<br \/>\ngit revert HEAD<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5revert\u66f4\u65e9\u7684commit\uff0c\u4f8b\u5982\uff1a<br \/>\ngit revert HEAD^<\/p>\n<p>\u5c06branchname\u5206\u652f\u5408\u5e76\u5230\u5f53\u524d\u5206\u652f\u4e2d\u3002(\u5982\u679c\u5408\u5e76\u53d1\u751f\u51b2\u7a81\uff0c\u9700\u8981\u81ea\u5df1\u89e3\u51b3\u51b2\u7a81)<br \/>\ngit merge branchname<\/p>\n<p>\u521b\u5efa git \u4ed3\u5e93<\/p>\n<p>\u521d\u59cb\u5316 git \u4ed3\u5e93<br \/>\nmkdir project # \u521b\u5efa\u9879\u76ee\u76ee\u5f55<br \/>\ncd project # \u8fdb\u5165\u5230\u9879\u76ee\u76ee\u5f55<br \/>\ngit init # \u521d\u59cb\u5316 git \u4ed3\u5e93\u3002\u6b64\u547d\u4ee4\u4f1a\u5728\u5f53\u524d\u76ee\u5f55\u65b0\u5efa\u4e00\u4e2a .git \u76ee\u5f55\uff0c\u7528\u4e8e\u5b58\u50a8 git \u4ed3\u5e93\u7684\u76f8\u5173\u4fe1\u606f<\/p>\n<p>\u521d\u59cb\u5316\u63d0\u4ea4<br \/>\ntouch README<br \/>\ngit add . # \u5c06\u5f53\u524d\u76ee\u5f55\u6dfb\u52a0\u5230 git \u4ed3\u5e93\u4e2d\uff0c \u4f7f\u7528 git add -A \u5219\u662f\u6dfb\u52a0\u6240\u6709\u6539\u52a8\u7684\u6587\u6863<br \/>\ngit commit -m &#8220;Initial commit&#8221;<br \/>\ngit remote add origin git@github.com:lugir\/repo.git # \u8bbe\u7f6e\u4ed3\u5e93<\/p>\n<p>\u4fee\u8865\u63d0\u4ea4\uff08\u4fee\u8865\u6700\u8fd1\u4e00\u6b21\u7684\u63d0\u4ea4\u800c\u4e0d\u521b\u5efa\u65b0\u7684\u63d0\u4ea4\uff09<br \/>\ngit commit &#8211;amend -m &#8220;commit message.&#8221;<\/p>\n<p>\u63d0\u4ea4\u51b2\u7a81\u65f6\u53ef\u4ee5\u5408\u5e76\u540e\u518d\u63a8\u9001<br \/>\ngit pull # \u83b7\u53d6\u8fdc\u7a0b\u7248\u672c\u5e93\u63d0\u4ea4\u4e0e\u672c\u5730\u63d0\u4ea4\u8fdb\u884c\u5408\u5e76<br \/>\ngit push # \u63d0\u4ea4<\/p>\n<p>\u4f7f\u7528\u522b\u4eba\u7684\u4ed3\u5e93<br \/>\ngit clone http:\/\/path\/to\/git.git # clone \u7684\u5185\u5bb9\u4f1a\u653e\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u65b0\u76ee\u5f55<\/p>\n<p>\u5c06\u4ee3\u7801\u4ece\u672c\u5730\u56de\u4f20\u5230\u4ed3\u5e93<br \/>\ngit push -u origin master<\/p>\n<p>\u4f7f\u7528 git status \u67e5\u770b\u6587\u4ef6\u72b6\u6001<br \/>\ngit status<\/p>\n<p>\u67e5\u770b\u63d0\u4ea4\u65e5\u5fd7<br \/>\ngit log # \u67e5\u770b\u63d0\u4ea4\u4fe1\u606f<br \/>\ngit log &#8211;pretty=oneline # \u4ee5\u6574\u6d01\u7684\u5355\u884c\u5f62\u5f0f\u663e\u793a\u63d0\u4ea4\u4fe1\u606f<\/p>\n<p>Git \u5206\u652f<\/p>\n<p>git branch # \u67e5\u770b\u5206\u652f<br \/>\ngit branch 6.x-1.x # \u6dfb\u52a0\u5206\u652f 6.x-1.x<br \/>\ngit branch checkout master # \u5207\u6362\u5230\u4e3b\u5206\u652f<br \/>\ngit branch -d 6.x-1.x # \u5220\u9664\u5206\u652f 6.x-1.x<br \/>\ngit push origin :branchname # \u5220\u9664\u8fdc\u7aef\u5206\u652f<\/p>\n<p>Git \u6807\u7b7e<\/p>\n<p>git tag # \u67e5\u770b\u5206\u652f<br \/>\ngit tag 6.x-1.0 # \u6dfb\u52a0\u6807\u7b7e 6.x-1.0<br \/>\ngit show 6.x-1.0 # \u67e5\u770b\u6807\u7b7e 6.x-1.0 \u7684\u4fe1\u606f<br \/>\ngit tag -a 6.x-1.0 965e066 # \u4e3a\u4e4b\u524d\u63d0\u4ea4\u7684\u4fe1\u606f\u8bb0\u5f55 965e066 \u52a0\u4e0a\u6807\u7b7e<br \/>\ngit push &#8211;tags # \u63d0\u4ea4\u65f6\u5e26\u4e0a\u6807\u7b7e\u4fe1\u606f<br \/>\ngit push origin :\/refs\/tags\/tagname # \u5220\u9664\u8fdc\u7aef\u6807\u7b7e<\/p>\n<p>\u4ece git \u4ed3\u5e93\u4e2d\u5bfc\u51fa\u9879\u76ee<br \/>\ngit archive &#8211;format tar &#8211;output \/path\/to\/file.tar master # \u5c06 master \u4ee5 tar \u683c\u5f0f\u6253\u5305\u5230\u6307\u5b9a\u6587\u4ef6<br \/>\ngit archive &#8211;format zip &#8211;output \/full\/path\/to\/zipfile.zip master # \u4ee5 zip \u683c\u5f0f\u6253\u5305 master<br \/>\ngit archive master | tar -x -C \/somewhere\/else # \u5c06master\u6587\u4ef6\u5bfc\u51fa\u53e6\u5916\u4e00\u4e2a\u6587\u4ef6\u5939<\/p>\n<p>\u4f7f\u7528 Git \u7684\u4e00\u4e9b\u57fa\u672c\u5b88\u5219\uff1a<\/p>\n<p>\u5f53\u8981commit\/\u63d0\u4ea4patch\u65f6\uff1a<br \/>\n\u4f7f\u7528 git diff &#8211;check \u68c0\u67e5\u884c\u5c3e\u6709\u6ca1\u6709\u591a\u4f59\u7684\u7a7a\u767d<br \/>\n\u6bcf\u4e2a commit \u53ea\u6539\u4e00\u4ef6\u4e8b\u60c5\u3002\u5982\u679c\u4e00\u4e2a\u6587\u6863\u6709\u591a\u4e2a\u53d8\u66f4\uff0c\u4f7f\u7528 git add &#8211;patch \u53ea\u9009\u62e9\u6587\u6863\u4e2d\u7684\u90e8\u5206\u53d8\u66f4\u8fdb\u5165 stage<br \/>\n\u5199\u6e05\u695a commit message<\/p>\n<p>\u7f13\u5b58\u672c\u5730\u66f4\u6539\uff0c\u9632\u6b62\u5207\u6362\u5230\u5176\u5b83\u5206\u652f\u8986\u76d6\u672c\u5730\u4fee\u6539\uff1a<br \/>\ngit stash<br \/>\ngit pull<br \/>\ngit stash pop<\/p>\n<p>\u66f4\u65b0\u5b50\u6a21\u5757\uff1a<br \/>\ngit submodule foreach git pull origin master<\/p>\n<p>\u521b\u5efa\u65b0\u5206\u652f\uff1a<br \/>\ngit checkout -b kyle-dev<\/p>\n<p>\u5f3a\u5236\u7528\u8fdc\u7a0b\u7684\u8986\u76d6\u672c\u5730\u4fee\u6539\uff1a<br \/>\ngit fetch &#8211;all<br \/>\ngit reset &#8211;hard origin\/master<\/p>\n<p>\u7b7e\u51fa\u8fdc\u7a0b\u7684test\u5206\u652f\uff1a<br \/>\ngit pull<br \/>\ngit checkout test<\/p>\n<p>\u64a4\u6d88\u4e0a\u4e00\u6b21\u7684commit\uff1a<br \/>\ngit reset &#8211;soft HEAD^<\/p>\n<p>\u5c06\u672c\u5730\u7684tag\u63d0\u4ea4\u5230\u8fdc\u7a0b\uff1a<br \/>\ngit tag -a v1.0 -m &#8216;version 1.0&#8217;<br \/>\ngit push\/pull &#8211;tags origin master<\/p>\n<p>\u5220\u9664\u8fdc\u7a0btag:<br \/>\ngit tag -d v2.1.3<br \/>\ngit push origin :refs\/tags\/v2.1.3<br \/>\ngit push origin &#8211;delete tag<br \/>\ngit push origin &#8211;delete<\/p>\n<p>\u514b\u9686\u672c\u5730\u540d\u79f0\u4e3aold\u7684tag \u4e3a new\uff1a<br \/>\ngit tag new old<\/p>\n<p>git \u5ffd\u7565 submodule\u9879\u76ee\u7684\u66f4\u6539\uff0c\u5728.gitmodules\u6587\u4ef6\u4e2d\u6dfb\u52a0 ignore = all (https:\/\/www.kernel.org\/pub\/software\/scm\/git\/docs\/gitmodules.html) \u4f8b\u5982\uff1a<br \/>\n[submodule &#8220;bundle\/fugitive&#8221;]<br \/>\npath = bundle\/fugitive<br \/>\nurl = git:\/\/github.com\/tpope\/vim-fugitive.git<br \/>\nignore = all<\/p>\n<p>git \u56de\u9000\u4e94\u4e2a\u63d0\u4ea4\uff1a<br \/>\ngit reset &#8211;hard HEAD~5<br \/>\n\u6216\u5177\u4f53\u67d0\u4e2a\u7248\u672c\uff1a<br \/>\ngit reset &#8211;hard commit_sha<\/p>\n<p>\u642dgitlab\u7684\u65f6\u5019\uff0cssh keys\u8ba4\u8bc1\u4e0d\u4e86\uff0c\u539f\u56e0\u4e3a .ssh \u76ee\u5f55 \u53ca .ssh\/authorized_keys \u6587\u4ef6\u7684\u6743\u9650\u8bbe\u7f6e\u4e0d\u6b63\u786e\uff0c\u5206\u522b\u4e3a 775 \u548c 644\u5c31\u884c\u4e86\u3002<br \/>\n&gt;&gt;&gt;\/home\/git\/gitlab$ sudo -u git -H bundle exec rake gitlab:shell:setup RAILS_ENV=production<\/p>\n<p>\u6dfb\u52a0\u5b50\u6a21\u5757\uff1a<br \/>\ngit submodule add git@git.github.com:server.git deps\/server<br \/>\n\u914d\u7f6e\u6587\u4ef6\u4e3a .gitmodules<\/p>\n<p>\u66f4\u6539\u5b50\u6a21\u5757\u7684\u8fdc\u7a0b\u5730\u5740\uff1a<br \/>\n\u4fee\u6539.gitmodules\u6587\u4ef6\u4e2d\u7684\u5730\u5740\uff0c\u7136\u540e \uff1agit submodule sync<\/p>\n<p>\u5f3a\u5236\u7528\u8fdc\u7a0b\u5206\u652f\u7684\u4ee3\u7801\uff0c\u5728\u5408\u5e76\u4ee3\u7801\u6709\u5927\u91cf\u51b2\u7a81\u7684\u60c5\u51b5\uff1a<br \/>\n# fetch from the default remote, origin<br \/>\ngit fetch<br \/>\n# reset your current branch (master) to origin&#8217;s master<br \/>\ngit reset &#8211;hard origin\/master<\/p>\n<p>\u56de\u6eda\u5355\u4e2a\u6587\u4ef6\uff1a<br \/>\ngit checkout v1.2.3 &#8212; filename # tag v1.2.3<br \/>\ngit checkout stable &#8212; filename # stable branch<br \/>\ngit checkout origin\/master &#8212; filename # upstream master<br \/>\ngit checkout HEAD &#8212; filename # the version from the most recent commit<br \/>\ngit checkout HEAD^ &#8212; filename # the version before the most recent commit<br \/>\n\u5176\u4e2d&#8211;\u7528\u6765\u533a\u5206\u6587\u4ef6\u540d\u548ctag\u540d\u5b57\u7b49\uff0c\u9632\u6b62\u51fa\u73b0\u6587\u4ef6\u540d\u4e0e\u5206\u652f\u540d\u3001tag\u540d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5c06\u4e00\u4e2a\u5206\u652f\u7684\u67d0\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u53e6\u4e00\u4e2a\u5206\u652f\u53bb\uff1a<br \/>\ngit checkout otherbranch myfile.txt<\/p>\n<p>\u53ea\u5408\u5e76\u67d0\u4e2a\u5206\u652f\u7684\u67d0\u4e2a\u63d0\u4ea4\uff1a<br \/>\ngit cherry-pick 38361a68<\/p>\n<p>\u5f3a\u5236\u56de\u6eda\u8fdc\u7a0b\u5230\u65e7\u7684\u7248\u672c\uff1a<br \/>\ngit push -f origin $old_commit_id:master<br \/>\nWhat will this do? It will push the $old_commit_id commit to origin as the new head of origin\u2019s master branch.<\/p>\n<p>\u67e5\u770b\u5f53\u524dcommit\u7684hash\u503c<br \/>\ngit rev-parse HEAD<br \/>\ngit rev-parse &#8211;short HEAD<\/p>\n<p>git \u5220\u9664\u9519\u8bef\u63d0\u4ea4\u7684commit \u65b9\u6cd5:<br \/>\ngit reset &#8211;hard &lt;commit_id&gt;<br \/>\ngit push origin HEAD &#8211;force<\/p>\n<p>\u7ed9git\u8bbe\u7f6esocket\u4ee3\u7406\uff1agit config &#8211;global https.proxy &#8216;socks5:\/\/127.0.0.1:1080&#8217;\uff0c\u53d6\u6d88\u4ee3\u7406\uff1agit config &#8211;global &#8211;unset https.proxy\uff0c\u8fd8\u6709\u4e00\u4e2a\u5168\u5c40\u914d\u7f6e\u00a0http.proxy \u4e5f\u53ef\u4ee5\u540c\u6837\u914d\u7f6e<\/p>\n<p>$ git config &#8211;global -l<br \/>\nhttps.proxy=socks5:\/\/127.0.0.1:1080<br \/>\nhttp.proxy=socks5:\/\/127.0.0.1:1080<br \/>\ncore.gitproxy=git-proxy<br \/>\nsocks.proxy=localhost:1080<\/p>\n<p>git\u7981\u6b62push\u64cd\u4f5c\uff0c\u53ea\u5141\u8bb8pull\uff1a<br \/>\n$ git remote set-url &#8211;push origin no-pushing<br \/>\n$ git push<br \/>\nfatal: &#8216;no-pushing&#8217; does not appear to be a git repository<br \/>\nfatal: The remote end hung up unexpectedly<\/p>\n<p>\u73b0\u5728\u4f4d\u4e8e test\u5206\u652f\uff0c\u60f3\u8ba9test\u5206\u652f\u4e0b\u9762\u7684\u6587\u4ef6 \/path\/file1.php \u5f3a\u5236\u4f7f\u7528 master\u5206\u652f\u7684\uff1a<br \/>\ngit show master:path\/file1.php &gt; path\/file1.php<\/p>\n<p>\u6279\u91cf\u5220\u9664\u672c\u5730\u7684 untrack files\uff1a<br \/>\ngit clean -id<\/p>\n<p>\u8981\u67e5\u627e\u65e5\u671f\u4e4b\u95f4\u7684git commit\u8bb0\u5f55\uff0c\u6bd4\u5982\u60f3\u67e5 2017-08-04\u548c05\u53f7\u8fd9\u4e24\u5929\u7684\u8bb0\u5f55\uff0c\u6709\u4e24\u79cd\u65b9\u6cd5\uff1a<br \/>\ngit log &#8211;format=format:&#8221;%ai %aE %s&#8221; | grep &#8220;2017-08-0&#8221;<br \/>\ngit log &#8211;until 2017-08-06 &#8211;since 2017-08-04<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bbe\u7f6e\u7528\u6237\u540d\u4e0e\u90ae\u7bb1 git config &#8211;global user.name &#8220;My N [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1878","post","type-post","status-publish","format-standard","hentry","category-my_notes"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=1878"}],"version-history":[{"count":17,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"predecessor-version":[{"id":6322,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions\/6322"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}