{"id":2449,"date":"2019-10-09T13:51:01","date_gmt":"2019-10-09T06:51:01","guid":{"rendered":"https:\/\/tino.vn\/?post_type=ht_kb&#038;p=2449"},"modified":"2019-10-09T13:51:58","modified_gmt":"2019-10-09T06:51:58","slug":"huong-dan-co-ban-ve-git","status":"publish","type":"ht_kb","link":"https:\/\/tino.vn\/blog\/docs\/huong-dan-co-ban-ve-git\/","title":{"rendered":"H\u01b0\u1edbng d\u1eabn c\u01a1 b\u1ea3n v\u1ec1 GIT"},"content":{"rendered":"<div class=\"scrollblock block-setup\">\n<h2 id=\"C\u00e0i_\u0111\u1eb7t\">C\u00e0i \u0111\u1eb7t<\/h2>\n<p><a href=\"http:\/\/git-scm.com\/download\/mac\" rel=\"nofollow noopener\" target=\"_blank\">T\u1ea3i git v\u1ec1 cho OSX<\/a><\/p>\n<p><a href=\"http:\/\/msysgit.github.io\/\" rel=\"nofollow noopener\" target=\"_blank\">T\u1ea3i git v\u1ec1 cho Windows<\/a><\/p>\n<p><a href=\"http:\/\/book.git-scm.com\/2_installing_git.html\" rel=\"nofollow noopener\" target=\"_blank\">T\u1ea3i git v\u1ec1 cho Linux<\/a><\/p>\n<\/div>\n<div class=\"scrollblock block-create\">\n<h2 id=\"T\u1ea1o_m\u1ed9t_repository_m\u1edbi\">T\u1ea1o m\u1ed9t repository m\u1edbi<\/h2>\n<p>\u0110\u1ec3 t\u1ea1o 1 repository m\u1edbi, b\u1ea1n h\u00e3y m\u1edf c\u1eeda s\u1ed5 l\u1ec7nh v\u00e0 g\u00f5 d\u00f2ng l\u1ec7nh sau<br \/>\n<code>git init<\/code><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"scrollblock block-checkout\">\n<h2 id=\"Sao_ch\u00e9p_(clone)_m\u1ed9t_repository\">Sao ch\u00e9p (clone) m\u1ed9t repository<\/h2>\n<p>\u0111\u1ec3 clone 1 repository c\u00f3 s\u1eb5n \u1edf tr\u00ean m\u00e1y c\u1ee5c b\u1ed9, b\u1ea1n h\u00e3y s\u1eed d\u1ee5ng d\u00f2ng l\u1ec7nh sau:<br \/>\n<code>git clone \/\u0111\u01b0\u1eddng-d\u1eabn-\u0111\u1ebfn\/repository\/<\/code><br \/>\nN\u1ebfu repository \u0111\u00f3 \u1edf m\u00e1y ch\u1ee7 kh\u00e1c th\u00ec b\u1ea1n h\u00e3y g\u00f5 d\u00f2ng l\u1ec7nh sau:<br \/>\n<code>git clone t\u00eanusername@\u0111\u1ecbach\u1ec9m\u00e1ych\u1ee7:\/\u0111\u01b0\u1eddng-d\u1eabn-\u0111\u1ebfn\/repository<\/code><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"scrollblock block-trees\">\n<h2 id=\"Quy_tr\u00ecnh_l\u00e0m_vi\u1ec7c\">Quy tr\u00ecnh l\u00e0m vi\u1ec7c<\/h2>\n<p>th\u01b0 m\u1ee5c c\u1ee5c b\u1ed9 c\u1ee7a b\u1ea1n bao g\u1ed3m ba &#8220;trees&#8221; \u0111\u01b0\u1ee3c duy tr\u00ec b\u1edfi git. \u0111\u1ea7u ti\u00ean l\u00e0\u00a0<code>Th\u01b0 M\u1ee5c \u0110ang L\u00e0m Vi\u1ec7c (Working Directory)<\/code>\u00a0c\u00f3 ch\u1ee9a c\u00e1c t\u1eadp tin hi\u1ec7n t\u1ea1i. c\u00e1i th\u1ee9 hai l\u00e0\u00a0<code>Ch\u1ec9 M\u1ee5c (Index)<\/code>\u00a0\u0111\u00f3ng vai tr\u00f2 nh\u01b0 staging area v\u00e0 cu\u1ed1i c\u00f9ng l\u00e0\u00a0<code>HEAD<\/code>\u00a0tr\u1ecf \u0111\u1ebfn commit g\u1ea7n \u0111\u00e2y nh\u1ea5t c\u1ee7a b\u1ea1n.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/trees.png\" alt=\"\" title=\"\"><\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"scrollblock block-add\">\n<h2 id=\"Th\u00eam_(add)_&amp;_commit\">Th\u00eam (add) &amp; commit<\/h2>\n<p>B\u1ea1n c\u00f3 th\u1ec3 \u0111\u1ec1 xu\u1ea5t thay \u0111\u1ed5i (th\u00eam n\u00f3 v\u00e0o ch\u1ec9 m\u1ee5c\u00a0<b>Index<\/b>) b\u1eb1ng c\u00e1ch<br \/>\n<code>git add &lt;t\u00ean-t\u1eadp-tin&gt;<\/code><br \/>\n<code>git add *<\/code><br \/>\n\u0110\u00e2y l\u00e0 b\u01b0\u1edbc \u0111\u1ea7u ti\u00ean trong quy tr\u00ecnh git c\u01a1 b\u1ea3n. \u0110\u1ec3 th\u1eadt s\u1ef1 commit nh\u1eefng thay \u0111\u1ed5i, b\u1ea1n s\u1eed d\u1ee5ng<br \/>\n<code>git commit -m \"Ghi ch\u00fa Commit\"<\/code><br \/>\nB\u00e2y gi\u1edd th\u00ec t\u1eadp tin \u0111\u00e3 \u0111\u01b0\u1ee3c commit \u0111\u1ebfn\u00a0<b>HEAD<\/b>, nh\u01b0ng ch\u01b0a ph\u1ea3i tr\u00ean th\u01b0 m\u1ee5c remote.<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"scrollblock block-remote\">\n<h2 id=\"\u0110\u1ea9y_(push)_c\u00e1c_thay_\u0111\u1ed5i\">\u0110\u1ea9y (push) c\u00e1c thay \u0111\u1ed5i<\/h2>\n<p>Thay \u0111\u1ed5i c\u1ee7a b\u1ea1n hi\u1ec7n \u0111ang n\u1eb1m t\u1ea1i\u00a0<b>HEAD<\/b>\u00a0c\u1ee7a b\u1ea3n sao c\u1ee5c b\u1ed9 \u0111ang l\u00e0m vi\u1ec7c. \u0110\u1ec3 g\u1eedi nh\u1eefng thay \u0111\u1ed5i \u0111\u00f3 \u0111\u1ebfn repository remote, b\u1ea1n th\u1ef1c thi<br \/>\n<code>git push origin master<\/code><br \/>\nThay \u0111\u1ed5i\u00a0<i>master<\/i>\u00a0b\u1eb1ng b\u1ea5t c\u1ee9 nh\u00e1nh n\u00e0o m\u00e0 b\u1ea1n mu\u1ed1n \u0111\u1ea7y nh\u1eefng thay \u0111\u1ed5i \u0111\u1ebfn.<\/p>\n<p>N\u1ebfu b\u1ea1n ch\u01b0a clone m\u1ed9t repository hi\u1ec7n c\u00f3 v\u00e0 mu\u1ed1n k\u1ebft n\u1ed1i repository c\u1ee7a b\u1ea1n \u0111\u1ebfn m\u00e1y ch\u1ee7 remote, b\u1ea1n ph\u1ea3i th\u00eam n\u00f3 v\u1edbi<br \/>\n<code>git remote add origin &lt;m\u00e1y-ch\u1ee7&gt;<\/code><br \/>\nB\u00e2y gi\u1edd b\u1ea1n \u0111\u00e3 c\u00f3 th\u1ec3 \u0111\u1ea9y c\u00e1c thay \u0111\u1ed5i c\u1ee7a m\u00ecnh v\u00e0o m\u00e1y ch\u1ee7 \u0111\u00e3 ch\u1ecdn<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<div class=\"scrollblock block-branching\">\n<h2 id=\"Nh\u00e1nh\">Nh\u00e1nh<\/h2>\n<p>C\u00e1c nh\u00e1nh (branches) \u0111\u01b0\u1ee3c d\u00f9ng \u0111\u1ec3 ph\u00e1t tri\u1ec3n t\u00ednh n\u0103ng t\u00e1ch ri\u00eang ra t\u1eeb nh\u1eefng nh\u00e1nh kh\u00e1c. Nh\u00e1nh\u00a0<i>master<\/i>\u00a0l\u00e0 nh\u00e1nh &#8220;m\u1eb7c \u0111\u1ecbnh&#8221; khi b\u1ea1n t\u1ea1o m\u1ed9t repository. S\u1eed d\u1ee5ng c\u00e1c nh\u00e1nh kh\u00e1c tri \u0111ang trong giai \u0111o\u1ea1n ph\u00e1t tri\u1ec3n v\u00e0 merge tr\u1edf l\u1ea1i nh\u00e1nh master m\u1ed9t khi \u0111\u00e3 ho\u00e0n t\u1ea5t.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/tino.vn\/blog\/wp-content\/uploads\/2019\/10\/branches.png\" alt=\"\" title=\"\">t\u1ea1o m\u1ed9t nh\u00e1nh m\u1edbi v\u00e0 \u0111\u1eb7t t\u00ean l\u00e0 &#8220;feature_x&#8221; v\u00e0 chuy\u1ec3n qua nh\u00e1nh \u0111\u00f3 (t\u1eeb master) b\u1eb1ng c\u00e1ch<br \/>\n<code>git checkout -b feature_x<\/code><br \/>\ntr\u1edf l\u1ea1i nh\u00e1nh master<br \/>\n<code>git checkout master<\/code><br \/>\nv\u00e0 x\u00f3a nh\u00e1nh feature_x \u0111\u00f3 l\u1ea7n n\u1eeda<br \/>\n<code>git branch -d feature_x<\/code><br \/>\nm\u1ed9t nh\u00e1nh\u00a0<i>kh\u00f4ng c\u00f3 gi\u00e1 tr\u1ecb v\u1edbi c\u00e1c nh\u00e1nh kh\u00e1c<\/i>\u00a0tr\u1eeb khi b\u1ea1n \u0111\u1ea9y nh\u00e1nh \u0111\u00f3 \u0111\u1ebfn remote repository<br \/>\n<code>git push origin &lt;nh\u00e1nh&gt;<\/code><\/p>\n<\/div>\n<div class=\"scrollblock block-merging\">\n<h2 id=\"C\u1eadp_nh\u1eadt_&amp;_tr\u1ed9n_(update_&amp;_merge)\">C\u1eadp nh\u1eadt &amp; tr\u1ed9n (update &amp; merge)<\/h2>\n<p>\u0111\u1ec3 c\u1eadp nh\u1eadt repository c\u1ee5c b\u1ed9 c\u1ee7a b\u1ea1n v\u00e0 commit m\u1edbi nh\u1ea5t, th\u1ef1c thi<br \/>\n<code>git pull<\/code><br \/>\ntrong th\u1ef1 m\u1ee5c \u0111ang l\u00e0m vi\u1ec7c \u0111\u1ec3\u00a0<i>l\u1ea5y v\u1ec1 (fetch)<\/i>\u00a0v\u00e0\u00a0<i>tr\u1ed9n (merge)<\/i>\u00a0c\u00e1c thay \u0111\u1ed5i \u1edf remote.<br \/>\n\u0111\u1ec3 tr\u1ed9n m\u1ed9t nh\u00e1nh kh\u00e1c v\u00e0o nh\u00e1nh \u0111ang ho\u1ea1t \u0111\u1ed9ng (vd: master), s\u1eed d\u1ee5ng<br \/>\n<code>git merge &lt;nh\u00e1nh&gt;<\/code><br \/>\ntrong c\u1ea3 hai tr\u01b0\u1eddng h\u1ee3p, git c\u1ed1 g\u1eafng tr\u1ed9n t\u1ef1 \u0111\u1ed9ng (auto-merge) c\u00e1c thay \u0111\u1ed5i. Kh\u00f4ng may, \u0111i\u1ec1u n\u00e0y kh\u00f4ng ph\u1ea3i l\u00fac n\u00e0o c\u0169ng l\u00e0m \u0111\u01b0\u1ee3c v\u00e0 th\u01b0\u1eddng d\u1eabn \u0111\u1ebfn\u00a0<i>xung \u0111\u1ed9t<\/i>. Tr\u00e1ch nhi\u1ec7m c\u1ee7a b\u1ea1n l\u00e0 tr\u1ed9n\u00a0<i>c\u00e1c xung \u0111\u1ed9t<\/i>\u00a0\u0111\u00f3 th\u1ee7 c\u00f4ng b\u1eb1ng c\u00e1ch ch\u1ec9nh s\u1eeda c\u00e1c t\u1eadp tin \u0111\u01b0\u1ee3c hi\u1ec3n th\u1ecb b\u1edfi git. Sau khi thay \u0111\u1ed5i, b\u1ea1n ph\u1ea3i \u0111\u00e1nh d\u1ea5u ch\u00fang l\u00e0 \u0111\u00e3 \u0111\u01b0\u1ee3c tr\u1ed9n (merged) v\u1edbi l\u1ec7nh<br \/>\n<code>git add &lt;t\u00ean-t\u1eadp-tin&gt;<\/code><br \/>\ntr\u01b0\u1edbc khi tr\u1ed9n c\u00e1c thay \u0111\u1ed5i, b\u1ea1n c\u00f3 th\u1ec3 xem tr\u01b0\u1edbc ch\u00fang b\u1eb1ng c\u00e1c<br \/>\n<code>git diff &lt;nh\u00e1nh_ngu\u1ed3n&gt; &lt;nh\u00e1nh_m\u1ee5c_ti\u00eau&gt;<\/code><\/p>\n<\/div>\n<div class=\"scrollblock block-tagging\">\n<h2 id=\"G\u1eafn_nh\u00e3n_(tagging)\">G\u1eafn nh\u00e3n (tagging)<\/h2>\n<p>ng\u01b0\u1eddi ta khuy\u00ean n\u00ean t\u1ea1o nh\u00e3n (tags) khi ph\u00e1t h\u00e0nh ph\u1ea7n m\u1ec1m. \u0111\u00e2y l\u00e0 kh\u00e1i ni\u1ec7m \u0111\u01b0\u1ee3c bi\u1ebft \u0111\u1ebfn, \u0111\u00e3 t\u1eebng c\u00f3 tr\u00ean SVN. B\u1ea1n t\u1ea1o tag m\u1edbi t\u00ean l\u00e0\u00a0<i>1.0.0<\/i>\u00a0b\u1eb1ng c\u00e1ch<br \/>\n<code>git tag 1.0.0 1b2e1d63ff<\/code><br \/>\nchu\u1ed7i\u00a0<i>1b2e1d63ff<\/i>\u00a0l\u00e0 10 k\u00fd t\u1ef1 \u0111\u1ea7u ti\u00ean c\u1ee7a m\u00e3 commit (commit id) m\u00e0 b\u1ea1n mu\u1ed1n tham chi\u1ebfu \u0111\u1ebfn b\u1eb1ng nh\u00e3n c\u1ee7a b\u1ea1n. B\u1ea1n c\u00f3 th\u1ec3 l\u1ea5y m\u00e3 commit v\u1edbi l\u1ec7nh<br \/>\n<code>git log<\/code><br \/>\nb\u1ea1n c\u0169ng c\u00f3 th\u1ec3 s\u1eed d\u1ee5ng \u00edt k\u00fd t\u1ef1 h\u01a1n t\u1eeb m\u00e3 commit, n\u00f3 ch\u1ec9 c\u1ea7n ph\u1ea3i l\u00e0 duy nh\u1ea5t.<\/p>\n<\/div>\n<div class=\"scrollblock block-checkout-replace\">\n<h2 id=\"Thay_th\u1ebf_c\u00e1c_thay_\u0111\u1ed5i_c\u1ee5c_b\u1ed9\">Thay th\u1ebf c\u00e1c thay \u0111\u1ed5i c\u1ee5c b\u1ed9<\/h2>\n<p>Trong tr\u01b0\u1eddng h\u1ee3p b\u1ea1n l\u00e0m sai \u0111i\u1ec1u g\u00ec \u0111\u00f3, b\u1ea1n c\u00f3 th\u1ec3 thay th\u1ebf c\u00e1c thay \u0111\u1ed5i c\u1ee5c b\u1ed9 b\u1eb1ng l\u1ec7nh<br \/>\n<code>git checkout -- &lt;t\u00ean-t\u1eadp-tin&gt;<\/code><br \/>\nl\u1ec7nh n\u00e0y thay th\u1ebf nh\u1eefng thay \u0111\u1ed5i trong &#8220;tree&#8221; \u0111ang l\u00e0m vi\u1ec7c v\u1edbi n\u1ed9i dung m\u1edbi nh\u1ea5t c\u1ee7a HEAD. C\u00e1c thay \u0111\u1ed5i \u0111\u00e3 \u0111\u01b0\u1ee3c th\u00eam v\u00e0o ch\u1ec9 m\u1ee5c, k\u1ec3 c\u1ea3 c\u00e1c t\u1eadp tin m\u1edbi, \u0111i\u1ec1u n\u00e0y s\u1ebd \u0111\u01b0\u1ee3c gi\u1eef l\u1ea1i.<\/p>\n<p>N\u1ebfu b\u1ea1n mu\u1ed1n h\u1ee7y t\u1ea5t c\u1ea3 thay \u0111\u1ed5i v\u00e0 commit c\u1ee5c b\u1ed9, l\u1ea5y v\u1ec1 (fetch) l\u1ecbch s\u1eed g\u1ea7n \u0111\u00e2y nh\u1ea5t t\u1eeb m\u00e1y ch\u1ee7 v\u00e0 tr\u1ecf nh\u00e1nh master c\u1ee5c b\u1ed9 v\u00e0o n\u00f3 nh\u01b0 sau<br \/>\n<code>git fetch origin<\/code><br \/>\n<code>git reset --hard origin\/master<\/code><\/p>\n<\/div>\n<p>g\u1ee3i \u00fd h\u1eefu \u00edch<\/p>\n<div class=\"scrollblock block-hints\">\n<p>git GUI t\u00edch h\u1ee3p s\u1eb5n<br \/>\n<code>gitk<\/code><br \/>\ns\u1eed d\u1ee5ng k\u1ebft qu\u1ea3 git v\u1edbi nhi\u1ec1u m\u00e0u<br \/>\n<code>git config color.ui true<\/code><br \/>\nhi\u1ec7n log tr\u00ean ch\u1ec9 m\u1ed9t d\u00f9ng m\u1ed7i commit<br \/>\n<code>git config format.pretty oneline<\/code><br \/>\ns\u1eed d\u1ee5ng th\u00eam t\u1eadp tin t\u01b0\u01a1ng t\u00e1c<br \/>\n<code>git add -i<\/code><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>C\u00e0i \u0111\u1eb7t T\u1ea3i git v\u1ec1 cho OSX T\u1ea3i git v\u1ec1 cho Windows T\u1ea3i git v\u1ec1 cho Linux T\u1ea1o m\u1ed9t repository m\u1edbi \u0110\u1ec3 t\u1ea1o 1 repository m\u1edbi, b\u1ea1n h\u00e3y m\u1edf c\u1eeda s\u1ed5 l\u1ec7nh v\u00e0 g\u00f5 d\u00f2ng l\u1ec7nh sau git init &nbsp; Sao ch\u00e9p (clone) m\u1ed9t repository \u0111\u1ec3 clone 1 repository c\u00f3 s\u1eb5n \u1edf tr\u00ean m\u00e1y c\u1ee5c [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":16424,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","format":"standard","meta":{"footnotes":""},"tags":[],"ht_kb_category":[29],"class_list":["post-2449","ht_kb","type-ht_kb","status-publish","format-standard","has-post-thumbnail","hentry","ht_kb_category-cau-hoi-thuong-gap"],"_links":{"self":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/2449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb"}],"about":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/types\/ht_kb"}],"author":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/comments?post=2449"}],"version-history":[{"count":0,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb\/2449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media\/16424"}],"wp:attachment":[{"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/tags?post=2449"},{"taxonomy":"ht_kb_category","embeddable":true,"href":"https:\/\/tino.vn\/blog\/wp-json\/wp\/v2\/ht_kb_category?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}