{"id":12238,"date":"2018-09-04T22:33:21","date_gmt":"2018-09-05T03:33:21","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=12238"},"modified":"2021-06-28T09:39:29","modified_gmt":"2021-06-28T14:39:29","slug":"git-reset","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=12238","title":{"rendered":"Git Reset"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Overview\">Overview<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Three_Kinds_Trees\">Three Kinds Trees<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Three_Flavors_of_Resets\">Three Flavors of Resets<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Reset_with_Path\">Reset with Path<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Squashing_Commits\">Squashing Commits<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#reset_vs_checkout\">reset vs checkout<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Remove_Untracked_Files\">Remove Untracked Files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Ignore_Committed_Files\">Ignore Committed Files<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Examples\">Examples<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#References\">References<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Overview\"><h2>Overview<\/h2><\/span>\n<p>* A tree can be visualized as a directory<br \/>\n* <em>git reset<\/em>: moves HEAD pointer in the current branch<br \/>\n* Counting commits backward:<br \/>\n<em>HEAD~<\/em>: one commit back<br \/>\n<em>HEAD~2<\/em>: two commits back<br \/>\n<em>HEAD~n<\/em>: n commits back<br \/>\n* Counting commits forward (do not use):<br \/>\n<em>HEAD@{1}<\/em>: one commit forward<br \/>\n* You can also use commit hash value to reset HEAD pointer<br \/>\n<em>git reset cdd1a5<\/em><\/p>\n<span id=\"Three_Kinds_Trees\"><h2>Three Kinds Trees<\/h2><\/span>\n<p>* <em>HEAD<\/em>: the directory containing last commit (<em>git commit<\/em> copies files from index to HEAD directory)<br \/>\n* <em>Index<\/em>: the directory containing next commit (<em>git add<\/em> copies files from working directory to index directory)<br \/>\n* <em>Working directory<\/em><\/p>\n<span id=\"Three_Flavors_of_Resets\"><h2>Three Flavors of Resets<\/h2><\/span>\n<p>* <em>soft<\/em>: move HEAD pointer back one commit (equivalent to undo last commit):<\/p>\n<pre lang=\"bash\">\r\ngit reset --soft HEAD~\r\n<\/pre>\n<p>* <em>mixed<\/em>: move HEAD pointer back one commit AND update index files with HEAD (Equivalent to undo last commit AND last staging. This is default behavior):<\/p>\n<pre lang=\"bash\">\r\ngit reset --mixed HEAD~\r\n<\/pre>\n<p>* <em>hard<\/em>: move HEAD pointer back one commit AND update BOTH index and working directory files with HEAD (Equivalent to undo everything! NOT working directory safe!):<\/p>\n<pre lang=\"bash\">\r\ngit reset --hard HEAD~\r\n<\/pre>\n<span id=\"Reset_with_Path\"><h2>Reset with Path<\/h2><\/span>\n<p>* Reset with path can only be applied with either <em>&#8211;mixed<\/em> (default) or <em>&#8211;hard<\/em> since HEAD pointer can only be moved for all files<\/p>\n<pre lang=\"bash\">\r\n# Undo last add for foo.txt file:\r\ngit reset foo.txt \r\n# equivalent to: \r\ngit reset --mixed HEAD~ foo.txt\r\n<\/pre>\n<span id=\"Squashing_Commits\"><h2>Squashing Commits<\/h2><\/span>\n<p>* Redo multiple previous commits into one single new commit<\/p>\n<pre lang=\"bash\">\r\ngit reset --soft HEAD~2\r\ngit commit -m \"Squashed commits\"\r\n<\/pre>\n<span id=\"reset_vs_checkout\"><h2>reset vs checkout<\/h2><\/span>\n<p>* Use checkout on branches<\/p>\n<pre lang=\"bash\">\r\ngit checkout dev_branch\r\n# is equivalent to:\r\ngit reset --hard dev_branch\r\n# but \r\n# - is working directory safe: changes will be merged\r\n# - also moves HEAD to dev_branch\r\n<\/pre>\n<p>* Do not use checkout with path. It&#8217;s not working directory safe!<\/p>\n<span id=\"Remove_Untracked_Files\"><h2>Remove Untracked Files<\/h2><\/span>\n<p>* Use with caution! <\/p>\n<pre lang=\"bash\">\r\ngit clean -f # remove untracked files\r\ngit clean -df # remove untracked files and directories\r\ngit clean -xdf # also removes ignored files!\r\n<\/pre>\n<span id=\"Ignore_Committed_Files\"><h2>Ignore Committed Files<\/h2><\/span>\n<p>* Ignore files that have already been Committed<\/p>\n<pre lang=\"bash\">\r\n# remove all cached files from index\r\ngit rm -r --cached .\r\n# add all files back except files in .ignore file\r\ngit add .\r\n# commit without all ignored files\r\ngit commit -m 'Removing ignored files'\r\n<\/pre>\n<span id=\"Examples\"><h2>Examples<\/h2><\/span>\n<pre lang=\"bash\">\r\nIf you want to revert changes made to your working copy, do this:\r\ngit checkout .\r\n\r\nIf you want to revert changes made to the index (i.e., that you have added), do this. Warning this will reset all of your unpushed commits to master!:\r\ngit reset\r\n\r\nIf you want to revert a change that you have committed, do this:\r\ngit revert <commit 1> <commit 2>\r\n\r\nIf you want to remove untracked files (e.g., new files, generated files):\r\ngit clean -f\r\n\r\nOr untracked directories (e.g., new or automatically generated directories):\r\ngit clean -fd\r\n<\/pre>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p>* <a href=\"https:\/\/git-scm.com\/book\/en\/v2\/Git-Tools-Reset-Demystified\">7.7 Git Tools &#8211; Reset Demystified<\/a><br \/>\n<a href=\"https:\/\/stackoverflow.com\/questions\/1146973\/how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state\">https:\/\/stackoverflow.com\/questions\/1146973\/how-do-i-revert-all-local-changes-in-git-managed-project-to-previous-state<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Overview * A tree can be visualized as a directory * git reset: moves HEAD pointer in the current branch * Counting commits backward: HEAD~: one commit back HEAD~2: two commits back HEAD~n: n commits back * Counting commits forward &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=12238\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[236],"tags":[616],"class_list":["post-12238","post","type-post","status-publish","format-standard","hentry","category-git","tag-git"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-3bo","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12238"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12238\/revisions"}],"predecessor-version":[{"id":12684,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/12238\/revisions\/12684"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}