{"id":1841,"date":"2017-12-06T08:50:09","date_gmt":"2017-12-06T08:50:09","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=1841"},"modified":"2025-02-01T22:47:38","modified_gmt":"2025-02-01T22:47:38","slug":"git-reset-vs-git-revert","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/git-reset-vs-git-revert\/","title":{"rendered":"Git Commands Tutorials and Example: Git Reset &#8211; Git Revert"},"content":{"rendered":"<p><strong>&nbsp;git reset Vs git revert<\/strong><br>\ngit reset Vs git revert<\/p>\n<p>The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures out how to undo the changes introduced by the commit and appends a new commit with the resulting content. This prevents Git from losing history, which is important for the integrity of your revision history and for reliable collaboration.<\/p>\n<p>git revert<\/p>\n<p>Reverting vs. Resetting<br>\nIt&#8217;s important to understand that git revert undoes a single commit\u2014it does not &#8220;revert&#8221; back to the previous state of a project by removing all subsequent commits. In Git, this is actually called a reset, not a revert.\\<\/p>\n<p>Reverting has two important advantages over resetting. First, it doesn\u2019t change the project history, which makes it a \u201csafe\u201d operation for commits that have already been published to a shared repository. For details about why altering shared history is dangerous, please see the git reset page.<\/p>\n<p>Second, git revert is able to target an individual commit at an arbitrary point in the history, whereas git reset can only work backwards from the current commit. For example, if you wanted to undo an old commit with git reset, you would have to remove all of the commits that occurred after the target commit, remove it, then re-commit all of the subsequent commits. Needless to say, this is not an elegant undo solution.<\/p>\n<p>git reset<\/p>\n<p>If git revert is a \u201csafe\u201d way to undo changes, you can think of git reset as the dangerous method. When you undo with git reset(and the commits are no longer referenced by any ref or the reflog), there is no way to retrieve the original copy\u2014it is a permanent undo. Care must be taken when using this tool, as it\u2019s one of the only Git commands that has the potential to lose your work.<\/p>\n<p>Usage<\/p>\n<p>git reset<\/p>\n<p>Remove the specified file from the staging area, but leave the working directory unchanged. This unstages a file without overwriting any changes.<\/p>\n<p>git reset<\/p>\n<p>Reset the staging area to match the most recent commit, but leave the working directory unchanged. This unstages all files without overwriting any changes, giving you the opportunity to re-build the staged snapshot from scratch.<\/p>\n<p>git reset &#8211;hard<\/p>\n<p>Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the &#8211;hard flag tells Git to overwrite all changes in the working directory, too. Put another way: this obliterates all uncommitted changes, so make sure you really want to throw away your local developments before using it.<\/p>\n<p>git reset<\/p>\n<p>Move the current branch tip backward to , reset the staging area to match, but leave the working directory alone. All changes made since will reside in the working directory, which lets you re-commit the project history using cleaner, more atomic snapshots.<\/p>\n<p>git reset &#8211;hard<\/p>\n<p>Move the current branch tip backward to and reset both the staging area and the working directory to match. This obliterates not only the uncommitted changes, but all commits after , as well.<br>\nRegards,<br>\nRajesh Kumar<br>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n<p><strong>rajeshkumar replied the topic: git reset Vs git revert<\/strong><br>\nMore Reference &#8212;<\/p>\n<p><a href=\"http:\/\/www.atlassian.com\/git\/tutorial\/undoing-changes#!revert\" target=\"_blank\" rel=\"noopener\">www.atlassian.com\/git\/tutorial\/undoing-changes#!revert <\/a><br>\n<a href=\"http:\/\/www.atlassian.com\/git\/tutorial\/undoing-changes#!reset\" target=\"_blank\" rel=\"noopener\">www.atlassian.com\/git\/tutorial\/undoing-changes#!reset<\/a><br>\nRegards,<br>\nRajesh Kumar<br>\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" target=\"_blank\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a><\/p>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_36280\"><iframe loading=\"lazy\"  id=\"_ytid_18764\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_36280\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>&nbsp;git reset Vs git revert git reset Vs git revert The git revert command undoes a committed snapshot. But, instead of removing the commit from the project history, it figures&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[38],"tags":[160],"class_list":["post-1841","post","type-post","status-publish","format-standard","hentry","category-git","tag-git"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=1841"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1841\/revisions"}],"predecessor-version":[{"id":25734,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/1841\/revisions\/25734"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=1841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=1841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=1841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}