±à¼ÍƼö: |
±¾ÎÄÖ÷Òª½éÉÜ
Git ´æ´¢Ä¿Â¼£¬Í¨¹ý git init ´´½¨Ò»¸ö¿ÕµÄ Git ²Ö¿â£¬ÒÔ¼°Git
ÊÇÈçºÎ´æ´¢µÄ£¬Ï£Íû¶ÔÄúµÄѧϰÓÐËù°ïÖú¡£
±¾ÎÄÀ´×ÔÓÚCSDN£¬ÓÉ»ðÁú¹ûÈí¼þAlice±à¼¡¢ÍƼö¡£ |
|
ǰÑÔ
×ö¼¼ÊõÒ»¶¨ÒªÖªÆäȻ֪ÆäËùÒÔÈ»£¬Òâ˼¾ÍÊÇ£ºÖªµÀËüÊÇÕâÑùµÄ£¬¸üÖªµÀËüΪʲôÊÇÕâÑùµÄ¡£ÎÒÖ÷Ҫͨ¹ý4¿éÄÚÈÝÀ´¼òµ¥½éÉÜ
Git ÔÀíÊÇʲôÑùµÄ¡£Õâ4¿éÄÚÈÝÈçÏ£º
Git ´æ´¢Ä¿Â¼½á¹¹½éÉÜ
Git ÊÇÈçºÎ´æ´¢µÄ
Git µÄ¶ÔÏó
GitÒýÓÃ
µ±È» Git ÔÀí²»½ö½ö°üº¬ÕâЩ£¬ÏëÒª¸üÉîÈëÁ˽âÇë²é¿´¹Ù·½½Ì³Ì https://git-scm.com/book/zh/v2/¡£
Git ´æ´¢Ä¿Â¼½á¹¹½éÉÜ
Ê×ÏÈÎÒÃÇÏÈ´Ó Git ´æ´¢Ä¿Â¼ËµÆð£¬Í¨¹ý git init ´´½¨Ò»¸ö¿ÕµÄ Git ²Ö¿â£¬¾ßÌå²Ù×÷ÈçÏÂͼ£º
´´½¨Íê³Éºó½øÈë .git Ŀ¼£¬ÈçÏÂͼËùʾ£º
hooks ¸ÃĿ¼ÓÃÓÚÅäÖà ¿Í»§¶ËÖ´Ìá½»²Ù×÷ÓÃÓÚ´¥·¢·þÎñ¶ËµÄ½Å±¾ÅäÖã¬Ò»°ãÓÃÓÚ×Ô¶¯»¯²¿ÊðʹÓÃ
info ¸ÃĿ¼ÓÃÓÚÅäÖÃһЩ²»Ï£Íû±» Git ¹Ü¿ØµÄÎļþ¡£
objects ¸ÃĿ¼ÓÃÓÚ´æ´¢ËùÓÐÊý¾Ý¶ÔÏóÄÚÈÝ£¬ÕâЩÊý¾ÝÄÚÈÝÀàÐÍÓÐ commit tree blob
tag
refs ¸ÃĿ¼ÓÃÓÚ´æ´¢ Git ±¾µØÒÔ¼°Ô¶³Ì·ÖÖ§µÄÒýÓ㬵±È»»¹ÓÐÒ»ÖÖÌØÊâµÄÒýÓñêÇ©ÒýÓÃ
config ¸ÃÎļþ°üº¬ÏîÄ¿ÌØÓеÄÅäÖÃÑ¡Ï²¢ÇÒ¸ÃÅäÖýö¶Ô¸Ã Git ²Ö¿âÓÐЧ
description ¸ÃÎļþ½ö¹© GitWeb ³ÌÐòʹÓÃ
HEAD ¸ÃÎļþ±íʾµ±Ç° Git ²Ö¿â´¦ÓÚÄĸö·ÖÖ§
index ¸ÃÎļþ±£´æÔÝ´æÇøÐÅÏ¢ £¨¿Õ²Ö¿âϸÃÎļþ²»»áÏÔʾһµ©Ö´ÐÐ git add ²Ù×÷¸ÃÎļþ¾Í»á³öÏÖ£©
ͨ¹ý git config --local ²é¿´ config ÎļþµÄ±ä»¯
ÎÒÃÇͨ¹ý git config --local ÅäÖýö¶ÔÓÚ gitLearn
ÏîÄ¿ÓÐЧ£¬Óû§ÃûºÍÓÊÏäÅäÖÃÈçÏÂͼËùʾ£º
²é¿´ config Îļþ£¬»á·¢ÏÖ¸ÃÎļþÐÂÔöÓû§ÐÅÏ¢ÅäÖá£
Git ÊÇÈçºÎ´æ´¢µÄ
Git ÊÇÒ»¸öÄÚÈÝѰַÎļþϵͳ ÆäºËÐIJ¿·ÖÊÇÒ»¸ö¼òµ¥µÄ¼üÖµ¶ÔÊý¾Ý¿â£¨key-value data store£©¡£
Äã¿ÉÒÔÏò¸ÃÊý¾Ý¿â²åÈëÈÎÒâÀàÐ͵ÄÄÚÈÝ£¬Ëü»á·µ»ØÒ»¸ö 40 λ×Ö·û´®¼ü£¬Í¨¹ý¸Ã 40 λ×Ö·û´®¼ü¿ÉÒÔÔÚÈÎÒâʱ¿ÌÔٴμìË÷£¨retrieve£©¸ÃÄÚÈÝ¡£
ʲôÊÇÄÚÈÝѰַ£¿
ÿ´ÎÎÒÃǽøÐÐÌá½»»áͨ¹ý SHA-1 Ëã·¨Éú³ÉÒ»¸ö³¤¶ÈΪ 40 ¸ö×Ö·ûµÄУÑéºÍ£¨checksum hash£©£¨Ò²¾ÍÊÇÎÒÃǵÄ
key£©È»ºó¸ù¾ÝУÑéºÍÈ¥»ñÈ¡ÎÒÃÇÎļþµÄÄÚÈÝ¡£ÕâÖÖͨ¹ýΨһ±êʶµÄ key£¨Ò²¿ÉÒÔÀí½âΪÄÚÈݵĵØÖ·£©È¥»ñÈ¡ÎÒÃǵÄÄÚÈݵIJÙ×÷¾ÍÊÇÄÚÈÝѰַ¡£
Git µÄ¶ÔÏó
ÔÚ Git ÖÐÓÐËÄÖÖ¶ÔÏó·Ö±ðΪ£º
blob ÊǾßÌåµÄÎļþ¶ÔÏó
tree ÊÇij¸öʱ¿ÌÌύĿ¼µÄÄÚÈÝ
commit Ö´ÐÐÒ»´Î commit ¾Í»á²úÉúÒ»¸ö commit ¶ÔÏó
tag ¿ÉÒÔÀí½â³É commit µÄ±ðÃû£¬Ò»¸ö tag ¶ÔÓ¦Ò»¸ö commit
Á˽â Git µÄ¶ÔÏóÐèҪʹÓÃÈçÏÂÃüÁî½øÐв鿴£º
git cat-file -p ¶ÔÏó hash Öµ ²é¿´¶ÔÏóµÄÄÚÈÝ
git cat-file -t ¶ÔÏó hashÖµ ²é¿´¶ÔÏóµÄÀàÐÍ
git ls-files --stage ²é¿´ index ÎļþÄÚÈÝ
git hash-object ²é¿´ÎļþµÄ hash Öµ
²é¿´ commit tree blob Èý¸ö¶ÔÏóÑÝʾ
ÎÒÃÇ´´½¨Ò»¸ö first.txt Îļþ£¬²¢½«ÆäÌá½»µ½ÔÝ´æÇøÖС£
½øÈë .git Îļþ¼ÐÏ»ᷢÏÖÐÂÔöÁËÒ»¸ö index Îļþ¡£
ÎÒÃÇ¿ÉÒÔͨ¹ý git ls-files --stage ²é¿´ index
ÎļþµÄÄÚÈÝ¡£
½øÈë objects Ŀ¼·¢ÏÖ 9c Îļþ¼ÐÃû³Æ+ÎļþÃû³Æ ºÍ index
ÎļþÖеÄÒ»¶Î×Ö·û´®ÄÚÈÝÏàͬ¡£
ÎÒÃÇͨ¹ý git cat-file -t 9c59e24b8393179a5d712de4f990178df5734d99
ÎҲ鿴¸Ã±íʾ¶ÔÏóÀàÐÍ ÈçÏÂͼËùʾ±íʾ¸Ã±êʶ¶ÔÏóÀàÐÍÊÇ blob¡£
Ö´ÐÐ git commit -m ½« first.txt ÎļþÌá½»µ½±¾µØ²Ö¿âÖС£
Ö´ÐÐ git log ²é¿´ÎÒÃǵÄÌá½»¼Ç¼¡£
ÈçÏÂͼËùʾÎÒÃÇͨ¹ý git cat-file -p commitId
²é¿´ÎÒÃÇÌá½»µÄÄÚÈÝ¡£ÈçÏÂͼËùʾ£ºÎÒÃÇ×îÐÂÒ»´ÎÌá½»°üº¬ÁËÒ»¸ö 59b06 ¿ªÍ·µÄ tree ¶ÔÏó¡£
ÔÚ ./git/objects Ŀ¼ÖпÉÒÔÕÒµ½ÎÒÃǶÔÓ¦µÄÎļþ¡£
ÎÒÃÇͨ¹ý git cat-file -p tree¶ÔÏó¹þÏ£Öµ£¬²é¿´¸Ã
tree ¶ÔÏóµÄÄÚÈÝ ¡£ÈçÏÂͼËùʾÏÔʾ¾ÍÊÇÎÒÃÇ git add²ÎÉúµÄ blob¶ÔÏó¡£
ÔÚͨ¹ý git cat-file -p blob¶ÔÏó¹þÏ£Öµ£¬²é¿´ÎÒÃÇ
blob¶ÔÏóÄÚÈÝ£¬ÈçÏÂͼËùʾ blob¶ÔÏó ÄÚÈݾÍÊÇÎÒÃÇ first.txt ÎļþµÄÄÚÈÝ¡£
ÎÒÃǽ« first.txt ÎļþÌá½»µ½±¾µØ²Ö¿â »á²úÉúÒ»¸ö commit Ò»¸ö tree ºÍÒ»¸ö
blob ¶ÔÏó¡£
tag ¶ÔÏóÔÀíÑÝʾ
Ê×ÏÈÎÒÃÇͨ¹ý git log ²é¿´×îеÄÌá½»ÊÇ add a.txt
×¢Ê굀 commit ÈçÏÂͼËùʾ¡£
ͨ¹ý git tag -a v1.1 -m ¡®add a.txt tag¡¯
Ϊ¸Ã commit ´´½¨Ò»¸ö¸½×¢±êÇ©¡£
ÔÚÎÒÃÇµÄ .git/refs/tags/ Ŀ¼Ï»áÐÂÔö v1.1 Îļþ¡£
v1.1
ÄÚÈÝÈçÏ£º
¿´µ½Õâ¸öÄã¿Ï¶¨Ïëµ½ÁËÕâÊÇÒ»¸ö Git ¶ÔÏó£¬ÎÒÃÇͨ¹ý git cat-file
-t ²é¿´Õâ¸ö¹þÏ£Öµ¶ÔÏóÀàÐÍ¡£ÈçÏÂͼËùʾËüÊÇÒ»¸ö tag¡£
È»ºóͨ¹ý git cat-file -p ²é¿´ËüµÄÄÚÈÝ£¬ÈçÏÂͼËùʾ£¬¸Ãtag°üº¬ÁËcommit
¶ÔÏóºÍ ±ê×¢µÄÐÅÏ¢¡£
Git ÒýÓÃ
ʲôÊÇ Git µÄÒýÓã¿
ÕâÀïµÄÒýÓÃÎÒÃÇ¿ÉÒÔÀí½â³ÉÒ»¸öÊéÇ©£¬ÄãÔÚÔĶÁÒ»±¾ÊéµÄʱºò¿ÉÒÔΪÄã¶Áµ½µÄ²¿·Ö´òÉϱêÇ©¡£È»ºóÄã¿ÉÒÔͨ¹ýÕâ¸öÊéÇ©¿ìËÙÕÒµ½Äã֮ǰÔĶÁµÄλÖá£ÔÚ
Git ÖÐÎÒÃÇ ´´½¨Ð嵀 Commit »òÕß´´½¨·ÖÖ§¶¼»á½øÐÐÒ»´Î´ò±êÇ©µÄ²Ù×÷¡£ÎÒÃǸ÷¸ö²»Í¬ commit
Ö®¼äµÄÇл»»ò·ÖÖ§µÄÇл»Æäʵ¾ÍÊDZêÇ©µÄÇл»¡£
Git ÒýÓõÄÈýÖÖÀàÐÍ
ÔÚ Git ÖÐÓÐÈýÖÖÀàÐ͵ĵÄÒýÓà ·Ö±ðÊÇ:
HEAD ÒýÓãºÓÃÀ´ÎªÎÒÃǵı¾µØ²Ö¿â´òÉϱêǩʹÓá£
Tag ÒýÓãºÓÃÀ´ÎªÎÒÃÇµÄ Git ²Ö¿â tag ±êǩʹÓá£
Ô¶³ÌÒýÓãºÓÃÀ´ÎªÎÒÃǵÄÔ¶³Ì²Ö¿â´ò±êǩʹÓá£
HEAD ÒýÓÃ
HEAD ÒýÓÃÔÀí ÎҵĸöÈËÀí½âÊÇÒ»¸ö Head Í·Ö¸Õë+µ±Ç°·ÖÖ§ Ö¸Ïòµ±Ç°×îÐÂÌá½»µÄ commit
¶ÔÏó¡£ÎÒÃÇÒ²¿ÉÒÔͨ¹ý git reset --hard À´Çл»ÎÒÃÇcommit ¼Ç¼£¬Çл»ºóµÄ commit
ÒÔǰµÄ commit ¼Ç¼¾ÍûÓÐÁË£¬²»¹ýÎÒÃÇ¿ÉÒÔͨ¹ý git reflog ²éѯ²Ù×÷¼Ç¼½«ÒÔǰ commit
Õһء£
ΪÁ˸üºÃµÄÀí½â HEAD£¬ÒýÓý¨Òé´ó¼Ò·ÃÎÊ http://onlywei.github.io/explain-git-with-d3/#zen
Ö´ÐÐ Git Ïà¹ØµÄ²Ù×÷À´Àí½âʲôÊÇ HEADÒýÓá£
ÈçÏÂͼËùʾ£¬ÎÒÃÇµÄ HeadÍ·Ö¸Õë·ÖÖ§ Ö¸Ïò master·ÖÖ§ ͬʱָÏò×îеÄ
commit e137e¡£
ÎÒÃÇÖ´ÐÐÒ»´Î commit HeadÍ·Ö¸Õë·ÖÖ§ + master·ÖÖ§
¾ÍÖ¸ÏòµÄÊÇ×îеÄÌá½» 896ee ÉÏ¡£
ͨ¹ý git branch b1 ´´½¨ÐµķÖÖ§Ãû³ÆÎªb1£¬ÈçÏÂͼËùʾ£¬ÎÒÃÇ·¢ÏÖ
Head Í·Ö¸Õ뻹ÊÇÖ¸Ïò master ¡£
µ±Ö´ÐÐ git checkout b1 µÄʱºò£¬ÈçÏÂͼËùʾ£¬´Ëʱ Head
Ö¸ÕëÖ¸Ïò·ÖÖ§ b1¡£
ÎÒÃÇÔÚ·ÖÖ§ b1 ÉÏÖ´ÐÐ commit Head Ö¸ÕëÖ¸ÏòÁËÔڸ÷Ö֧ϵÄ×îÐÂÌá½»
3c7acb¡£
µ±Ö´ÐÐ git checkout master ÊÇ Head Ö¸ÕëÖ¸ÏòÁË
master ÉÏ×îÐ嵀 commit 896ee¡£
µ±Ö´ÐÐ git merge b1 µÄʱºò£¬»á½« b1·ÖÖ§×îРcommit
ºÏ²¢µ½ master£¬ÎÒÃÇ HeadÖ¸Õë + master ͬʱָÏò×îÐ嵀 commit 3c7ba
ÉÏ¡£
ͨ¹ýÉÏÃæµÄ¶¯Ì¬ÑÝʾ£¬Õâ»Ø¶Ô Head Ó¦ÓÃÈçºÎÇл» commit Çл»·ÖÖ§ÓÐÁËÒ»¶¨µÄÀí½â¡£
½ÓÏÂÀ´ÎÒÃÇÀ´Í¨¹ý²é¿´ .git Ŀ¼À´½éÉÜ Head Ö¸ÕëÊÇÈçºÎʵÏֵġ£½øÈë .git Ŀ¼Äã»á·¢ÏÖÓÐÒ»¸ö
HEAD Îļþ£¬ËüµÄÄÚÈÝÊÇ ref: refs/heads/master£¬ÈçÏÂͼËùʾ¡£
refs/heads/master ÊǾßÌåÎļþ·¾¶£¬ÎÒÃDzé refs/heads Ŀ¼Ï嵀 master
ÎļþÄÚÈÝ£¬ÈçÏÂͼËùʾ£¬master ÎļþÄÚÈݾÍÊÇÎÒÃÇ×îÐ嵀 commit¡£
ÎÒÃÇÖ´ÐÐ git chekcout demoBranch3¡£
´ËʱÎÒÃÇµÄ HEAD ÎļþÄÚÈݱä³ÉÁË ref: refs/heads/demoBranch3¡£
²é¿´ refs/heads/ ·¾¶Ï嵀 demoBranch3 Îļþ£¬ÈçÏÂͼËùʾ demoBranch3
Îļþ¼Ç¼ÊÇdemoBranch3 ·ÖÖ§ÏÂ×îÐ嵀 commit¡£
±êÇ©ÒýÓÃ
±êÇ©ÒýÓÃÊÇÒ»¸öÌØÊâµÄÒýÓã¬Ëü²»Ïñ HEAD ÒýÓÿÉÒÔͨ¹ý git checkout À´¸ü¸ÄÒýÓõÄÖ¸ÕëÖ¸Ïò¡£±êÇ©ÒýÓò»»áÒÆ¶¯ËüÓÀÔ¶»áÖ¸ÏòÒ»¸ö
Commit ¶ÔÏó¡£
±êÇ©ÒýÓõÄÑÝʾÇë²Î¿´ Tag ¶ÔÏóÔÀíÑÝʾ²¿·Ö¡£
Ô¶³ÌÒýÓÃ
Ô¶³ÌÒýÓÃÊÇÖ»¶ÁµÄÎÒÃDz»ÄÜͨ¹ýÇл»µ½Ô¶³ÌÒýÓÃÖ´ÐÐ commit ½«Ìá½»¸üе½Ô¶³Ì²Ö¿âÖÐ
ÎÒÃÇÔÚ±¾µØ·ÖÖ§Ö´ÐÐ push ²Ù×÷£¬Git ¶¼»á°ïÎÒÃǼǼ push
µ½Ô¶³Ì·ÖÖ§µÄ×îРcomit£¬µ±Ö´ÐÐpush µÄʱºò£¬Èç¹û·¢ÏÖÔ¶³Ì·ÖÖ§×îРcommit ºÍÎÒÃDZ¾µØ²Ö¿â¼Ç¼×îºóÒ»´Î
push µÄ commit ²»Í¬»á±¨ Note about fast-forwards Òì³££¬ÈçÏÂͼËùʾ£º
³öÏÖ Note about fast-forwards ÎÒÃÇÐèÒªÖ´ÐÐ git pull ½«Ô¶³Ì×îеÄ
commit ÀÈ¡ÏÂÀ´ È»ºóÔÙÖ´ÐÐ git push ²Ù×÷ »òÕß Ö±½ÓÖ´ÐÐ git puhs -f¡£ÕâÀïÇ¿µ÷һϲ»½¨ÒéÖ´ÐÐ
git puhs -f ²Ù×÷ÒòΪ»áÇ¿ÖÆ½«±¾µØµÄÀúÊ·¼Ç¼¸²¸Çµ½Ô¶³Ì²Ö¿âµÄÀúÊ·¼Ç¼¡£
ͨ¹ý²é¿´ .git Ŀ¼Àí½âÔ¶³ÌÒýÓÃ
ÔÚ .git/refs/remotes/origin Îļþ¼ÐÖÐ HEAD
ÎļþÄÚÈݱíʾÄÚÈݱíʾԶ³Ì·ÖÖ§´¦ÓÚÄĸö·ÖÖ§¡£
ÔÚ .git/refs/remotes/origin Îļþ¼ÐÖÐ master
ÎļþÄÚÈÝ£¬±íʾ×îºóÒ»´Î push µ½Ô¶³Ì²Ö¿âµÄcommit£¬ÈçÏÂͼËùʾ£º
ÔÚ .git/refs/remotes/origin Îļþ¼ÐÖÐ demoBranch1 Îļþ±íʾ£¬×îºóÒ»´Î
push µ½ demoBranch1 ·ÖÖ§Ìá½»µÄ commitId¡£
|