🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 169 (from laksa026)

2. Crawled Status Check

Query:
Response:

3. Robots.txt Check

Query:
Response:

4. Spam/Ban Check

Query:
Response:

5. Seen Status Check

ℹ️ Skipped - page is already crawled

📄
INDEXABLE
✅
CRAWLED
19 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.7 months ago
History dropPASSisNull(history_drop_reason)No drop reason
Spam/banPASSfh_dont_index != 1 AND ml_spam_score = 0ml_spam_score=0
CanonicalPASSmeta_canonical IS NULL OR = '' OR = src_unparsedNot set

Page Details

PropertyValue
URLhttps://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit
Last Crawled2026-03-30 01:29:56 (19 days ago)
First Indexed2019-06-11 02:22:02 (6 years ago)
HTTP Status Code200
Meta Titlegit checkout - How do I revert a Git repository to a previous commit? - Stack Overflow
Meta Descriptionnull
Meta Canonicalnull
Boilerpipe Text
This depends a lot on what you mean by "revert". Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: # This will detach your HEAD, that is, leave you with no branch checked out: git checkout 0d1d7fc32 Or if you want to make commits while you're there, go ahead and make a new branch while you're at it: git checkout -b old-state 0d1d7fc32 To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.) Hard delete unpublished commits If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset: # This will destroy any local modifications. # Don't do it if you have uncommitted work you want to keep. git reset --hard 0d1d7fc32 # Alternatively, if there's work to keep: git stash git reset --hard 0d1d7fc32 git stash pop # This saves the modifications, then reapplies that patch after resetting. # You could get merge conflicts, if you've modified things which were # changed since the commit you reset to. If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again. Undo published commits with new commits On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history. First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits. # This lists all merge commits between 0d1d7fc and HEAD: git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' # This lists all non merge commits between 0d1d7fc and HEAD: git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' Note: if you revert multiple commits, the order matters. Start with the most recent commit. # This will create three separate revert commits, use non merge commits only: git revert a867b4af 25eee4ca 0766c053 # It also takes ranges. This will revert the last two commits: git revert HEAD~2..HEAD # Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash): git revert 0d1d7fc..a867b4a # Reverting a merge commit. You can also use a range of merge commits here. git revert -m 1 <merge_commit_sha> # To get just one, you could use `rebase -i` to squash them afterwards # Or, you could do it manually (be sure to do this at top level of the repo) # get your index and work tree into the desired state, without changing HEAD: git checkout 0d1d7fc32 . # Then commit. Be sure and write a good message describing what you just did git commit The git-revert manpage actually covers a lot of this in its description. Another useful link is this git-scm.com section discussing git-revert . If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section). You may also find this answer helpful in this case: How can I move HEAD back to a previous location? (Detached head) & Undo commits
Markdown
# ![site logo](https://stackoverflow.com/Content/Img/SE-logo75.png) By clicking “Sign up”, you agree to our [terms of service](https://stackoverflow.com/legal/terms-of-service/public) and acknowledge you have read our [privacy policy](https://stackoverflow.com/legal/privacy-policy). # OR Already have an account? [Log in](https://stackoverflow.com/users/login) [Skip to main content](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#content) 1. [About](https://stackoverflow.co/) 2. Products 3. [For Teams](https://stackoverflow.co/internal/) 4. Try new site Try BETA 1. [Stack Internal Implement a knowledge platform layer to power your enterprise and AI tools.](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-for-teams) 2. [Stack Data Licensing Get access to top-class technical expertise with trusted & attributed content.](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=overflow-api) 3. [Stack Ads Connect your brand to the world’s most trusted technologist communities.](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=stack-overflow-advertising) 4. [Releases Keep up-to-date on features we add to Stack Overflow and Stack Internal.](https://stackoverflow.blog/releases/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=releases) 5. [About the company](https://stackoverflow.co/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=about-the-company) [Visit the blog](https://stackoverflow.blog/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=top-nav&utm_content=blog) 1. ### [current community](https://stackoverflow.com/) - [Stack Overflow](https://stackoverflow.com/) [help](https://stackoverflow.com/help) [chat](https://chat.stackoverflow.com/?tab=explore) - [Meta Stack Overflow](https://meta.stackoverflow.com/) ### your communities [Sign up](https://stackoverflow.com/users/signup?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F4114095%2Fhow-do-i-revert-a-git-repository-to-a-previous-commit) or [log in](https://stackoverflow.com/users/login?ssrc=site_switcher&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F4114095%2Fhow-do-i-revert-a-git-repository-to-a-previous-commit) to customize your list. ### [more stack exchange communities](https://stackexchange.com/sites) [company blog](https://stackoverflow.blog/) 2. [Log in](https://stackoverflow.com/users/login?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F4114095%2Fhow-do-i-revert-a-git-repository-to-a-previous-commit) 3. [Sign up](https://stackoverflow.com/users/signup?ssrc=head&returnurl=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F4114095%2Fhow-do-i-revert-a-git-repository-to-a-previous-commit) # Let's set up your homepage Select a few topics you're interested in: python javascript c\# reactjs java android html flutter c++ node.js typescript css r php angular next.js spring-boot machine-learning sql excel ios azure docker Or search from our full list: - javascript - python - java - c\# - php - android - html - jquery - c++ - css - ios - sql - mysql - r - reactjs - node.js - arrays - c - asp.net - json - python-3.x - .net - ruby-on-rails - sql-server - swift - django - angular - objective-c - excel - pandas - angularjs - regex - typescript - ruby - linux - ajax - iphone - vba - xml - laravel - spring - asp.net-mvc - database - wordpress - string - flutter - postgresql - mongodb - wpf - windows - xcode - amazon-web-services - bash - git - oracle-database - spring-boot - dataframe - azure - firebase - list - multithreading - docker - vb.net - react-native - eclipse - algorithm - powershell - macos - visual-studio - numpy - image - forms - scala - function - vue.js - performance - twitter-bootstrap - selenium - winforms - kotlin - loops - dart - express - sqlite - hibernate - matlab - python-2.7 - shell - rest - apache - entity-framework - android-studio - csv - maven - linq - qt - dictionary - unit-testing - asp.net-core - facebook - apache-spark - tensorflow - file - swing - class - unity-game-engine - sorting - date - authentication - go - symfony - t-sql - opencv - matplotlib - .htaccess - google-chrome - for-loop - datetime - codeigniter - perl - http - validation - sockets - google-maps - object - uitableview - xaml - oop - visual-studio-code - if-statement - cordova - ubuntu - web-services - email - android-layout - github - spring-mvc - elasticsearch - kubernetes - selenium-webdriver - ms-access - ggplot2 - user-interface - parsing - pointers - google-sheets - c++11 - security - machine-learning - google-apps-script - ruby-on-rails-3 - templates - flask - nginx - variables - exception - sql-server-2008 - gradle - debugging - tkinter - delphi - listview - jpa - asynchronous - haskell - web-scraping - jsp - pdf - ssl - amazon-s3 - google-cloud-platform - xamarin - testing - jenkins - wcf - batch-file - generics - npm - ionic-framework - network-programming - unix - recursion - google-app-engine - mongoose - visual-studio-2010 - .net-core - android-fragments - assembly - animation - math - rust - svg - session - intellij-idea - hadoop - join - winapi - curl - django-models - laravel-5 - next.js - url - heroku - http-redirect - tomcat - inheritance - google-cloud-firestore - webpack - gcc - swiftui - image-processing - keras - asp.net-mvc-4 - logging - dom - matrix - pyspark - actionscript-3 - button - post - optimization - firebase-realtime-database - cocoa - jquery-ui - xpath - iis - web - d3.js - javafx - firefox - xslt - internet-explorer - caching - select - asp.net-mvc-3 - opengl - events - asp.net-web-api - plot - dplyr - encryption - magento - stored-procedures - search - amazon-ec2 - ruby-on-rails-4 - memory - multidimensional-array - canvas - audio - random - jsf - vector - redux - cookies - input - facebook-graph-api - flash - indexing - xamarin.forms - arraylist - ipad - cocoa-touch - data-structures - video - model-view-controller - azure-devops - serialization - apache-kafka - jdbc - razor - awk - woocommerce - routes - mod-rewrite - servlets - excel-formula - beautifulsoup - filter - iframe - docker-compose - design-patterns - aws-lambda - text - visual-c++ - django-rest-framework - cakephp - mobile - android-intent - struct - react-hooks - methods - groovy - mvvm - lambda - ssh - time - checkbox - ecmascript-6 - grails - installation - google-chrome-extension - cmake - sharepoint - shiny - spring-security - jakarta-ee - plsql - android-recyclerview - core-data - types - sed - meteor - android-activity - bootstrap-4 - activerecord - websocket - replace - graph - group-by - scikit-learn - vim - file-upload - boost - junit - memory-management - sass - async-await - import - deep-learning - error-handling - eloquent - dynamic - dependency-injection - silverlight - soap - layout - apache-spark-sql - charts - deployment - browser - gridview - svn - while-loop - google-bigquery - vuejs2 - highcharts - dll - ffmpeg - view - foreach - makefile - plugins - redis - c\#-4.0 - reporting-services - jupyter-notebook - unicode - merge - reflection - https - server - google-maps-api-3 - twitter - oauth-2.0 - extjs - terminal - pip - axios - split - cmd - encoding - pytorch - django-views - collections - database-design - hash - netbeans - automation - data-binding - ember.js - build - tcp - pdo - apache-flex - sqlalchemy - entity-framework-core - concurrency - command-line - spring-data-jpa - printing - react-redux - java-8 - lua - html-table - neo4j - ansible - service - jestjs - enums - parameters - flexbox - mysqli - promise - material-ui - module - visual-studio-2012 - outlook - web-applications - uwp - webview - firebase-authentication - jquery-mobile - utf-8 - datatable - python-requests - parallel-processing - colors - drop-down-menu - scipy - tfs - scroll - hive - count - syntax - ms-word - twitter-bootstrap-3 - ssis - fonts - rxjs - constructor - file-io - google-analytics - paypal - three.js - powerbi - cassandra - graphql - discord - graphics - compiler-errors - gwt - react-router - socket.io - backbone.js - solr - memory-leaks - url-rewriting - datatables - nlp - terraform - oauth - datagridview - drupal - zend-framework - oracle11g - knockout.js - triggers - interface - neural-network - django-forms - casting - angular-material - jmeter - linked-list - google-api - path - timer - django-templates - arduino - orm - windows-phone-7 - directory - proxy - parse-platform - visual-studio-2015 - cron - conditional-statements - push-notification - functional-programming - primefaces - pagination - model - jar - xamarin.android - hyperlink - uiview - visual-studio-2013 - vbscript - google-cloud-functions - azure-active-directory - gitlab - jwt - download - swift3 - sql-server-2005 - rspec - process - pygame - configuration - properties - callback - combobox - windows-phone-8 - linux-kernel - safari - scrapy - emacs - permissions - x86 - clojure - scripting - raspberry-pi - io - scope - azure-functions - compilation - mongodb-query - responsive-design - expo - nhibernate - angularjs-directive - reference - binding - bluetooth - request - architecture - dns - playframework - 3d - pyqt - version-control - discord.js - doctrine-orm - package - f\# - rubygems - get - sql-server-2012 - autocomplete - tree - datepicker - openssl - kendo-ui - jackson - yii - controller - grep - nested - xamarin.ios - static - null - transactions - statistics - datagrid - active-directory - uiviewcontroller - dockerfile - webforms - sas - discord.py - computer-vision - phpmyadmin - notifications - duplicates - pycharm - mocking - youtube - nullpointerexception - yaml - menu - blazor - sum - plotly - bitmap - visual-studio-2008 - asp.net-mvc-5 - floating-point - yii2 - css-selectors - stl - android-listview - electron - jsf-2 - time-series - cryptography - ant - hashmap - character-encoding - msbuild - stream - asp.net-core-mvc - sdk - google-drive-api - selenium-chromedriver - jboss - joomla - devise - navigation - cuda - cors - frontend - anaconda - background - multiprocessing - binary - pyqt5 - camera - iterator - linq-to-sql - mariadb - onclick - ios7 - android-jetpack-compose - microsoft-graph-api - android-asynctask - rabbitmq - tabs - amazon-dynamodb - laravel-4 - environment-variables - uicollectionview - insert - linker - coldfusion - xsd - console - continuous-integration - upload - textview - ftp - opengl-es - macros - operating-system - mockito - localization - formatting - xml-parsing - json.net - type-conversion - vuejs3 - data.table - kivy - timestamp - integer - calendar - segmentation-fault - android-ndk - prolog - char - drag-and-drop - crash - jasmine - azure-pipelines - automated-tests - dependencies - geometry - fortran - android-gradle-plugin - itext - sprite-kit - mfc - header - attributes - nosql - format - firebase-cloud-messaging - nuxt.js - odoo - db2 - jquery-plugins - event-handling - julia - jenkins-pipeline - leaflet - annotations - flutter-layout - keyboard - nestjs - postman - arm - textbox - stripe-payments - visual-studio-2017 - gulp - libgdx - uikit - timezone - synchronization - azure-web-app-service - dom-events - wso2 - google-sheets-formula - xampp - crystal-reports - aggregation-framework - namespaces - android-emulator - uiscrollview - swagger - jvm - sequelize.js - chart.js - com - snowflake-cloud-data-platform - subprocess - html5-canvas - geolocation - garbage-collection - webdriver - sql-update - centos - dialog - concatenation - numbers - widget - qml - tuples - set - java-stream - mapreduce - ionic2 - smtp - windows-10 - rotation - android-edittext - nuget - modal-dialog - spring-data - radio-button - doctrine - http-headers - grid - lucene - sonarqube - xmlhttprequest - listbox - initialization - switch-statement - internationalization - boolean - components - apache-camel - google-play - gdb - serial-port - ios5 - return - ldap - youtube-api - pivot - eclipse-plugin - latex - frameworks - tags - containers - c++17 - github-actions - subquery - embedded - dataset - foreign-keys - asp-classic - label - uinavigationcontroller - delegates - copy - google-cloud-storage - struts2 - migration - protractor - base64 - uibutton - find - queue - sql-server-2008-r2 - arguments - composer-php - append - jaxb - stack - tailwind-css - zip - cucumber - autolayout - ide - entity-framework-6 - iteration - popup - r-markdown - windows-7 - vb6 - airflow - g++ - clang - hover - ssl-certificate - jqgrid - range - gmail Next You’ll be prompted to create an account to view your personalized homepage. 1. 1. [Home](https://stackoverflow.com/) 2. [Questions](https://stackoverflow.com/questions) 3. [AI Assist](https://stackoverflow.com/ai-assist) 4. [Tags](https://stackoverflow.com/tags) 5. [Challenges](https://stackoverflow.com/beta/challenges) 6. [Chat](https://chat.stackoverflow.com/?tab=explore) 7. [Articles](https://stackoverflow.blog/contributed?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=so-blog&utm_content=experiment-articles) 8. [Users](https://stackoverflow.com/users) 9. [Companies](https://stackoverflow.com/jobs/companies?so_medium=stackoverflow&so_source=SiteNav) 10. [Collectives]() 11. Communities for your favorite technologies. [Explore all Collectives](https://stackoverflow.com/collectives-all) 2. Stack Internal Stack Overflow for Teams is now called **Stack Internal**. Bring the best of human thought and AI automation together at your work. [Try for free](https://stackoverflowteams.com/teams/create/free/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams) 3. [Stack Internal]() 4. Bring the best of human thought and AI automation together at your work. [Learn more](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact) ##### Collectives™ on Stack Overflow Find centralized, trusted content and collaborate around the technologies you use most. [Learn more about Collectives](https://stackoverflow.com/collectives) **Stack Internal** Knowledge at work Bring the best of human thought and AI automation together at your work. [Explore Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=side-bar&utm_content=explore-teams-compact-popover) # [How do I revert a Git repository to a previous commit?](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit) [Ask Question](https://stackoverflow.com/questions/ask) Asked 15 years, 4 months ago Modified [1 year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?lastactivity "2025-02-23 12:21:56Z") Viewed 12.8m times This question shows research effort; it is useful and clear 7607 Save this question. Show activity on this post. **This question's answers are a [community effort](https://stackoverflow.com/help/privileges/edit-community-wiki)**. Edit existing answers to improve this post. It is not currently accepting new answers or interactions. How do I revert from my current state to a snapshot made on a certain commit? If I do `git log`, then I get the following output: ``` $ git log commit a867b4af366350be2e7c21b8de9cc6504678a61b` Author: Me <me@me.com> Date: Thu Nov 4 18:59:41 2010 -0400 blah blah blah... commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4 Author: Me <me@me.com> Date: Thu Nov 4 05:13:39 2010 -0400 more blah blah blah... commit 0766c053c0ea2035e90f504928f8df3c9363b8bd Author: Me <me@me.com> Date: Thu Nov 4 00:55:06 2010 -0400 And yet more blah blah... commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50 Author: Me <me@me.com> Date: Wed Nov 3 23:56:08 2010 -0400 Yep, more blah blah. ``` How do I revert to the commit from November 3, i.e. commit `0d1d7fc`? - [git](https://stackoverflow.com/questions/tagged/git "show questions tagged 'git'") - [git-checkout](https://stackoverflow.com/questions/tagged/git-checkout "show questions tagged 'git-checkout'") - [git-reset](https://stackoverflow.com/questions/tagged/git-reset "show questions tagged 'git-reset'") - [git-revert](https://stackoverflow.com/questions/tagged/git-revert "show questions tagged 'git-revert'") [Share](https://stackoverflow.com/q/4114095 "Short permalink to this question") Share a link to this question Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") Follow Follow this question to receive notifications [edited Dec 16, 2019 at 13:02](https://stackoverflow.com/posts/4114095/revisions "show all edits to this post") [![Paolo's user avatar](https://www.gravatar.com/avatar/895c1539b3062a1feadfad9d1d80e8e9?s=64&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/3390419/paolo) [Paolo](https://stackoverflow.com/users/3390419/paolo) 26\.7k88 gold badges5353 silver badges8888 bronze badges asked Nov 6, 2010 at 16:58 [![Crazy Serb's user avatar](https://www.gravatar.com/avatar/94f2f4d10581cebd4432a10f66fec19f?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/111174/crazy-serb) [Crazy Serb](https://stackoverflow.com/users/111174/crazy-serb) 76\.3k88 gold badges3737 silver badges4747 bronze badges 3 - 10 Related [How to undo the last Git commit?](http://stackoverflow.com/q/927358/456814). user456814 – user456814 2014-05-23 17:57:12 +00:00 [Commented May 23, 2014 at 17:57](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment36675816_4114095) - 116 Here's [a very clear and thorough post](https://github.com/blog/2019-how-to-undo-almost-anything-with-git) about undoing things in git, straight from Github. Aurelio – [Aurelio](https://stackoverflow.com/users/1446845/aurelio "25,952 reputation") 2015-06-08 19:41:52 +00:00 [Commented Jun 8, 2015 at 19:41](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment49492986_4114095) - 58 I love git, but the fact that there's 35 answers to something that should be incredibly simple exposes a huge issue with git. Or is it the docs? The Muffin Man – [The Muffin Man](https://stackoverflow.com/users/445303/the-muffin-man "20,002 reputation") 2018-01-03 22:26:59 +00:00 [Commented Jan 3, 2018 at 22:26](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment83147073_4114095) Comments disabled on deleted / locked posts / reviews \| ## 41 Answers 41 Sorted by: [Reset to default](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?answertab=scoredesc#tab-top) 1 [2](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?page=2&tab=scoredesc#tab-top "Go to page 2") [Next](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?page=2&tab=scoredesc#tab-top "Go to page 2") This answer is useful 12578 Save this answer. Show activity on this post. This depends a lot on what you mean by "revert". ## Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: ``` Copy ``` Or if you want to make commits while you're there, go ahead and make a new branch while you're at it: ``` git checkout -b old-state 0d1d7fc32 ``` To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.) ## Hard delete unpublished commits If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset: ``` Copy ``` If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again. ## Undo published commits with new commits On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history. First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits. ``` # This lists all merge commits between 0d1d7fc and HEAD: git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' # This lists all non merge commits between 0d1d7fc and HEAD: git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' ``` Note: if you revert multiple commits, the order matters. Start with the most recent commit. ``` Copy ``` The [`git-revert` manpage](https://git-scm.com/docs/git-revert) actually covers a lot of this in its description. Another useful link is [this git-scm.com section discussing git-revert](https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_undoing_merges). If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section). You may also find this answer helpful in this case: [How can I move HEAD back to a previous location? (Detached head) & Undo commits](https://stackoverflow.com/questions/34519665/how-to-move-head-forward-checkout-revet-reflog-reset/34519716#34519716) [Share](https://stackoverflow.com/a/4114122 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/4114122/edit) Follow Follow this answer to receive notifications [edited Mar 6, 2024 at 15:03](https://stackoverflow.com/posts/4114122/revisions "show all edits to this post") [![Sicco's user avatar](https://www.gravatar.com/avatar/ea0e086452e5359a7657efddec94e02d?s=64&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/1266303/sicco) [Sicco](https://stackoverflow.com/users/1266303/sicco) 6,33955 gold badges4949 silver badges6363 bronze badges answered Nov 6, 2010 at 17:04 [![Cascabel's user avatar](https://i.sstatic.net/tBCc0.jpg?s=64)](https://stackoverflow.com/users/119963/cascabel) [Cascabel](https://stackoverflow.com/users/119963/cascabel) 503k7676 gold badges385385 silver badges320320 bronze badges Sign up to request clarification or add additional context in comments. ## 18 Comments Add a comment [![](https://i.sstatic.net/ZKsgT.jpg?s=64)](https://stackoverflow.com/users/263858/new-alexandria) New Alexandria [New Alexandria](https://stackoverflow.com/users/263858/new-alexandria) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment16132219_4114122) [@Rod's comment](http://stackoverflow.com/questions/4114095/git-revert-to-previous-commit-how/7760865#comment11806784_7760865) on `git revert HEAD~3` as the best wat to revert back `3` commits is am important convention. 2012-08-22T15:16:45.317Z+00:00 208 Reply - Copy link [![](https://www.gravatar.com/avatar/96653d600af48517c03480353258cc71?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/800011/spoeken) Spoeken [Spoeken](https://stackoverflow.com/users/800011/spoeken) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment18820010_4114122) Could you write the whole number? like: `git reset --hard 0d1d7fc32e5a947fbd92ee598033d85bfc445a50` 2012-12-04T13:58:13.573Z+00:00 38 Reply - Copy link [![](https://i.sstatic.net/tBCc0.jpg?s=64)](https://stackoverflow.com/users/119963/cascabel) Cascabel [Cascabel](https://stackoverflow.com/users/119963/cascabel) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment18825947_4114122) @MathiasMadsenStav Yes, you can of course specify commits by the full SHA1. I used abbreviated hashes to make the answer more readable, and you also tend to use them if you're typing out. If you're copying and pasting, by all means use the full hash. See [Specifying Revisions in man git rev-parse](http://www.kernel.org/pub/software/scm/git/docs/git-rev-parse.html#_specifying_revisions) for a full description of how you can name commits. 2012-12-04T16:55:52.413Z+00:00 22 Reply - Copy link [![](https://www.gravatar.com/avatar/47baa383a908353af6d0535c7cd09984?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/828095/mirko-akov) Mirko Akov [Mirko Akov](https://stackoverflow.com/users/828095/mirko-akov) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment28036980_4114122) You can use `git revert --no-commit hash1 hash2 ...` and after this just commit every single revert in one commit `git commit -m "Message"` 2013-09-24T12:12:16.757Z+00:00 84 Reply - Copy link [![](https://i.sstatic.net/tYhmf.jpg?s=64)](https://stackoverflow.com/users/5289570/wassadamo) Wassadamo [Wassadamo](https://stackoverflow.com/users/5289570/wassadamo) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment139519429_4114122) @New Alexandria to undo the previous commit (ie revert back 1 commit) I used `git revert HEAD~0` was off-by-one at first. 2024-10-24T22:57:42.637Z+00:00 1 Reply - Copy link Add a comment \| Show 13 more comments This answer is useful 3965 Save this answer. \+50 This answer has been awarded bounties worth 50 reputation by Eli Courtwright Show activity on this post. Lots of complicated and dangerous answers here, but it's actually easy: ``` git revert --no-commit 0d1d7fc3..HEAD git commit git push ``` This will revert everything from the HEAD back to the commit hash (excluded), meaning it will recreate that commit state in the working tree *as if* every commit after `0d1d7fc3` had been walked back. You can then commit the current tree, and it will create a brand new commit essentially equivalent to the commit you "reverted" to. (The `--no-commit` flag lets git revert all the commits at once- otherwise you'll be prompted for a message for each commit in the range, littering your history with unnecessary new commits.) This is a **safe and easy way to rollback to a previous state**. No history is destroyed, so it can be used for commits that have already been made public. *** **Note on merge commits:** If one of the commits between 0766c053..HEAD (inclusive) is a merge then there will be an error popping up (to do with no -m specified). The following link may help those encountering that: [Why does git revert complain about a missing -m option?](https://stackoverflow.com/questions/5970889/why-does-git-revert-complain-about-a-missing-m-option) (thanks @timhc22 for pointing out) [Share](https://stackoverflow.com/a/21718540 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/21718540/edit) Follow Follow this answer to receive notifications [edited Nov 24, 2024 at 14:23](https://stackoverflow.com/posts/21718540/revisions "show all edits to this post") [![Atul's user avatar](https://www.gravatar.com/avatar/a013447678c2adcff7adbc4af467c4e4?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1911652/atul) [Atul](https://stackoverflow.com/users/1911652/atul) 4,58366 gold badges5555 silver badges109109 bronze badges answered Feb 12, 2014 at 4:18 [![Yarin's user avatar](https://www.gravatar.com/avatar/e43e620f96a26bf6ee7fa70750c0302a?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/165673/yarin) [Yarin](https://stackoverflow.com/users/165673/yarin) 186k156156 gold badges414414 silver badges532532 bronze badges ## 15 Comments Add a comment user456814 user456814 [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment37873063_21718540) If you really do want to have individual commits (instead of reverting everything with one big commit), then you can pass `--no-edit` instead of `--no-commit`, so that you don't have to edit a commit message for each reversion. 2014-06-28T20:11:40.217Z+00:00 49 Reply - Copy link [![](https://i.sstatic.net/EDx3nVpZ.jpg?s=64)](https://stackoverflow.com/users/1017550/scorpio1441) scorpio1441 [scorpio1441](https://stackoverflow.com/users/1017550/scorpio1441) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment64792735_21718540) `$ git revert --no-commit 53742ae..HEAD` returns `fatal: empty commit set passed` 2016-08-01T20:30:18.953Z+00:00 50 Reply - Copy link [![](https://i.sstatic.net/uRfwU.jpg?s=64)](https://stackoverflow.com/users/20035351/nuser137) nuser137 [nuser137](https://stackoverflow.com/users/20035351/nuser137) [Aug 3, 2025 at 7:15](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment140638411_21718540) If you remove `..HEAD`, it works fine. 2025-08-03T07:15:37.58Z+00:00 1 Reply - Copy link [![](https://www.gravatar.com/avatar/56655dc56d01bd75a3c4fdc2ff9f6c2e?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/3267317/wensiso) wensiso [wensiso](https://stackoverflow.com/users/3267317/wensiso) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment121517141_21718540) If you remove that '..HEAD' at the end of the command, you can remove only a specific commit. For example: `git revert --no-commit 0766c053` will remove only the specific changes made for 0766c053 keeping all changes after 0766c053 untouched. 2021-08-12T14:22:02.147Z+00:00 17 Reply - Copy link [![](https://www.gravatar.com/avatar/e7ad6fb5dc2f10279b43129570737497?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/4135063/pedro-a) Pedro A [Pedro A](https://stackoverflow.com/users/4135063/pedro-a) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment124084389_21718540) As @timhc22 mentioned, this doesn't work if there is one or more merge commits in between (which can happen frequently). An answer that really works in all cases and is just as safe is here: [stackoverflow.com/a/15563149/4135063](https://stackoverflow.com/a/15563149/4135063) 2021-12-02T03:36:43.347Z+00:00 5 Reply - Copy link [![](https://www.gravatar.com/avatar/991c4e6d0872e18880e3e0ff51cdf5a2?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1541397/david-callanan) David Callanan [David Callanan](https://stackoverflow.com/users/1541397/david-callanan) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment124203377_21718540) Note that if you want to revert to the previous commit without the commit ID you can use `git revert --no-commit HEAD~1..HEAD` and if you want to revert the last 5 commits you can do `git revert --no-commit HEAD~5..HEAD` 2021-12-07T14:28:15.967Z+00:00 7 Reply - Copy link Add a comment \| Show 9 more comments This answer is useful 2163 Save this answer. Show activity on this post. ## Rogue Coder? Working on your own and just want it to work? Follow these instructions below, they’ve worked reliably for me and many others for years. Working with others? Git is complicated. Read the comments below this answer, consider other answers, and discuss with your team before you do something rash. ### Reverting Working Copy to Most Recent Commit To revert to the previous commit, ignoring any changes: ``` git reset --hard HEAD ``` where HEAD is the last commit in your current branch ### Reverting The Working Copy to an Older Commit To revert to a commit that's older than the most recent commit: ``` Copy ``` Credits go to a similar Stack Overflow question, *[Revert to a commit by a SHA hash in Git?](https://stackoverflow.com/questions/1895059/git-revert-to-a-commit-by-sha-hash)*. [Share](https://stackoverflow.com/a/12049323 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/12049323/edit) Follow Follow this answer to receive notifications [edited Dec 7, 2022 at 13:33](https://stackoverflow.com/posts/12049323/revisions "show all edits to this post") answered Aug 21, 2012 at 6:19 [![boulder\_ruby's user avatar](https://www.gravatar.com/avatar/7149d2bc15314277ac69357ef7251650?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1276506/boulder-ruby) [boulder\_ruby](https://stackoverflow.com/users/1276506/boulder-ruby) 40k99 gold badges8282 silver badges104104 bronze badges ## 8 Comments Add a comment [![](https://www.gravatar.com/avatar/f618e05e02566e66784439877f82f1db?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/480522/lennon) Lennon [Lennon](https://stackoverflow.com/users/480522/lennon) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment16934916_12049323) I did that, but then I wasn't able to commit and push to the remote repository. I want a specific older commit to become HEAD... 2012-09-24T18:17:28.717Z+00:00 57 Reply - Copy link [![](https://www.gravatar.com/avatar/4e7cef738181a09b006a426bca052cc3?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/27963/entropo) entropo [entropo](https://stackoverflow.com/users/27963/entropo) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment121892239_12049323) @Lennon. Say you made a change, committed, and pushed it. You want both local and remote to appear as though this never happened. First `git reset --hard HEAD^` You've now blown away all local changes from the last commit. Then: `git push --force origin HEAD` This takes the current HEAD commit in local and overwrites the HEAD in the remote, removing the last commit. Note: This isn't a secure way to delete secrets accidentally pushed to a remote. Assume all secrets are compromised then See caveats for '--force': [evilmartians.com/chronicles/…](https://evilmartians.com/chronicles/git-push---force-and-how-to-deal-with-it) 2021-08-29T05:46:56.347Z+00:00 20 Reply - Copy link [![](https://i.sstatic.net/i96nQXj8.jpg?s=64)](https://stackoverflow.com/users/3317728/stefanbob) StefanBob [StefanBob](https://stackoverflow.com/users/3317728/stefanbob) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment129489445_12049323) OP asks for revert. Guy gives answer with reset 2022-08-11T15:40:54.767Z+00:00 1 Reply - Copy link [![](https://www.gravatar.com/avatar/7149d2bc15314277ac69357ef7251650?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1276506/boulder-ruby) boulder\_ruby [boulder\_ruby](https://stackoverflow.com/users/1276506/boulder-ruby) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment131791606_12049323) @StefanBob "git revert should be used to undo changes on a public branch, and git reset should be reserved for undoing changes on a private branch. You can also think of git revert as a tool for undoing committed changes, while git reset HEAD is for undoing uncommitted changes." 2022-12-03T14:38:11.95Z+00:00 3 Reply - Copy link [![](https://www.gravatar.com/avatar/fbd5e1846f7098ab0684fca9eebebe51?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/16952511/ngobin) NGobin [NGobin](https://stackoverflow.com/users/16952511/ngobin) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment135224623_12049323) Note if you do a git reset --hard HEAD, new untracked files and directories will be left behind. You can clean those up with `git clean -f -d` [as detailed here](https://stackoverflow.com/a/4327720/16952511). 2023-07-16T21:32:27.33Z+00:00 1 Reply - Copy link Add a comment \| Show 3 more comments This answer is useful 383 Save this answer. Show activity on this post. > **Note**: As mentioned in comments don't do this if you're sharing your branch with other people who have copies of the old commits ### 1\. Resetting Locally: Quick & Clean For a fast local cleanup, the Git reset approach is ideal. This command resets your working directory and removes untracked files: ``` git reset --hard <commitId> && git clean -f ``` This method has been my go-to option for its simplicity and effectiveness. ### 2\. Resetting Remote Branches to Erase Mistaken Commits If the unwanted commits have already been pushed to a remote repository, you’ll need to update the remote branch by force pushing: ``` git reset --hard <commitId> && git push origin <branchName> --force ``` This ensures that the remote branch reflects your reset state by removing the unwanted commits from the remote as well. In my workflow as a solo developer, I can safely use the forced method since there are no collaborators involved. **However, if you share your branch with others, consider using**: ``` git push --force-with-lease ``` This safer alternative helps prevent overwriting any new commits added remotely. ### 3\. An Interactive Alternative If you prefer a less drastic method for cleaning up untracked files, you can use the interactive clean command: ``` git clean -i ``` This command lets you review and selectively remove files, offering a safer way to manage your working directory. #### ⚠️ Important Considerations: - It is super important to NOT use `--force` if you’re sharing your branch with others. It rewrites history and can cause issues for teammates. Instead, consider `git push --force-with-lease` to avoid overwriting any new commits added remotely. - `git clean -f` removes untracked files but doesn’t affect commits. If you need a safer, interactive way to clean files, use: [Share](https://stackoverflow.com/a/19517124 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/19517124/edit) Follow Follow this answer to receive notifications [edited Feb 23, 2025 at 12:21](https://stackoverflow.com/posts/19517124/revisions "show all edits to this post") [![Luhn's user avatar](https://www.gravatar.com/avatar/e56ebc5ac08ca5a7d01f86e1f2ae5fea?s=64&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/4632878/luhn) [Luhn](https://stackoverflow.com/users/4632878/luhn) 90388 silver badges1717 bronze badges answered Oct 22, 2013 at 11:53 [![Pogrindis's user avatar](https://i.sstatic.net/vHeRE.png?s=64)](https://stackoverflow.com/users/1606432/pogrindis) [Pogrindis](https://stackoverflow.com/users/1606432/pogrindis) 8,37455 gold badges3434 silver badges4444 bronze badges ## 9 Comments Add a comment user456814 user456814 [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment37872931_19517124) **Obligatory Warning: don't do this** if you're sharing your branch with other people who have copies of the old commits, because using a hard reset like this will force them to have to resynchronize their work with the newly reset branch. **For a solution that explains in detail how to safely revert commits without losing work with a hard reset, [see this answer](http://stackoverflow.com/a/4114122/456814)**. 2014-06-28T20:03:54Z+00:00 71 Reply - Copy link [![](https://i.sstatic.net/XdMvJ.jpg?s=64)](https://stackoverflow.com/users/13062807/kat) kat [kat](https://stackoverflow.com/users/13062807/kat) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment124373029_19517124) It's worth noting this will permanently delete any uncommitted files in your source directory :/ 2021-12-15T04:58:18.71Z+00:00 4 Reply - Copy link [![](https://i.sstatic.net/ddeaM.jpg?s=64)](https://stackoverflow.com/users/5777189/babyishtank) BabyishTank [BabyishTank](https://stackoverflow.com/users/5777189/babyishtank) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment133648142_19517124) is it necessary for the git clean -i ? 2023-03-16T22:22:06.873Z+00:00 0 Reply - Copy link [![](https://www.gravatar.com/avatar/e56ebc5ac08ca5a7d01f86e1f2ae5fea?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/4632878/luhn) Luhn [Luhn](https://stackoverflow.com/users/4632878/luhn) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment139103133_19517124) For me `git reset --hard <commitId> && git push origin <branchName> --force` worked 2024-08-21T12:09:49.79Z+00:00 1 Reply - Copy link [![](https://i.sstatic.net/AfU4T.jpg?s=64)](https://stackoverflow.com/users/2523501/yeliabsalohcin) yeliabsalohcin [yeliabsalohcin](https://stackoverflow.com/users/2523501/yeliabsalohcin) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment139668273_19517124) @Luhn is right - you have to force the push in order for the remote (ie. github) to have the erroneous commit(s) removed. In my case as it's just me and my work, no branches, so `git push -f` was all that was necessary. Previously pushed rouge commit be gone\! 2024-11-19T22:08:19.153Z+00:00 1 Reply - Copy link Add a comment \| Show 4 more comments This answer is useful 319 Save this answer. Show activity on this post. Before answering let's add some background, explaining what this `HEAD` is. # ***First of all what is HEAD?*** `HEAD` is simply a reference to the current commit (latest) on the current branch. There can only be a single `HEAD` at any given time (excluding `git worktree`). The content of `HEAD` is stored inside `.git/HEAD`, and it contains the 40-bytes SHA-1 hash of the current commit. *** # ***`detached HEAD`*** If you are not on the latest commit - meaning that `HEAD` is pointing to a prior commit in history it's called ***`detached HEAD`***. [![Diagram illustrating the concept of detached HEAD](https://i.sstatic.net/OlavO.png)](https://i.sstatic.net/OlavO.png) On the command-line it will look like this - SHA-1 hash instead of the branch name since the `HEAD` is not pointing to the the tip of the current branch: [![Running git checkout HEAD^0 in a terminal](https://i.sstatic.net/U0l3s.png)](https://i.sstatic.net/U0l3s.png) *** ### A few options on how to recover from a detached HEAD: *** ### [`git checkout`](https://git-scm.com/docs/git-checkout) ``` git checkout <commit_id> git checkout -b <new branch> <commit_id> git checkout HEAD~X // x is the number of commits t go back ``` This will checkout new branch pointing to the desired commit. This command will checkout to a given commit. At this point you can create a branch and start to work from this point on: ``` # Checkout a given commit. # Doing so will result in a `detached HEAD` which mean that the `HEAD` # is not pointing to the latest so you will need to checkout branch # in order to be able to update the code. git checkout <commit-id> # Create a new branch forked to the given commit git checkout -b <branch name> ``` *** ### [`git reflog`](https://git-scm.com/docs/git-reflog) You can always use the `reflog` as well. `git reflog` will display any change which updated the `HEAD` and checking out the desired reflog entry will set the `HEAD` back to this commit. **Every time the HEAD is modified there will be a new entry in the `reflog`** ``` git reflog git checkout HEAD@{...} ``` This will get you back to your desired commit. [![Running git reflog in a terminal](https://i.sstatic.net/atW9w.png)](https://i.sstatic.net/atW9w.png) *** ### ***[`git reset HEAD --hard <commit_id>`](https://git-scm.com/docs/git-reset)*** "Move" your `HEAD` back to the desired commit. ``` Copy ``` - Note: ([Since Git 2.7](https://github.com/git/git/blob/master/Documentation/RelNotes/2.7.0.txt)) you can also use the `git rebase --no-autostash` as well. *** This schema illustrates which command does what. As you can see there `reset && checkout` modify the `HEAD`. [![Diagram illustrating staging area and checking out HEAD](https://i.sstatic.net/NuThL.png)](https://i.sstatic.net/NuThL.png) [Share](https://stackoverflow.com/a/28354830 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/28354830/edit) Follow Follow this answer to receive notifications [edited Nov 5, 2021 at 6:46](https://stackoverflow.com/posts/28354830/revisions "show all edits to this post") user17242583 answered Feb 5, 2015 at 21:56 [![CodeWizard's user avatar](https://i.sstatic.net/S6a50.jpg?s=64)](https://stackoverflow.com/users/1755598/codewizard) [CodeWizard](https://stackoverflow.com/users/1755598/codewizard) 146k2222 gold badges162162 silver badges181181 bronze badges ## 2 Comments Add a comment [![](https://i.sstatic.net/deSUR.jpg?s=64)](https://stackoverflow.com/users/3220554/phunsoft) phunsoft [phunsoft](https://stackoverflow.com/users/3220554/phunsoft) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment137228841_28354830) Shouldn't this diagrams show that `git reset` may modify the working tree (`git reset --hard`)? 2024-01-18T16:49:23.42Z+00:00 0 Reply - Copy link [![](https://lh5.googleusercontent.com/-EsnBLDR24HI/AAAAAAAAAAI/AAAAAAAABk0/yrJvOo0AP4E/s48-rj/photo.jpg)](https://stackoverflow.com/users/11207658/navjot-kashi) Navjot Kashi [Navjot Kashi](https://stackoverflow.com/users/11207658/navjot-kashi) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment138341463_28354830) git reflog, git reset --hard 0d1d7fc32, git push --force, works for me. 2024-05-13T09:16:35.417Z+00:00 1 Reply - Copy link This answer is useful 240 Save this answer. Show activity on this post. If you want to "uncommit", erase the last commit message, and put the modified files back in staging, you would use the command: ``` git reset --soft HEAD~1 ``` - `--soft` indicates that the uncommitted files should be retained as working files opposed to `--hard` which would discard them. - `HEAD~1` is the last commit. If you want to rollback 3 commits you could use `HEAD~3`. If you want to rollback to a specific revision number, you could also do that using its SHA hash. This is an extremely useful command in situations where you committed the wrong thing and you want to undo that last commit. Source: <http://nakkaya.com/2009/09/24/git-delete-last-commit/> [Share](https://stackoverflow.com/a/22178776 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/22178776/edit) Follow Follow this answer to receive notifications answered Mar 4, 2014 at 17:25 [![Stephen Ostermiller's user avatar](https://i.sstatic.net/HZ9CL.png?s=64)](https://stackoverflow.com/users/1145388/stephen-ostermiller) [Stephen Ostermiller](https://stackoverflow.com/users/1145388/stephen-ostermiller)♦ 25\.8k1818 gold badges9797 silver badges117117 bronze badges ## 2 Comments Add a comment [![](https://www.gravatar.com/avatar/4e7cef738181a09b006a426bca052cc3?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/27963/entropo) entropo [entropo](https://stackoverflow.com/users/27963/entropo) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment121892378_22178776) Note: This only applies to your local repository. If you've pushed commits to a remote, just a reset locally will not change anything on the remotes. It is more complicated and dangerous and full of caveats to try to undo changes on a remote. 2021-08-29T06:04:14.71Z+00:00 5 Reply - Copy link [![](https://www.gravatar.com/avatar/4a1893b18ee404523fb4bf12479de892?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/15441/hawkeye) hawkeye [hawkeye](https://stackoverflow.com/users/15441/hawkeye) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment138924196_22178776) Also `git reset --soft HEAD~` 2024-07-26T01:12:03.927Z+00:00 0 Reply - Copy link This answer is useful 234 Save this answer. Show activity on this post. You can do this by the following two commands: ``` git reset --hard [previous Commit SHA id here] git push origin [branch Name] -f ``` It will remove your previous Git commit. If you want to keep your changes, you can also use: ``` git reset --soft [previous Commit SHA id here] ``` Then it will save your changes. [Share](https://stackoverflow.com/a/27438379 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/27438379/edit) Follow Follow this answer to receive notifications [edited Jul 3, 2016 at 6:30](https://stackoverflow.com/posts/27438379/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Dec 12, 2014 at 6:52 [![kiran boghra's user avatar](https://www.gravatar.com/avatar/da516bf182f6e570944acbd5089c480f?s=64&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/2968484/kiran-boghra) [kiran boghra](https://stackoverflow.com/users/2968484/kiran-boghra) 3,84222 gold badges2121 silver badges2323 bronze badges ## Comments Add a comment This answer is useful 161 Save this answer. Show activity on this post. The best way is: ``` git reset --hard <commidId> && git push --force ``` This will reset the branch to the specific commit and then will upload the remote server with the same commits as you have in local. Be careful with the `--force` flag as it removes all the subsequent commits after the selected commit without the option to recover them. [Share](https://stackoverflow.com/a/60399727 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/60399727/edit) Follow Follow this answer to receive notifications [edited Jan 13, 2021 at 16:16](https://stackoverflow.com/posts/60399727/revisions "show all edits to this post") answered Feb 25, 2020 at 17:00 [![david.t\_92's user avatar](https://i.sstatic.net/HLjIJ.png?s=64)](https://stackoverflow.com/users/3478780/david-t-92) [david.t\_92](https://stackoverflow.com/users/3478780/david-t-92) 2,05111 gold badge1212 silver badges1515 bronze badges ## Comments Add a comment This answer is useful 126 Save this answer. Show activity on this post. I have tried a lot of ways to revert local changes in Git, and it seems that this works the best if you just want to revert to the latest commit state. ``` git add . && git checkout master -f ``` Short description: - It will NOT create any commits as `git revert` does. - It will NOT detach your HEAD like `git checkout <commithashcode>` does. - It WILL override all your local changes and DELETE all added files since the last commit in the branch. - It works only with branches names, so you can revert only to latest commit in the branch this way. I found a much more convenient and simple way to achieve the results above: ``` git add . && git reset --hard HEAD ``` where HEAD points to the latest commit at you current branch. It is the same code code as boulder\_ruby suggested, but I have added `git add .` before `git reset --hard HEAD` to erase all new files created since the last commit since this is what most people expect I believe when reverting to the latest commit. [Share](https://stackoverflow.com/a/11708691 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/11708691/edit) Follow Follow this answer to receive notifications [edited Jun 24, 2014 at 20:08](https://stackoverflow.com/posts/11708691/revisions "show all edits to this post") [![franzwr's user avatar](https://i.sstatic.net/BzmNB.jpg?s=64)](https://stackoverflow.com/users/3741258/franzwr) [franzwr](https://stackoverflow.com/users/3741258/franzwr) 10655 bronze badges answered Jul 29, 2012 at 11:01 [![Roman Minenok's user avatar](https://www.gravatar.com/avatar/723d95dd98f23c410d02abaecbade75c?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/794755/roman-minenok) [Roman Minenok](https://stackoverflow.com/users/794755/roman-minenok) 9,37844 gold badges2828 silver badges2626 bronze badges ## Comments Add a comment This answer is useful 121 Save this answer. Show activity on this post. **OK,** going back to a previous commit in Git is quite easy... Revert back **without keeping** the changes: ``` git reset --hard <commit> ``` Revert back **with keeping** the changes: ``` git reset --soft <commit> ``` **Explanation:** using `git reset`, you can reset to a specific state. It's common using it with a commit hash as you see above. But as you see the difference is using the two flags `--soft` and `--hard`, by default `git reset` using `--soft` flag, but it's a good practice always using the flag, I explain each flag: *** ## \--soft The default flag as explained, not need to provide it, does not change the working tree, but it adds all changed files ready to commit, so you go back to the commit status which changes to files get unstaged. *** ## \--hard Be careful with this flag. It resets the working tree and all changes to tracked files and all will be gone\! *** I also created the image below that may happen in a real life working with Git: [![Git reset to a commit](https://i.sstatic.net/y6Xgj.png)](https://i.sstatic.net/y6Xgj.png) [Share](https://stackoverflow.com/a/45219734 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/45219734/edit) Follow Follow this answer to receive notifications [edited Jan 26, 2020 at 17:28](https://stackoverflow.com/posts/45219734/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Jul 20, 2017 at 15:55 [![Alireza's user avatar](https://i.sstatic.net/WtfBG.png?s=64)](https://stackoverflow.com/users/5423108/alireza) [Alireza](https://stackoverflow.com/users/5423108/alireza) 106k2727 gold badges280280 silver badges173173 bronze badges ## 1 Comment Add a comment [![](https://i.sstatic.net/GNroX.png?s=64)](https://stackoverflow.com/users/15273968/the-hutt) the Hutt [the Hutt](https://stackoverflow.com/users/15273968/the-hutt) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment127416075_45219734) `--soft` doesn't loose the changes. That's very important!\! 2022-05-04T12:03:45.977Z+00:00 1 Reply - Copy link This answer is useful 89 Save this answer. Show activity on this post. Assuming you're talking about master and on that respective branch (that said, this could be any working branch you're concerned with): ``` # Reset local master branch to November 3rd commit ID git reset --hard 0d1d7fc32e5a947fbd92ee598033d85bfc445a50 # Reset remote master branch to November 3rd commit ID git push -f origin 0d1d7fc32e5a947fbd92ee598033d85bfc445a50:master ``` I found the answer from in a blog post (now no longer exists) Note that this is Resetting and Forcing the change to the remote, so that if others on your team have already git pulled, you will cause problems for them. You are destroying the change history, which is an important reason why people use git in the first place. Better to use revert (see other answers) than reset. If you're a one man team then it probably doesn't matter. [Share](https://stackoverflow.com/a/37145089 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/37145089/edit) Follow Follow this answer to receive notifications [edited Apr 28, 2019 at 19:59](https://stackoverflow.com/posts/37145089/revisions "show all edits to this post") [![rmcsharry's user avatar](https://www.gravatar.com/avatar/fe67e29bbbb733dedab9bdf075a9ff38?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1028679/rmcsharry) [rmcsharry](https://stackoverflow.com/users/1028679/rmcsharry) 5,5911111 gold badges7474 silver badges119119 bronze badges answered May 10, 2016 at 17:21 [![markreyes's user avatar](https://www.gravatar.com/avatar/7099b7b5392c9c5f63009d592b211602?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1880601/markreyes) [markreyes](https://stackoverflow.com/users/1880601/markreyes) 1,28911 gold badge1515 silver badges2727 bronze badges ## Comments Add a comment This answer is useful 77 Save this answer. Show activity on this post. ## Extra Alternatives to Jefromi's Solutions [Jefromi's solutions](https://stackoverflow.com/a/4114122/456814) are definitely the best ones, and you should definitely use them. However, for the sake of completeness, I also wanted to show these other alternative solutions that can also be used to revert a commit (in the sense that you **create a new commit that undoes changes in previous commit**, just like what `git revert` does). To be clear, these alternatives ***are not the best way to revert commits***, [Jefromi's solutions are](https://stackoverflow.com/a/4114122/456814), but I just want to point out that you can also use these other methods to achieve the same thing as `git revert`. ## Alternative 1: Hard and Soft Resets This is a very slightly modified version of Charles Bailey's solution to [Revert to a commit by a SHA hash in Git?](https://stackoverflow.com/questions/1895059/revert-to-a-commit-by-sha-hash-in-git/1895095#1895095): ``` Copy ``` This basically works by using the fact that soft resets will leave the state of the previous commit staged in the index/staging-area, which you can then commit. ## Alternative 2: Delete the Current Tree and Replace with the New One This solution comes from svick's solution to [Checkout old commit and make it a new commit](https://stackoverflow.com/questions/3380805/checkout-old-commit-and-make-it-a-new-commit/3382249#3382249): ``` git rm -r . git checkout <commit> . git commit ``` Similarly to alternative \#1, this reproduces the state of `<commit>` in the current working copy. It is necessary to do `git rm` first because `git checkout` won't remove files that have been added since `<commit>`. [Share](https://stackoverflow.com/a/24478674 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/24478674/edit) Follow Follow this answer to receive notifications [edited May 23, 2017 at 11:47](https://stackoverflow.com/posts/24478674/revisions "show all edits to this post") community wiki [2 revs user456814](https://stackoverflow.com/posts/24478674/revisions "show revision history for this post") ## Comments Add a comment This answer is useful 69 Save this answer. Show activity on this post. Say you have the following commits in a text file named `~/commits-to-revert.txt` (I used `git log --pretty=oneline` to get them) ``` fe60adeba6436ed8f4cc5f5c0b20df7ac9d93219 0c27ecfdab3cbb08a448659aa61764ad80533a1b f85007f35a23a7f29fa14b3b47c8b2ef3803d542 e9ec660ba9c06317888f901e3a5ad833d4963283 6a80768d44ccc2107ce410c4e28c7147b382cd8f 9cf6c21f5adfac3732c76c1194bbe6a330fb83e3 fff2336bf8690fbfb2b4890a96549dc58bf548a5 1f7082f3f52880cb49bc37c40531fc478823b4f5 e9b317d36a9d1db88bd34831a32de327244df36a f6ea0e7208cf22fba17952fb162a01afb26de806 137a681351037a2204f088a8d8f0db6e1f9179ca ``` Create a [Bash](http://en.wikipedia.org/wiki/Bash_%28Unix_shell%29) shell script to revert each of them: ``` Copy ``` This reverts everything back to the previous state, including file and directory creations, and deletions, commit it to your branch and you retain the history, but you have it reverted back to the same file structure. Why Git doesn't have a `git revert --to <hash>` is beyond me. [Share](https://stackoverflow.com/a/7760865 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/7760865/edit) Follow Follow this answer to receive notifications [edited Jun 29, 2014 at 0:13](https://stackoverflow.com/posts/7760865/revisions "show all edits to this post") user456814 answered Oct 13, 2011 at 21:51 [![Lance Caraccioli's user avatar](https://www.gravatar.com/avatar/2e5dceec69f65d9bc77287a6a80f0e85?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/415144/lance-caraccioli) [Lance Caraccioli](https://stackoverflow.com/users/415144/lance-caraccioli) 1,4291313 silver badges1414 bronze badges ## Comments Add a comment This answer is useful 67 Save this answer. Show activity on this post. Here is a **much simpler** way to go back to a previous commit (and have it in an uncommited state, to do with it whatever you like): ``` git reset HEAD~1 ``` So, no need for commit ids and so on :) [Share](https://stackoverflow.com/a/35695065 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/35695065/edit) Follow Follow this answer to receive notifications [edited Jul 3, 2016 at 10:56](https://stackoverflow.com/posts/35695065/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Feb 29, 2016 at 8:40 [![Paul Walczewski's user avatar](https://www.gravatar.com/avatar/2736b7105d34dbe90cfcf6986b92b5d4?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/2185898/paul-walczewski) [Paul Walczewski](https://stackoverflow.com/users/2185898/paul-walczewski) 1,3361010 silver badges1313 bronze badges ## 2 Comments Add a comment [![](https://i.sstatic.net/ddeaM.jpg?s=64)](https://stackoverflow.com/users/5777189/babyishtank) BabyishTank [BabyishTank](https://stackoverflow.com/users/5777189/babyishtank) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment127525307_35695065) This move all the changes back into the stage area, and you still have to do extra work to get rid of them 2022-05-09T19:02:43.917Z+00:00 0 Reply - Copy link [![](https://i.sstatic.net/uRfwU.jpg?s=64)](https://stackoverflow.com/users/20035351/nuser137) nuser137 [nuser137](https://stackoverflow.com/users/20035351/nuser137) [Aug 3, 2025 at 7:42](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment140638439_35695065) After doing that, remember to `git push --force`; otherwise, it gives a "rejection error". 2025-08-03T07:42:41.313Z+00:00 0 Reply - Copy link This answer is useful 67 Save this answer. Show activity on this post. > **Caution\!** This command can cause losing commit history, if user put the wrong commit mistakenly. Always have en extra backup of your git some where else just in case if you do mistakes, than you are a bit safer. :) I have had a similar issue and wanted to revert back to an earlier commit. In my case I was not interested to keep the newer commit, hence I used `Hard`. This is how I did it: ``` git reset --hard CommitId && git clean -f ``` This will revert on the local repository, and here after using `git push -f` will update the remote repository. ``` git push -f ``` *** For instance, if you want to completely ignore the commit with the name `enforce non-group manage policies` from the next image [![enter image description here](https://i.sstatic.net/JyrDu.png)](https://i.sstatic.net/JyrDu.png) you'd run ``` git reset --hard dd52eb9 && git clean -f ``` followed by ``` git push -f ``` After, you won't see that commit (`enforce non-group manage policies`) there [![enter image description here](https://i.sstatic.net/Dkljl.png)](https://i.sstatic.net/Dkljl.png) [Share](https://stackoverflow.com/a/48152125 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/48152125/edit) Follow Follow this answer to receive notifications [edited Mar 23, 2021 at 12:57](https://stackoverflow.com/posts/48152125/revisions "show all edits to this post") [![Tiago Peres's user avatar](https://i.sstatic.net/J1BafE2C.jpg?s=64)](https://stackoverflow.com/users/5675325/tiago-peres) [Tiago Peres](https://stackoverflow.com/users/5675325/tiago-peres) 15\.9k2222 gold badges106106 silver badges176176 bronze badges answered Jan 8, 2018 at 14:15 [![Maytham Fahmi's user avatar](https://www.gravatar.com/avatar/7f5cbe13d76e559198ea6857799b044b?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/3088349/maytham-fahmi) [Maytham Fahmi](https://stackoverflow.com/users/3088349/maytham-fahmi) 33\.2k1717 gold badges134134 silver badges159159 bronze badges ## Comments Add a comment This answer is useful 62 Save this answer. Show activity on this post. You can complete all these initial steps yourself and push back to the Git repository. 1. Pull the latest version of your repository from Bitbucket using the `git pull --all` command. 2. Run the Git log command with `-n 4` from your terminal. The number after the `-n` determines the number of commits in the log starting from the most recent commit in your local history. ``` $ git log -n 4 ``` 3. Reset the head of your repository's history using the `git reset --hard HEAD~N` where N is the number of commits you want to take the head back. In the following example the head would be set back one commit, to the last commit in the repository history: 4. Push the change to Git repository using `git push --force` to force push the change. If you want the Git repository to a previous commit:- ``` git pull --all git reset --hard HEAD~1 git push --force ``` [Share](https://stackoverflow.com/a/43255067 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/43255067/edit) Follow Follow this answer to receive notifications [edited Sep 27, 2021 at 14:11](https://stackoverflow.com/posts/43255067/revisions "show all edits to this post") answered Apr 6, 2017 at 12:20 [![Nanhe Kumar's user avatar](https://www.gravatar.com/avatar/64fdf4319b8c978adc6b30f30a9791b4?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1087040/nanhe-kumar) [Nanhe Kumar](https://stackoverflow.com/users/1087040/nanhe-kumar) 16\.5k55 gold badges8585 silver badges7272 bronze badges ## 1 Comment Add a comment [![](https://lh6.googleusercontent.com/-Qu21MmZcMgw/AAAAAAAAAAI/AAAAAAAAAAA/ABtNlbBGFmY1H59ls9eEMdsjGOCdsKmdsQ/mo/s48-rj/photo.jpg)](https://stackoverflow.com/users/10598138/javad-shirkhani) Javad Shirkhani [Javad Shirkhani](https://stackoverflow.com/users/10598138/javad-shirkhani) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment129823798_43255067) Simple and perfect solution 2022-08-28T07:33:04.903Z+00:00 0 Reply - Copy link This answer is useful 54 Save this answer. Show activity on this post. Revert is the command to rollback the commits. ``` git revert <commit1> <commit2> ``` *Sample:* ``` git revert 2h3h23233 ``` It is capable of taking range from the HEAD like below. Here 1 says "revert last commit." ``` git revert HEAD~1..HEAD ``` And then do: ``` git push ``` [Share](https://stackoverflow.com/a/32121369 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/32121369/edit) Follow Follow this answer to receive notifications [edited Jul 12, 2021 at 19:53](https://stackoverflow.com/posts/32121369/revisions "show all edits to this post") [![galoget's user avatar](https://i.sstatic.net/ypy8w.png?s=64)](https://stackoverflow.com/users/3961903/galoget) [galoget](https://stackoverflow.com/users/3961903/galoget) 72499 silver badges1515 bronze badges answered Aug 20, 2015 at 14:45 [![Sireesh Yarlagadda's user avatar](https://i.sstatic.net/LDiUuOdr.webp?s=64)](https://stackoverflow.com/users/2057902/sireesh-yarlagadda) [Sireesh Yarlagadda](https://stackoverflow.com/users/2057902/sireesh-yarlagadda) 13\.9k33 gold badges8080 silver badges7777 bronze badges ## 2 Comments Add a comment [![](https://www.gravatar.com/avatar/d88de3b7d952730bb2d580a0d6f822ad?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/6556994/mac) mac [mac](https://stackoverflow.com/users/6556994/mac) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment133959942_32121369) Wouldn't this create a new commit that lacks 2h3h23233? 2023-04-06T14:04:33.307Z+00:00 0 Reply - Copy link [![](https://www.gravatar.com/avatar/992305361a98940905dfb42338f42fa4?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/2597775/sebasira) sebasira [sebasira](https://stackoverflow.com/users/2597775/sebasira) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment134302276_32121369) This is the way to go to properly undo the last commit and keep things in order in remote also. Thank you\! 2023-05-02T13:33:21.807Z+00:00 0 Reply - Copy link This answer is useful 48 Save this answer. Show activity on this post. After all the changes, when you push all these commands, you might have to use: ``` git push -f ... ``` And not only `git push`. [Share](https://stackoverflow.com/a/18638479 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/18638479/edit) Follow Follow this answer to receive notifications [edited Sep 28, 2013 at 19:07](https://stackoverflow.com/posts/18638479/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Sep 5, 2013 at 14:03 [![sivi's user avatar](https://i.sstatic.net/kUf56.jpg?s=64)](https://stackoverflow.com/users/1984636/sivi) [sivi](https://stackoverflow.com/users/1984636/sivi) 11\.2k22 gold badges5757 silver badges5252 bronze badges ## Comments Add a comment This answer is useful 47 Save this answer. Show activity on this post. There is a command (not a part of core Git, but it is in the [git-extras](https://github.com/visionmedia/git-extras) package) specifically for reverting and staging old commits: ``` git undo ``` Per the [man page](https://en.wikipedia.org/wiki/Man_page), it can also be used as such: ``` # Remove the latest three commits git undo 3 ``` [Share](https://stackoverflow.com/a/18133419 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/18133419/edit) Follow Follow this answer to receive notifications [edited Jul 6, 2022 at 21:01](https://stackoverflow.com/posts/18133419/revisions "show all edits to this post") [![Chris Hayes's user avatar](https://i.sstatic.net/nEG5s.png?s=64)](https://stackoverflow.com/users/2096769/chris-hayes) [Chris Hayes](https://stackoverflow.com/users/2096769/chris-hayes) 14k99 gold badges4040 silver badges4747 bronze badges answered Aug 8, 2013 at 18:30 [![Shadow Man's user avatar](https://i.sstatic.net/0QXwK.jpg?s=64)](https://stackoverflow.com/users/2167531/shadow-man) [Shadow Man](https://stackoverflow.com/users/2167531/shadow-man) 3,47211 gold badge2727 silver badges3737 bronze badges ## 2 Comments Add a comment [![](https://i.sstatic.net/nEG5s.png?s=64)](https://stackoverflow.com/users/2096769/chris-hayes) Chris Hayes [Chris Hayes](https://stackoverflow.com/users/2096769/chris-hayes) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment128742702_18133419) May have since been removed or renamed? I don't see it documented it in their GitHub command list: [github.com/tj/git-extras/blob/master/Commands.md](https://github.com/tj/git-extras/blob/master/Commands.md) 2022-07-06T21:00:00.33Z+00:00 1 Reply - Copy link [![](https://i.sstatic.net/nEG5s.png?s=64)](https://stackoverflow.com/users/2096769/chris-hayes) Chris Hayes [Chris Hayes](https://stackoverflow.com/users/2096769/chris-hayes) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment128742725_18133419) Wait I think I found it, seems like it's now called `git undo` [github.com/tj/git-extras/blob/master/Commands.md\#git-undo](https://github.com/tj/git-extras/blob/master/Commands.md#git-undo) 2022-07-06T21:00:50.333Z+00:00 0 Reply - Copy link This answer is useful 41 Save this answer. Show activity on this post. **This solution does not require a force push and it works for merge commits too.** **Idea:** You basically want to replace the current working tree state with the one from a previous commit and then create a commit out of it. Ignored files should best be not changed. Here is how: 1. Emtpy the working tree \*. ``` git rm -r --cached . && git clean -f -d ``` 2. Bring the working tree in the state we want \*\*. ``` git checkout 0d1d7fc3 . ``` 3. Create the revert commit. ``` git add --all && git commit -m "revert to 0d1d7fc3" ``` *** It does not delete anything (pushed or upushed) from the history. It produces one clean commit which represents the state we want to revert back to. *** \* by removing untracked but not ignored files (the ones specified in *.gitignore*) from working tree. The working tree is empty except for the ignored files which we wanted to keep (if not specifiy `-x` option for `clean`) \*\* When a path is specified (here: `.`), checkout leaves HEAD alone. [Share](https://stackoverflow.com/a/58787607 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/58787607/edit) Follow Follow this answer to receive notifications [edited Feb 2, 2024 at 13:32](https://stackoverflow.com/posts/58787607/revisions "show all edits to this post") answered Nov 10, 2019 at 10:10 [![Willi Mentzel's user avatar](https://i.sstatic.net/OliiqTH1.jpg?s=64)](https://stackoverflow.com/users/1788806/willi-mentzel) [Willi Mentzel](https://stackoverflow.com/users/1788806/willi-mentzel) 30\.2k2121 gold badges120120 silver badges130130 bronze badges ## 5 Comments Add a comment [![](https://www.gravatar.com/avatar/508a4e2d4b18d033bd0733cbd1b65d7d?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1737333/cesar) Cesar [Cesar](https://stackoverflow.com/users/1737333/cesar) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment126931620_58787607) this is the only answer here that actually answers the question without resorting to force pushes 2022-04-11T13:33:03.357Z+00:00 3 Reply - Copy link [![](https://www.gravatar.com/avatar/78f0fa4ac1ca5eb09da399cab82e5b41?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/8578634/zim) Zim [Zim](https://stackoverflow.com/users/8578634/zim) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment130785277_58787607) This is the only answer here that is aware that `git reset --hard` won't remove untracked files. 2022-10-14T19:25:03.95Z+00:00 2 Reply - Copy link [![](https://www.gravatar.com/avatar/92ab9af5b2597efeee9eab0f0d782409?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/329496/simbo1905) simbo1905 [simbo1905](https://stackoverflow.com/users/329496/simbo1905) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment136655177_58787607) the only slight tweak i would make is to run a `git stash` first to backup anything that might be lying around before you blow things away to recover the old position. having tried the reset style approaches and had challenges this answer is now my go-to for how to forward fix with a revert of many commits. its a lifesaver. 2023-11-20T10:51:00.293Z+00:00 1 Reply - Copy link [![](https://i.sstatic.net/SBdfG.png?s=64)](https://stackoverflow.com/users/1964109/max-n) Max N [Max N](https://stackoverflow.com/users/1964109/max-n) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment137797003_58787607) Results in ! \[rejected\] HEAD -\> main (non-fast-forward) error: failed to push some refs to '[github.com/lalalla.git](https://github.com/lalalla.git)' hint: Updates were rejected because a pushed branch tip is behind its remote hint: counterpart. Check out this branch and integrate the remote changes hint: (e.g. 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details. 2024-03-14T16:46:22.633Z+00:00 0 Reply - Copy link [![](https://i.sstatic.net/OliiqTH1.jpg?s=64)](https://stackoverflow.com/users/1788806/willi-mentzel) Willi Mentzel [Willi Mentzel](https://stackoverflow.com/users/1788806/willi-mentzel) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment138799424_58787607) @MaxN you first need to pull. This has nothing to do with my answer. 2024-07-09T09:48:38.933Z+00:00 0 Reply - Copy link Add a comment This answer is useful 40 Save this answer. Show activity on this post. Try resetting to the desired commit: ``` git reset <COMMIT_ID> ``` To check `COMMIT_ID` use: ``` git log ``` This will reset all changed files to un-added state. Now you can `checkout` all un-added files by ``` git checkout . ``` To verify your changes use: ``` git log ``` **UPDATE** If you have **one and only** commit in your repo, try ``` git update-ref -d HEAD ``` [Share](https://stackoverflow.com/a/43826173 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/43826173/edit) Follow Follow this answer to receive notifications [edited Jul 13, 2021 at 13:42](https://stackoverflow.com/posts/43826173/revisions "show all edits to this post") [![galoget's user avatar](https://i.sstatic.net/ypy8w.png?s=64)](https://stackoverflow.com/users/3961903/galoget) [galoget](https://stackoverflow.com/users/3961903/galoget) 72499 silver badges1515 bronze badges answered May 6, 2017 at 23:03 [![optimistanoop's user avatar](https://i.sstatic.net/96dFL.jpg?s=64)](https://stackoverflow.com/users/3566045/optimistanoop) [optimistanoop](https://stackoverflow.com/users/3566045/optimistanoop) 95211 gold badge1111 silver badges1414 bronze badges ## Comments Add a comment This answer is useful 39 Save this answer. Show activity on this post. If the situation is an **urgent one**, and you just want to do what the questioner asked in a **quick and dirty** way, assuming your project is under a directory called, for example, "my project": *** **QUICK AND DIRTY**: depending on the circumstances, quick and dirty may in fact be very GOOD. What my solution here does is **NOT** replace irreversibly the files you have in your working directory with files hauled up/extracted from the depths of the git repository lurking beneath your .git/ directory using fiendishly clever and diabolically powerful git commands, of which there are many. **YOU DO NOT HAVE TO DO SUCH DEEP-SEA DIVING TO RECOVER what may appear to be a disastrous situation, and attempting to do so without sufficient expertise may prove fatal**. *** 1. Copy the whole directory and call it something else, like "my project - copy". Assuming your git repository ("repo") files are under the "my project" directory (the default place for them, under a directory called ".git"), you will now have copied both your work files and your repo files. 2. Do this in the directory "my project": ``` .../my project $ git reset --hard [first-4-letters&numbers-of-commit's-SHA] ``` This will return the state of the repo under "my project" to what it was when you made that commit (a "commit" means a snapshot of your working files). All commits since the "`reset`ted" commit will be lost forever under "my project", BUT... **they will still be present** in the repo under "my project - copy" since you copied *all* those files - including the ones in the repo, under .../.git/. You then have two versions on your system... you can examine or copy or modify files of interest, or whatever, from the previous commit. You can completely discard the files under "my project - copy", if you have decided the new work since the restored commit was going nowhere... The obvious thing if you want to carry on with the state of the project without actually discarding the work since this retrieved commit is to rename your directory again: Delete the project containing the retrieved commit (or give it a temporary name) and rename your "my project - copy" directory back to "my project". Then maybe try to understand some of the other answers here, and probably do another commit fairly soon. Git is a brilliant creation but absolutely no-one is able to just "pick it up on the fly": also people who try to explain it **far too often** assume prior knowledge of other VCS \[Version Control Systems\] and delve far too deep far too soon, and commit other terrible crimes, like using interchangeable terms for "checking out" - in ways which sometimes appear almost calculated to confuse a beginner. To save yourself much stress, learn from my scars. You have to pretty much have to read a book on Git - I'd recommend reading THE BOOK, *Pro Git 2nd edition*: available for free download etc. [from git central](https://git-scm.com/book/en/v2). Published 2014 but, as at early 2022, still the best. Do it sooner rather than later: Git is destined to be part of your life from now on. If you do, bear in mind that much of the complexity of Git comes from branching and then remerging: the *Pro Git* book actually introduces this central aspect very gently, but you can skip those parts in any book on your first read. From your question there's **no reason why people should be blinding you with science**. Especially if, for example, this is a desperate situation and you're a newbie with Git\! PS: (slight **caution**) One other thought: It is (now) actually quite simple to keep the Git repo in a directory other than the one with the working files. This would mean you would not copy the entire Git repository using the above quick & dirty solution. See the answer by Fryer using `--separate-git-dir` [here](https://stackoverflow.com/questions/505467/can-i-store-the-git-folder-outside-the-files-i-want-tracked). Bearing that in mind, **be warned**: If you have a "separate-directory" repository which you don't copy, and you do a hard reset, all versions subsequent to the reset commit really will be lost *forever* forever, unless you have, as you absolutely should, regularly backed up your repository, preferably to the Cloud (e.g. [Google Drive](https://en.wikipedia.org/wiki/Google_Drive)) among other places. On this subject of "backing up to the Cloud", the next step is to open an account (free of course) with GitHub or (better in my view) [GitLab](https://gitlab.com/). You can then regularly do a `git push` command to make your Cloud repo up-to-date "properly". But again, talking about this may be too much too soon: `git push` has to be configured, can fail to work for a totally baffling technical reason, involves learning about remote repos ("origin", etc). So a quick-and-dirty Cloud-based backup approach may be preferable until you become knowledgeable. Again, the *Pro Git* book introduces how remote repositories work, and relate to your local repo, very gently and rationally. [Share](https://stackoverflow.com/a/29151326 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/29151326/edit) Follow Follow this answer to receive notifications [edited Feb 2, 2022 at 11:26](https://stackoverflow.com/posts/29151326/revisions "show all edits to this post") answered Mar 19, 2015 at 17:33 [![mike rodent's user avatar](https://www.gravatar.com/avatar/de73cca7e15d1d8feb243f3c125dc3ca?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/595305/mike-rodent) [mike rodent](https://stackoverflow.com/users/595305/mike-rodent) 16\.2k1515 gold badges121121 silver badges205205 bronze badges ## 1 Comment Add a comment [![](https://www.gravatar.com/avatar/8f0ced6387609d34ea274936f598f0b7?s=48&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/434537/rollsch) rollsch [rollsch](https://stackoverflow.com/users/434537/rollsch) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment135451747_29151326) I regularly do this. Copy the entire git repo to another folder, then you are free to experiment and mess with the git repo (and break things) without any risk (assuming you don't push). A good way to learn git, you get as many tries as you need until you understand what you are trying to do. 2023-08-04T03:32:14.81Z+00:00 0 Reply - Copy link This answer is useful 36 Save this answer. Show activity on this post. Select your required commit, and check it by ``` git show HEAD git show HEAD~1 git show HEAD~2 ``` till you get the required commit. To make the HEAD point to that, do ``` git reset --hard HEAD~1 ``` or `git reset --hard HEAD~2` or whatever. [Share](https://stackoverflow.com/a/22042019 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/22042019/edit) Follow Follow this answer to receive notifications [edited Jun 28, 2014 at 19:51](https://stackoverflow.com/posts/22042019/revisions "show all edits to this post") user456814 answered Feb 26, 2014 at 12:52 [![tonythomas01's user avatar](https://i.sstatic.net/8R08F.jpg?s=64)](https://stackoverflow.com/users/3355893/tonythomas01) [tonythomas01](https://stackoverflow.com/users/3355893/tonythomas01) 54811 gold badge55 silver badges77 bronze badges ## Comments Add a comment This answer is useful 32 Save this answer. Show activity on this post. Revert to *most recent* commit and ignoring all local changes: ``` git reset --hard HEAD ``` [Share](https://stackoverflow.com/a/38590895 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/38590895/edit) Follow Follow this answer to receive notifications [edited Jul 4, 2018 at 19:28](https://stackoverflow.com/posts/38590895/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Jul 26, 2016 at 13:13 [![Mohammed Irfan Tirupattur's user avatar](https://www.gravatar.com/avatar/5892fa036a647b0b5c696f7b44cad901?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1465378/mohammed-irfan-tirupattur) [Mohammed Irfan Tirupattur](https://stackoverflow.com/users/1465378/mohammed-irfan-tirupattur) 1,5891818 silver badges99 bronze badges ## Comments Add a comment This answer is useful 25 Save this answer. Show activity on this post. ## This is one more way to directly reset to a recent commit ``` git stash git stash clear ``` It directly clears all the changes that you have been making since the last commit. PS: It has a little problem; it also deletes all you recently stored stash changes. Which I guess in most cases should not matter. [Share](https://stackoverflow.com/a/37049724 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/37049724/edit) Follow Follow this answer to receive notifications [edited Jul 3, 2016 at 10:57](https://stackoverflow.com/posts/37049724/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered May 5, 2016 at 11:43 [![Point Networks's user avatar](https://www.gravatar.com/avatar/ca893187a5b14923dff233129f07f9ce?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1922447/point-networks) [Point Networks](https://stackoverflow.com/users/1922447/point-networks) 1,13111 gold badge1313 silver badges3737 bronze badges ## 2 Comments Add a comment [![](https://www.gravatar.com/avatar/f3373d3cb40957b62ec7ab4b3562b53b?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/2218992/andreyro) andreyro [andreyro](https://stackoverflow.com/users/2218992/andreyro) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment69290153_37049724) NOTE: New files not added in index are not stashed. You have too add them or manually delete them. 2016-12-08T14:08:46.75Z+00:00 0 Reply - Copy link [![](https://www.gravatar.com/avatar/e261055e2ed9f0176e1cecf7e38533c5?s=48&d=identicon&r=PG)](https://stackoverflow.com/users/1057485/romain-valeri) Romain Valeri [Romain Valeri](https://stackoverflow.com/users/1057485/romain-valeri) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment96827884_37049724) Why oh why clearing stash? In addition to being a non-solution, this is actually harmful. Reading the very first sentence of the question immediately invalidates the stash solution (which could be useful ONLY to reset to the LAST commit). 2019-03-07T07:35:51.157Z+00:00 0 Reply - Copy link This answer is useful 24 Save this answer. Show activity on this post. To completely clean a coder's directory up from some accidental changes, we used: ``` git add -A . git reset --hard HEAD ``` Just `git reset --hard HEAD` will get rid of modifications, but it won't get rid of "new" files. In their case they'd accidentally dragged an important folder somewhere random, and all those files were being treated as new by Git, so a `reset --hard` didn't fix it. By running the `git add -A .` beforehand, it explicitly tracked them all with git, to be wiped out by the reset. [Share](https://stackoverflow.com/a/33060283 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/33060283/edit) Follow Follow this answer to receive notifications [edited Jul 3, 2016 at 10:55](https://stackoverflow.com/posts/33060283/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Oct 11, 2015 at 0:10 [![Chris Moschini's user avatar](https://www.gravatar.com/avatar/354c23d7ab1248c0d1a9c3868caa68b2?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/176877/chris-moschini) [Chris Moschini](https://stackoverflow.com/users/176877/chris-moschini) 38\.2k1919 gold badges168168 silver badges198198 bronze badges ## 1 Comment Add a comment [![](https://i.sstatic.net/qmZfB.png?s=64)](https://stackoverflow.com/users/9213345/henke) Henke [Henke](https://stackoverflow.com/users/9213345/henke) [Over a year ago](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit#comment116043207_33060283) I think this is an answer to a rather different question - [stackoverflow.com/q/1125968](https://stackoverflow.com/q/1125968). I interpret [the question here](https://stackoverflow.com/q/4114095) to concern the ***remote*** repository. 2021-01-08T17:35:22.623Z+00:00 0 Reply - Copy link This answer is useful 23 Save this answer. Show activity on this post. I believe some people may come to this question wanting to know how to rollback committed changes they've made in their master - ie throw everything away and go back to origin/master, in which case, do this: ``` git reset --hard origin/master ``` <https://superuser.com/questions/273172/how-to-reset-master-to-origin-master> [Share](https://stackoverflow.com/a/28334713 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/28334713/edit) Follow Follow this answer to receive notifications [edited Mar 20, 2017 at 10:18](https://stackoverflow.com/posts/28334713/revisions "show all edits to this post") [![Community's user avatar](https://www.gravatar.com/avatar/a007be5a61f6aa8f3e85ae2fc18dd66e?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/-1/community) [Community](https://stackoverflow.com/users/-1/community)Bot 111 silver badge answered Feb 5, 2015 at 1:28 [![nevster's user avatar](https://www.gravatar.com/avatar/67d49c7cd51ebd01d8377fd40dc26cac?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/36510/nevster) [nevster](https://stackoverflow.com/users/36510/nevster) 6,46577 gold badges3838 silver badges4444 bronze badges ## Comments Add a comment This answer is useful 23 Save this answer. Show activity on this post. To keep the changes from the previous commit to HEAD and move to the previous commit, do: ``` git reset <SHA> ``` If changes are not required from the previous commit to HEAD and just discard all changes, do: ``` git reset --hard <SHA> ``` [Share](https://stackoverflow.com/a/31671224 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/31671224/edit) Follow Follow this answer to receive notifications [edited Jul 3, 2016 at 10:21](https://stackoverflow.com/posts/31671224/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Jul 28, 2015 at 8:35 [![Vishnu Atrai's user avatar](https://www.gravatar.com/avatar/8693312806c21245f272e6768ee17f91?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/585675/vishnu-atrai) [Vishnu Atrai](https://stackoverflow.com/users/585675/vishnu-atrai) 2,3682222 silver badges2424 bronze badges ## Comments Add a comment This answer is useful 17 Save this answer. Show activity on this post. As your commits are pushed remotely, you need to remove them. Let me assume your branch is *develop* and it is pushed over *origin*. You first need to remove *develop* from *origin*: ``` git push origin :develop (note the colon) ``` Then you need to get develop to the status you want, let me assume the commit hash is EFGHIJK: ``` git reset --hard EFGHIJK ``` Lastly, push *develop* again: ``` git push origin develop ``` [Share](https://stackoverflow.com/a/42898055 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/ "The current license for this post: CC BY-SA 4.0") [Improve this answer](https://stackoverflow.com/posts/42898055/edit) Follow Follow this answer to receive notifications [edited Jan 26, 2020 at 17:01](https://stackoverflow.com/posts/42898055/revisions "show all edits to this post") [![Peter Mortensen's user avatar](https://i.sstatic.net/RIZKi.png?s=64)](https://stackoverflow.com/users/63550/peter-mortensen) [Peter Mortensen](https://stackoverflow.com/users/63550/peter-mortensen) 31\.2k2222 gold badges111111 silver badges134134 bronze badges answered Mar 20, 2017 at 7:33 [![George Ninan's user avatar](https://www.gravatar.com/avatar/1c51f79eb33599a66a2ff492d000c27b?s=64&d=identicon&r=PG&f=y&so-version=2)](https://stackoverflow.com/users/6734526/george-ninan) [George Ninan](https://stackoverflow.com/users/6734526/george-ninan) 2,12722 gold badges1515 silver badges88 bronze badges ## Comments Add a comment This answer is useful 16 Save this answer. Show activity on this post. If you want to correct some error in the last commit a good alternative would be using **git commit --amend** command. If the last commit is not pointed by any reference, this will do the trick, as it create a commit with the same parent as the last commit. If there is no reference to the last commit, it will simply be discarded and this commit will be the last commit. This is a good way of correcting commits without reverting commits. However it has its own limitations. [Share](https://stackoverflow.com/a/31160175 "Short permalink to this answer") Share a link to this answer Copy link [CC BY-SA 3.0](https://creativecommons.org/licenses/by-sa/3.0/ "The current license for this post: CC BY-SA 3.0") [Improve this answer](https://stackoverflow.com/posts/31160175/edit) Follow Follow this answer to receive notifications answered Jul 1, 2015 at 11:35 [![Upul Doluweera's user avatar](https://i.sstatic.net/0FAyk.jpg?s=64)](https://stackoverflow.com/users/2028135/upul-doluweera) [Upul Doluweera](https://stackoverflow.com/users/2028135/upul-doluweera) 2,36622 gold badges2424 silver badges3030 bronze badges ## Comments Add a comment 1 [2](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?page=2&tab=scoredesc#tab-top "Go to page 2") [Next](https://stackoverflow.com/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit?page=2&tab=scoredesc#tab-top "Go to page 2") Start asking to get answers Find the answer to your question by asking. [Ask question](https://stackoverflow.com/questions/ask) Explore related questions - [git](https://stackoverflow.com/questions/tagged/git "show questions tagged 'git'") - [git-checkout](https://stackoverflow.com/questions/tagged/git-checkout "show questions tagged 'git-checkout'") - [git-reset](https://stackoverflow.com/questions/tagged/git-reset "show questions tagged 'git-reset'") - [git-revert](https://stackoverflow.com/questions/tagged/git-revert "show questions tagged 'git-revert'") See similar questions with these tags. - The Overflow Blog - [DeveloperWeek 2026: Making AI tools that are actually good](https://stackoverflow.blog/2026/03/05/developerweek-2026/?cb=1) - [Building brains for bulldozers](https://stackoverflow.blog/2026/03/06/building-brains-for-bulldozers/?cb=1) - Featured on Meta - [Logo updates to Stack Overflow's visual identity](https://meta.stackexchange.com/questions/417394/logo-updates-to-stack-overflows-visual-identity?cb=1) - [Policy: Generative AI (e.g., ChatGPT) is banned](https://meta.stackoverflow.com/questions/421831/policy-generative-ai-e-g-chatgpt-is-banned?cb=1) - [New site design and philosophy for Stack Overflow: Starting February 24, 2026...](https://meta.stackoverflow.com/questions/438177/new-site-design-and-philosophy-for-stack-overflow-starting-february-24-2026-at?cb=1 "New site design and philosophy for Stack Overflow: Starting February 24, 2026 at beta.stackoverflow.com") - [I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s...](https://meta.stackoverflow.com/questions/438369/i-m-jody-the-chief-product-and-technology-officer-at-stack-overflow-let-s-talk?cb=1 "I’m Jody, the Chief Product and Technology Officer at Stack Overflow. Let’s talk about the site redesign") [Visit chat](https://chat.stackoverflow.com/) Community activity Last 1 hr - Users online activity 10926 users online - 14 questions - 20 answers - 52 comments - 220 upvotes Popular tags [javascript](https://stackoverflow.com/questions/tagged/javascript)[python](https://stackoverflow.com/questions/tagged/python)[c\#](https://stackoverflow.com/questions/tagged/c)[java](https://stackoverflow.com/questions/tagged/java)[bash](https://stackoverflow.com/questions/tagged/bash)[html](https://stackoverflow.com/questions/tagged/html) Popular unanswered question [Running "npm ci" when building docker image is much slower](https://stackoverflow.com/questions/55088190) [node.js](https://stackoverflow.com/questions/tagged/node.js)[docker](https://stackoverflow.com/questions/tagged/docker)[docker-for-windows](https://stackoverflow.com/questions/tagged/docker-for-windows)[docker-desktop](https://stackoverflow.com/questions/tagged/docker-desktop)[npm-ci](https://stackoverflow.com/questions/tagged/npm-ci) [![User avatar](https://www.gravatar.com/avatar/bd53e7ed0b6d276980744e3cc93b7c2a?s=256&d=identicon&r=PG)](https://stackoverflow.com/users/825423) [Pavels Ahmadulins](https://stackoverflow.com/users/825423) - 480 2,556 days ago #### Linked [761](https://stackoverflow.com/questions/1895059/revert-to-a-commit-by-a-sha-hash-in-git?lq=1 "Question score (upvotes - downvotes)") [Revert to a commit by a SHA hash in Git?](https://stackoverflow.com/questions/1895059/revert-to-a-commit-by-a-sha-hash-in-git?noredirect=1&lq=1) [822](https://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit?lq=1 "Question score (upvotes - downvotes)") [How do you roll back (reset) a Git repository to a particular commit?](https://stackoverflow.com/questions/1616957/how-do-you-roll-back-reset-a-git-repository-to-a-particular-commit?noredirect=1&lq=1) [1323](https://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit?lq=1 "Question score (upvotes - downvotes)") [How do I use 'git reset --hard HEAD' to revert to a previous commit?](https://stackoverflow.com/questions/9529078/how-do-i-use-git-reset-hard-head-to-revert-to-a-previous-commit?noredirect=1&lq=1) [583](https://stackoverflow.com/questions/3639115/reverting-to-a-specific-commit-based-on-commit-id-with-git?lq=1 "Question score (upvotes - downvotes)") [Reverting to a specific commit based on commit id with Git?](https://stackoverflow.com/questions/3639115/reverting-to-a-specific-commit-based-on-commit-id-with-git?noredirect=1&lq=1) [337](https://stackoverflow.com/questions/6794110/git-revert-back-to-certain-commit?lq=1 "Question score (upvotes - downvotes)") [git revert back to certain commit](https://stackoverflow.com/questions/6794110/git-revert-back-to-certain-commit?noredirect=1&lq=1) [249](https://stackoverflow.com/questions/3380805/checkout-old-commit-and-make-it-a-new-commit?lq=1 "Question score (upvotes - downvotes)") [Checkout old commit and make it a new commit](https://stackoverflow.com/questions/3380805/checkout-old-commit-and-make-it-a-new-commit?noredirect=1&lq=1) [7](https://stackoverflow.com/questions/11301359/why-doesnt-git-reset-hard-change-my-local-files-back-to-their-original-state?lq=1 "Question score (upvotes - downvotes)") [Why doesn't git reset --hard change my local files back to their original state?](https://stackoverflow.com/questions/11301359/why-doesnt-git-reset-hard-change-my-local-files-back-to-their-original-state?noredirect=1&lq=1) [19](https://stackoverflow.com/questions/41427441/revert-back-to-specific-commit-in-git?lq=1 "Question score (upvotes - downvotes)") [Revert back to specific commit in Git](https://stackoverflow.com/questions/41427441/revert-back-to-specific-commit-in-git?noredirect=1&lq=1) [7](https://stackoverflow.com/questions/27707752/whats-the-command-to-reset-a-file-to-a-specific-commit?lq=1 "Question score (upvotes - downvotes)") [What's the command to reset a file to a specific commit?](https://stackoverflow.com/questions/27707752/whats-the-command-to-reset-a-file-to-a-specific-commit?noredirect=1&lq=1) [6](https://stackoverflow.com/questions/23323354/git-reversing-mistake?lq=1 "Question score (upvotes - downvotes)") [Git reversing mistake](https://stackoverflow.com/questions/23323354/git-reversing-mistake?noredirect=1&lq=1) [See more linked questions](https://stackoverflow.com/questions/linked/4114095?lq=1) #### Related [30](https://stackoverflow.com/questions/5004237/git-revert-to-previous-commit-status?rq=3 "Question score (upvotes - downvotes)") [Git: Revert to previous commit status](https://stackoverflow.com/questions/5004237/git-revert-to-previous-commit-status?rq=3) [1](https://stackoverflow.com/questions/5741162/how-do-i-revert-back-to-an-older-commit-in-git?rq=3 "Question score (upvotes - downvotes)") [How do I revert back to an older commit in Git?](https://stackoverflow.com/questions/5741162/how-do-i-revert-back-to-an-older-commit-in-git?rq=3) [638](https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit?rq=3 "Question score (upvotes - downvotes)") [How to revert initial git commit?](https://stackoverflow.com/questions/6632191/how-to-revert-initial-git-commit?rq=3) [1](https://stackoverflow.com/questions/10890855/how-to-revert-in-git?rq=3 "Question score (upvotes - downvotes)") [how to revert in Git?](https://stackoverflow.com/questions/10890855/how-to-revert-in-git?rq=3) [5](https://stackoverflow.com/questions/16364674/revert-git-repo-to-previous-commit?rq=3 "Question score (upvotes - downvotes)") [Revert git repo to previous commit](https://stackoverflow.com/questions/16364674/revert-git-repo-to-previous-commit?rq=3) [2](https://stackoverflow.com/questions/16927402/restore-repository-to-a-previous-git-commit?rq=3 "Question score (upvotes - downvotes)") [Restore repository to a previous GIT commit](https://stackoverflow.com/questions/16927402/restore-repository-to-a-previous-git-commit?rq=3) [0](https://stackoverflow.com/questions/26622913/how-to-revert-to-an-earlier-commit?rq=3 "Question score (upvotes - downvotes)") [How to revert to an earlier commit](https://stackoverflow.com/questions/26622913/how-to-revert-to-an-earlier-commit?rq=3) [4](https://stackoverflow.com/questions/29235888/git-how-to-revert-a-revert?rq=3 "Question score (upvotes - downvotes)") [Git: how to revert a revert](https://stackoverflow.com/questions/29235888/git-how-to-revert-a-revert?rq=3) [1](https://stackoverflow.com/questions/39368647/git-reset-to-old-commit?rq=3 "Question score (upvotes - downvotes)") [Git: Reset to old commit](https://stackoverflow.com/questions/39368647/git-reset-to-old-commit?rq=3) [4](https://stackoverflow.com/questions/50431456/how-to-rollback-github-repository-to-a-previous-commit?rq=3 "Question score (upvotes - downvotes)") [How to rollback github repository to a previous commit?](https://stackoverflow.com/questions/50431456/how-to-rollback-github-repository-to-a-previous-commit?rq=3) #### [Hot Network Questions](https://stackexchange.com/questions?tab=hot) - [Relying on declaration order vs explicit lifecycle (breaking Rule of Zero)?](https://softwareengineering.stackexchange.com/questions/460961/relying-on-declaration-order-vs-explicit-lifecycle-breaking-rule-of-zero) - [How to depict an organic salt in chemfig](https://tex.stackexchange.com/questions/760604/how-to-depict-an-organic-salt-in-chemfig) - [According to 1 Timothy 5:17, what is the difference between the Word and doctrine?](https://hermeneutics.stackexchange.com/questions/115208/according-to-1-timothy-517-what-is-the-difference-between-the-word-and-doctrin) - [How can I speed up arbitrary-precision numerical matrix multiplication?](https://mathematica.stackexchange.com/questions/318955/how-can-i-speed-up-arbitrary-precision-numerical-matrix-multiplication) - [What is the political ideology of the Republican Party under Trump (2026)?](https://politics.stackexchange.com/questions/94344/what-is-the-political-ideology-of-the-republican-party-under-trump-2026) - [Why is mass coupled to inertia but the other fundamental charges aren't?](https://physics.stackexchange.com/questions/869912/why-is-mass-coupled-to-inertia-but-the-other-fundamental-charges-arent) - [Minimum matchsticks to form all rectangles up to 7x7](https://puzzling.stackexchange.com/questions/137325/minimum-matchsticks-to-form-all-rectangles-up-to-7x7) - [How can I transfer money from Wise account to bank account without fee?](https://money.stackexchange.com/questions/169366/how-can-i-transfer-money-from-wise-account-to-bank-account-without-fee) - [Producing an Elliptical Ring of Images](https://tex.stackexchange.com/questions/760598/producing-an-elliptical-ring-of-images) - [Convert OpenAPI Response Schema to PySpark DataFrame Schema](https://codereview.stackexchange.com/questions/301537/convert-openapi-response-schema-to-pyspark-dataframe-schema) - [How does a spherical bearing work in bridges?](https://engineering.stackexchange.com/questions/65393/how-does-a-spherical-bearing-work-in-bridges) - [How to make a jungle absolutely impenetrable and inimical to 'modern' humans without resorting to magic](https://worldbuilding.stackexchange.com/questions/272860/how-to-make-a-jungle-absolutely-impenetrable-and-inimical-to-modern-humans-wit) - [Why are CSS colors different on different GPUs within the CMYK gamut, and a colorimeter doesn't fix it?](https://superuser.com/questions/1935587/why-are-css-colors-different-on-different-gpus-within-the-cmyk-gamut-and-a-colo) - [How many atoms are coplanar in Zeise’s salt?](https://chemistry.stackexchange.com/questions/195184/how-many-atoms-are-coplanar-in-zeise-s-salt) - [Is it always obligatory to accept the Jizyah from disbelievers (when they are not captured)?](https://islam.stackexchange.com/questions/91207/is-it-always-obligatory-to-accept-the-jizyah-from-disbelievers-when-they-are-no) - [“Certain wands of Jacob’s wit” in Keats’ ‘Sleep and Poetry’](https://literature.stackexchange.com/questions/31774/certain-wands-of-jacob-s-wit-in-keats-sleep-and-poetry) - [Texture Paint behaves differently in Solid vs Material Preview](https://blender.stackexchange.com/questions/345586/texture-paint-behaves-differently-in-solid-vs-material-preview) - [Leetcode Coin Change](https://codereview.stackexchange.com/questions/301534/leetcode-coin-change) - [Build a minimal i686 Ubuntu live image (no GUI) with hardware detection similar to the Ubuntu installer?](https://unix.stackexchange.com/questions/804878/build-a-minimal-i686-ubuntu-live-image-no-gui-with-hardware-detection-similar) - [Do you bleed brakes after experiencing fade?](https://bicycles.stackexchange.com/questions/100067/do-you-bleed-brakes-after-experiencing-fade) - [What is a terminal view of a connector?](https://electronics.stackexchange.com/questions/766714/what-is-a-terminal-view-of-a-connector) - [Would perfect muscle control realistically be advantageous?](https://worldbuilding.stackexchange.com/questions/272841/would-perfect-muscle-control-realistically-be-advantageous) - [Failed at creating Barcode using pst-barcode](https://tex.stackexchange.com/questions/760602/failed-at-creating-barcode-using-pst-barcode) - [Can you sleep overnight in a camper in Walmart parking lots?](https://travel.stackexchange.com/questions/203409/can-you-sleep-overnight-in-a-camper-in-walmart-parking-lots) ![](https://stackoverflow.com/posts/4114095/ivc/d2f6?prg=ace81514-789f-493e-8a87-ef9e78c14587) # Why are you flagging this comment? Probable spam. This comment promotes a product, service or website while [failing to disclose the author's affiliation](https://stackoverflow.com/help/promotion). Unfriendly or contains harassment/bigotry/abuse. This comment is unkind, insulting or attacks another person or group. Learn more in our [Abusive behavior policy](https://stackoverflow.com/conduct/abusive-behavior). Not needed. This comment is not relevant to the post. ``` ``` Enter at least 6 characters Something else. A problem not listed above. Try to be as specific as possible. ``` ``` Enter at least 6 characters Flag comment Cancel You have 0 flags left today # ![Illustration of upvote icon after it is clicked](https://stackoverflow.com/Content/Img/modal/img-upvote.png?v=fce73bd9724d) # Hang on, you can't upvote just yet. You'll need to complete a few actions and gain 15 reputation points before being able to upvote. **Upvoting** indicates when questions and answers are useful. [What's reputation and how do I get it?](https://stackoverflow.com/help/whats-reputation) Instead, you can save this post to reference later. Save this post for later Not now ##### [Stack Overflow](https://stackoverflow.com/) - [Questions](https://stackoverflow.com/questions) - [Help](https://stackoverflow.com/help) - [Chat](https://chat.stackoverflow.com/?tab=explore) ##### [Business](https://stackoverflow.co/) - [Stack Internal](https://stackoverflow.co/internal/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=teams) - [Stack Data Licensing](https://stackoverflow.co/data-licensing/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=data-licensing) - [Stack Ads](https://stackoverflow.co/advertising/?utm_medium=referral&utm_source=stackoverflow-community&utm_campaign=footer&utm_content=advertising) ##### [Company](https://stackoverflow.co/) - [About](https://stackoverflow.co/) - [Press](https://stackoverflow.co/company/press/) - [Work Here](https://stackoverflow.co/company/work-here/) - [Legal](https://stackoverflow.com/legal) - [Privacy Policy](https://stackoverflow.com/legal/privacy-policy) - [Terms of Service](https://stackoverflow.com/legal/terms-of-service/public) - [Contact Us](https://stackoverflow.com/contact) - Cookie Settings - [Cookie Policy](https://policies.stackoverflow.co/stack-overflow/cookie-policy) ##### [Stack Exchange Network](https://stackexchange.com/) - [Technology](https://stackexchange.com/sites#technology) - [Culture & recreation](https://stackexchange.com/sites#culturerecreation) - [Life & arts](https://stackexchange.com/sites#lifearts) - [Science](https://stackexchange.com/sites#science) - [Professional](https://stackexchange.com/sites#professional) - [Business](https://stackexchange.com/sites#business) - [API](https://api.stackexchange.com/) - [Data](https://data.stackexchange.com/) - [Blog](https://stackoverflow.blog/?blb=1) - [Facebook](https://www.facebook.com/officialstackoverflow/) - [Twitter](https://twitter.com/stackoverflow) - [LinkedIn](https://linkedin.com/company/stack-overflow) - [Instagram](https://www.instagram.com/thestackoverflow) Site design / logo © 2026 Stack Exchange Inc; user contributions licensed under [CC BY-SA](https://stackoverflow.com/help/licensing) . rev 2026.3.6.40623
Readable Markdown
This depends a lot on what you mean by "revert". ## Temporarily switch to a different commit If you want to temporarily go back to it, fool around, then come back to where you are, all you have to do is check out the desired commit: ``` # This will detach your HEAD, that is, leave you with no branch checked out: git checkout 0d1d7fc32 ``` Or if you want to make commits while you're there, go ahead and make a new branch while you're at it: ``` git checkout -b old-state 0d1d7fc32 ``` To go back to where you were, just check out the branch you were on again. (If you've made changes, as always when switching branches, you'll have to deal with them as appropriate. You could reset to throw them away; you could stash, checkout, stash pop to take them with you; you could commit them to a branch there if you want a branch there.) ## Hard delete unpublished commits If, on the other hand, you want to really get rid of everything you've done since then, there are two possibilities. One, if you haven't published any of these commits, simply reset: ``` # This will destroy any local modifications. # Don't do it if you have uncommitted work you want to keep. git reset --hard 0d1d7fc32 # Alternatively, if there's work to keep: git stash git reset --hard 0d1d7fc32 git stash pop # This saves the modifications, then reapplies that patch after resetting. # You could get merge conflicts, if you've modified things which were # changed since the commit you reset to. ``` If you mess up, you've already thrown away your local changes, but you can at least get back to where you were before by resetting again. ## Undo published commits with new commits On the other hand, if you've published the work, you probably don't want to reset the branch, since that's effectively rewriting history. In that case, you could indeed revert the commits. In many enterprise organisations, the concept of "protected" branches will even prevent history from being rewritten on some major branches. In this case, reverting is your only option. With Git, revert has a very specific meaning: create a commit with the reverse patch to cancel it out. This way you don't rewrite any history. First figure out what commits to revert. Depending on the technique chosen below, you want to either revert only the merge commits, or only the non-merge commits. ``` # This lists all merge commits between 0d1d7fc and HEAD: git log --merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' # This lists all non merge commits between 0d1d7fc and HEAD: git log --no-merges --pretty=format:"%h" 0d1d7fc..HEAD | tr '\n' ' ' ``` Note: if you revert multiple commits, the order matters. Start with the most recent commit. ``` # This will create three separate revert commits, use non merge commits only: git revert a867b4af 25eee4ca 0766c053 # It also takes ranges. This will revert the last two commits: git revert HEAD~2..HEAD # Similarly, you can revert a range of commits using commit hashes (non inclusive of first hash): git revert 0d1d7fc..a867b4a # Reverting a merge commit. You can also use a range of merge commits here. git revert -m 1 <merge_commit_sha> # To get just one, you could use `rebase -i` to squash them afterwards # Or, you could do it manually (be sure to do this at top level of the repo) # get your index and work tree into the desired state, without changing HEAD: git checkout 0d1d7fc32 . # Then commit. Be sure and write a good message describing what you just did git commit ``` The [`git-revert` manpage](https://git-scm.com/docs/git-revert) actually covers a lot of this in its description. Another useful link is [this git-scm.com section discussing git-revert](https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#_undoing_merges). If you decide you didn't want to revert after all, you can revert the revert (as described here) or reset back to before the revert (see the previous section). You may also find this answer helpful in this case: [How can I move HEAD back to a previous location? (Detached head) & Undo commits](https://stackoverflow.com/questions/34519665/how-to-move-head-forward-checkout-revet-reflog-reset/34519716#34519716)
Shard169 (laksa)
Root Hash714406497480128969
Unparsed URLcom,stackoverflow!/questions/4114095/how-do-i-revert-a-git-repository-to-a-previous-commit s443