×ܽáGitɾ³ýÔ¶³ÌcommitµÄÒªÁì
gitÊÇÒ»¸ö¿ªÔ´µÄÂþÑÜʽ°æ±¾¿ØÖÆϵͳ£¬Îª¿ª·¢ÕßÌṩÁËһЩÖ÷ÒªµÄÏÂÁîºÍ¹¤¾ß£¬ÔÚÈí¼þ¿ª·¢ºÍÏàÖúÀú³ÌÖм«ÎªÀû±ã¡£ÆäÖУ¬git½»Á÷µÄÒªº¦ÔÚÓÚÌá½»£¨commit£©µÄ¿´·¨£¬ÓÉÓÚÔÚÌá½»µÄÀú³ÌÖУ¬¿ÉÒÔÈÃÍŶÓÏàʶµ½´úÂëµÄÐÞ¸ÄÇéÐÎÒÔ¼°Ð¡ÎÒ˽ÈËТ˳¡£
È»¶ø£¬ÓÐʱ¼äÎÒÃÇÐèҪɾ³ýÒѾÌá½»µÄcommit£¬ºÃ±ÈÌá½»ÁËһЩÎÞÓõĴúÂë»òÕßÃô¸ÐÐÅÏ¢£¬ÐèÒª´ÓÔ¶³Ì´úÂë¿âÖÐɾµô¡£Õâʱ¼ä£¬ÎÒÃÇÐèҪѧ»áÔõÑùÔÚGitÖÐɾ³ýÒѾÌá½»µÄcommit£¬ÒÔ¼°ÔõÑù½«É¾³ýºóµÄ¸ü¸ÄÍÆË͵½Ô¶³Ì´úÂë¿âÖС£ÏÂÃ潫ÏÈÈÝÔÚGitɾ³ýÔ¶³ÌcommitµÄÒªÁì¡£
Ò»¡¢Git commitµÄʵÖÊ
ÔÚGitÖУ¬Ã¿¸öcommit¶¼ÓÐÒ»¸öΨһµÄSHA-1¹þÏ£Öµ×÷Ϊ±êʶ¡£Õâ¸ö¹þÏ£ÖµÊÇÓÉGit»ùÓÚcommitµÄÄÚÈÝÅÌËãµÃ³öµÄ£¬ÈôÊÇÓÐí§ÒâÒ»¸ö×Ö·û±¬·¢¸Ä±ä£¬Õâ¸ö¹þÏ£Öµ¾Í»áËæÖ®¸Ä±ä¡£ÕâÒ²ÊÇΪʲôһ¸öcommitÖ»Äܱ»É¾³ý£¬¶øÎÞ·¨±»Ð޸ġ£
¶þ¡¢Ê¹ÓÃGit resetÏÂÁîɾ³ýcommit
Ҫɾ³ýÒ»¸öcommit£¬Ê×ÏÈÐèҪʹÓÃGit resetÏÂÁHEADÖ¸ÕëÖ¸ÏòÐèҪɾ³ýcommitµÄÇ°Ò»Ìõ¡£Õâ¸öHEADÖ¸ÕëÖ¸ÏòµÄÊÇÍâµØ´úÂë¿â×îеÄcommit£¬resetÏÂÁî¿ÉÒÔÓÃÀ´ÐÞ¸ÄHEADÖ¸ÕëµÄλÖá£ÈôÊÇҪɾ³ýÒ»¸öcommit£¬¾ÍÐèÒª½«HEADÖ¸ÏòÐèҪɾ³ýµÄcommitµÄÇ°Ò»¸öcommit£¬ÕâÑù¾Í¿ÉÒÔ½«ÐèҪɾ³ýµÄcommit¡°È¥µô¡±¡£
ÀýÈ磬ÎÒÃÇÓÐÈçϼ¸Ìõcommit¼Í¼£º
commit2 commit1
µÇ¼ºó¸´ÖÆ
ÈôÊÇҪɾ³ýcommit2£¬ÎÒÃÇÐèÒªÏȽ«HEADÖ¸Ïòcommit1£º
git reset HEAD~1
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁîµÄÒâ˼Êǽ«HEADÖ¸ÏòÇ°Ò»Ìõcommit£¨commit1£©£¬Õâ¸öÏÂÁî²»µ«½ö½«HEADÖ¸ÕëÒƶ¯µ½ÁËcommit1£¬Í¬Ê±Ò²½«commit2´ÓGitµÄÍâµØ´úÂë¿âÖÐɾ³ýÁË¡£
Èý¡¢Ê¹ÓÃGit pushÏÂÁ¸ü¸ÄÌá½»µ½Ô¶³Ì´úÂë¿â
ÔÚÉÏÒ»²½ÖУ¬Ê¹ÓÃresetÏÂÁîɾ³ýÁËÍâµØ´úÂë¿âÖеÄcommit£¬¿ÉÊÇÈôÊÇҪɾ³ýÔ¶³Ì´úÂë¿âÖеÄcommit£¬¾ÍÐèÒª½«É¾³ýºóµÄ¸ü¸ÄÍÆË͵½Ô¶³Ì´úÂë¿âÖС£ÕâÀïÏÈÈÝÁ½ÖÖ·½·¨£º
1.Ç¿ÖÆÍÆËÍ£¨force push£©£ºÔÚGitÖУ¬Ç¿ÖÆÍÆËÍÊÇ×î³£Óõķ½·¨£¬ÓÉÓÚËü¿ÉÒÔÈÃÔ¶³Ì´úÂë¿âÁ¬Ã¦¸üС£Ç¿ÖÆÍÆË͵ÄÏÂÁîÊÇ£º
git push -f
µÇ¼ºó¸´ÖÆ
Õâ¸öÏÂÁîµÄÒâ˼Êǽ«ÍâµØ¸ü¸ÄÇ¿ÖÆÍÆË͵½Ô¶³Ì´úÂë¿âÖУ¬×ÝÈ»ÕâЩ¸ü¸Ä»áÁýÕÖµôÔ¶³Ì´úÂë¿âÖеÄÒѾÌá½»µÄcommit¡£
2.ʹÓá°revert¡±À´·´×ªcommit£ºÕâ¸öÒªÁìÊÊÓÃÓÚ²»Ïëɾ³ýcommit£¬¶øÊǽ«Ëü·´×ª³ÉÏà·´µÄЧ¹ûµÄÇéÐΡ£ÒªÊ¹ÓÃÕâ¸öÒªÁ죬ÐèÒªÊ×ÏÈÌá½»Ò»¸ö¡°revert¡±commit£¬Õâ¸öcommit»á½«ÏÈÇ°µÄcommit×÷·Ï²¢Ìí¼ÓÒ»¸ö¶ÔÓ¦µÄ·´×ªcommit¡£Õâ¸ö·´×ªcommitµÄÄÚÈÝÊÇÏÈÇ°µÄcommitËù×öµÄÐ޸ĵÄÏà·´²Ù×÷£¬Òò´Ë»á½«´úÂë¿â»Ö¸´µ½ÏÈÇ°µÄ״̬¡£Õâ¸öÏÂÁîʾÀý£º
git revert <commit-id></commit-id>
µÇ¼ºó¸´ÖÆ
ËÄ¡¢×¢ÖØÊÂÏî
ɾ³ýÔ¶³ÌµÄcommitʱӦ¸ÃÉóÉ÷£¬ÓÉÓÚÒ»µ©É¾³ý½«ÎÞ·¨»Ö¸´¡£ÒªÈ·±£ÒѾ±¸·ÝÁËÐèҪɾ³ýµÄcommitµÄ´úÂ룬ÒÔ±ãÈÕºóÐèҪʱ¿ÉÒÔ»Ö¸´¡£
²»¿É´Ó¹«¹²¿Íջɾ³ý´úÂ룬ÓÉÓÚɾ³ýÁËcommitÖ®ºóËüÈÔÈ»ÔÚÆäËûÈ˵ÄÍâµØ¿ÍÕ»Öб£´æ£¬¹«¹²¿ÍÕ»²»»á±»Çå¿Õ¡£ÈôÊÇҪɨ³ýÃô¸ÐÐÅÏ¢£¬¿ÉÒÔ˼Á¿Ê¹ÓÃGitµÄgit filter-branch ÏÂÁî»òÀàËƵŤ¾ß¡£
×ܽ᣺
ÒÔÉÏÊÇGitɾ³ýÔ¶³ÌcommitµÄÒªÁ죬ʹÓÃGitÏÂÁî¾ÙÐдúÂëÖÎÀíµÄ¿ª·¢Õ߶¼ÐèÒªÏàʶÕâЩ»ùÀ´Ô´ÀíºÍ²Ù×÷Àú³Ì¡£ÔÚɾ³ýcommitʱ£¬ÐèҪעÖر£»¤ºÃ´úÂë¿âÖеÄÔ´´úÂ룬²¢×öºÃ±¸·ÝÊÂÇ飬ÒÔÃâÊý¾Ýɥʧ¡£Í¬Ê±£¬ÍƼö½«Ãô¸ÐÐÅÏ¢´Ó´úÂëÖÐɾ³ý£¬ÒÔ±£»¤ÏîÄ¿µÄÇå¾²ÐÔ¡£
ÒÔÉϾÍÊÇ×ܽáGitɾ³ýÔ¶³ÌcommitµÄÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡