🕷️ Crawler Inspector

URL Lookup

Direct Parameter Lookup

Raw Queries and Responses

1. Shard Calculation

Query:
Response:
Calculated Shard: 2 (from laksa097)

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
11 days ago
🤖
ROBOTS ALLOWED

Page Info Filters

FilterStatusConditionDetails
HTTP statusPASSdownload_http_code = 200HTTP 200
Age cutoffPASSdownload_stamp > now() - 6 MONTH0.4 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://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html
Last Crawled2026-04-06 16:15:33 (11 days ago)
First Indexed2020-01-16 06:34:01 (6 years ago)
HTTP Status Code200
Meta TitleHow to Revert a Git Repository to a Previous Commit
Meta DescriptionRead this tutorial and learn how to temporarily switch to different commit, hard delete unpublished commits, and undo published commits with new commits.
Meta Canonicalnull
Boilerpipe Text
While working with Git , it is often required to get back to one of the older commits to view its state or to delete the commits coming after it. Below, we will discuss the mentioned scenarios. To go back to an older commit temporarily, you can use the git checkout command by mentioning the commit hash: git checkout <sha1-commit-hash> The command above will detach your HEAD , that is, leave you with no branch checked out. To make commits after detaching from your branch, you need to create a new branch on the basis of that commit by using the command below: git switch -c <new-branch-name> Instead of using the 2 commands above, you can run the command below to create a new branch on the basis of an older commit : git checkout -b <new-branch-name> <sha1-commit-hash> If you want to delete the recent commits existing only on your local repository, run the command below: git reset --hard <sha1-commit-hash> The command above will delete all the recent commits up to the one you have mentioned the hash for. The mentioned commit will be the most recent one In case you have uncommitted local changes on your branch, this command will delete all of them. If there are changes that you want to keep, act as follows: git stash git reset --hard <sha1-commit-hash> git stash pop This saves the modifications, then re-applies that patch after resetting. Merge conflicts may arise if you have modified things which were changed since the commit you reset to. If you mess it up, you can go back anytime. To learn how to do that, check out How to Undo Git Reset . In this section, we will demonstrate two ways of undoing the published commits, depending on whether you want to save the history or not. Remove the published commits As it was demonstrated in the previous section, it is possible to remove local commits with the git reset --hard command. After that, you can run git push with --force option and remove the commits from the remote repository. git push --force origin HEAD --force overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch. git push --force-with-lease origin HEAD --force-with-lease is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's. Revert the published commits If you have published your work and don't want to reset the branch, you can revert the commits. Use git revert so as not to rewrite any history: git revert <sha1-commit-hash> If you don't want to add a separate commit for revert, after reverting you can run git rebase in the interactive mode to combine the latest commit with the commit of the revert. To learn how to do that check out How to Combine Multiple Git Commits into One . git rebase -i HEAD~ 2 Git revert belongs to the “undo” category operations, yet it’s not a classic undo command. The git revert command is applied to undo changes to the commit history of the repository. The git revert command allows you to pick a single commit, converse the changes and, finally, generate a completely new one. In the scope of this tutorial, you will learn how to revert to the previous state by using Git commands. Generally, we can distinguish 2 significant advantages of git revert . They are as follows: The revert command can be effectively applied to a specific commit. It doesn’t change the history of the project. This means that it’s a completely secure operation for commits, which were already sent to the shared repository. The most significant difference between git revert and git reset is that the git revert command targets a specific commit not removing all the coming commits. On the contrary, using the git reset command will remove the overall coming commits. Let’s say you are going to undo a commit with the usage of the git reset command, you need to remove the whole commits coming after the targeted one. Generally, git revert is the most secure alternative of git reset . Although the process may seem quite complicated, it becomes pretty easy after going through it several times.
Markdown
[![w3docs logo](https://www.w3docs.com/build/images/logo-color-w3.png)](https://www.w3docs.com/) Books [Learn HTML](https://www.w3docs.com/learn-html.html) [Learn CSS](https://www.w3docs.com/learn-css.html) [Learn Git](https://www.w3docs.com/learn-git.html) [Learn Javascript](https://www.w3docs.com/learn-javascript.html) [Learn PHP](https://www.w3docs.com/learn-php.html) [Learn python](https://www.w3docs.com/learn-python.html) [Learn Java](https://www.w3docs.com/learn-java.html) Exercises [HTML](https://www.w3docs.com/exercise/html) [JavaScript](https://www.w3docs.com/exercise/javascript) [Git](https://www.w3docs.com/exercise/git) [CSS](https://www.w3docs.com/exercise/css) [PHP](https://www.w3docs.com/exercise/php) [Courses](https://www.w3docs.com/nx/course) [Quizzes](https://www.w3docs.com/quiz/) [Snippets](https://www.w3docs.com/snippets) Tools General Tools - [Password Generator](https://www.w3docs.com/tools/password-generator) - [HTML Editor](https://www.w3docs.com/tools/editor) - [HTML Encoder](https://www.w3docs.com/tools/html-encoder/) - [Base 64](https://www.w3docs.com/tools/image-base64) - [Code Diff](https://www.w3docs.com/tools/code-diff/) - [JSON Beautifier](https://www.w3docs.com/nx/json-beautify) - [CSS Beautifier](https://www.w3docs.com/nx/css-beautify) - [Markdown Convertor](https://www.w3docs.com/nx/marked) - [Find the Closest Tailwind CSS Color](https://www.w3docs.com/nx/tailwindcss-closest-color) - [Phrase encrypt / decrypt](https://www.w3docs.com/nx/phrase-encrypt) - [Browser Feature Detection](https://www.w3docs.com/nx/feature-detection) - [Number convertor](https://www.w3docs.com/nx/number/base-converter) - [JTW Decoder](https://www.w3docs.com/nx/jwt-decoder) CSS Maker - [CSS Maker](https://www.w3docs.com/nx/css-maker/border-radius) - [CSS Maker text shadow](https://www.w3docs.com/nx/css-maker/text-shadow) - [CSS Maker Text Rotation](https://www.w3docs.com/nx/css-maker/text-rotation) - [CSS Maker Out Line](https://www.w3docs.com/nx/css-maker/outline-tool) - [CSS Maker RGB Shadow](https://www.w3docs.com/nx/css-maker/rgba-shadow) - [CSS Maker Transform](https://www.w3docs.com/nx/css-maker/transform-tool) - [CSS Maker Font Face](https://www.w3docs.com/nx/css-maker/font-face) Color Tools - [Color Picker](https://www.w3docs.com/nx/color/color-picker) - [Colors CMYK](https://www.w3docs.com/nx/color/color-cmyk) - [Colors HWB](https://www.w3docs.com/nx/color/color-hwb) - [Colors HSL](https://www.w3docs.com/nx/color/color-hsl) - [Color Hex](https://www.w3docs.com/nx/color/color-hex) - [Color mixer](https://www.w3docs.com/nx/color/color-mixer) - [Color Converter](https://www.w3docs.com/nx/color/color-converter) - [Colors RGB](https://www.w3docs.com/nx/color/color-rgb) - [Color Contrast Analyzer](https://www.w3docs.com/nx/color/contrast-ratio) - [Color Gradient](https://www.w3docs.com/nx/color/gradient-background) String Tools - [String Length Calculator](https://www.w3docs.com/tools/string-length-calculator) - [MD5 Hash Generator](https://www.w3docs.com/tools/string-md5-generator) - [Sha256 Hash Generator](https://www.w3docs.com/tools/string-sha256-generator) - [String Reverse](https://www.w3docs.com/tools/string-revers) - [URL Encoder](https://www.w3docs.com/tools/string-url-encoder) - [URL Decoder](https://www.w3docs.com/tools/string-url-decoder) - [Base 64 Encoder](https://www.w3docs.com/tools/string-base64-encoder) - [Base 64 Decoder](https://www.w3docs.com/tools/string-base64-decoder) - [Extra Spaces Remover](https://www.w3docs.com/tools/string-remove-extra-spaces) - [String to Lowercase](https://www.w3docs.com/tools/string-convert-lowercase) - [String to Uppercase](https://www.w3docs.com/tools/string-convert-uppercase) - [Word Count Calculator](https://www.w3docs.com/tools/string-word-count) - [Empty Lines Remover](https://www.w3docs.com/tools/string-remove-empty-lines) - [HTML Tags Remover](https://www.w3docs.com/tools/string-remove-tags) - [Binary to Hex](https://www.w3docs.com/tools/string-bin-2-hex) - [Hex to Binary](https://www.w3docs.com/tools/string-hex-2-bin) - [Rot13 Transform on a String](https://www.w3docs.com/tools/string-convert-rot-13) - [String to Binary](https://www.w3docs.com/tools/string-convert-to-bin) - [Duplicate Lines Remover](https://www.w3docs.com/tools/string-remove-double-line) Change theme - Dark - Light - System - Books - [Learn HTML](https://www.w3docs.com/learn-html.html) - [Learn CSS](https://www.w3docs.com/learn-css.html) - [Learn Git](https://www.w3docs.com/learn-git.html) - [Learn Javascript](https://www.w3docs.com/learn-javascript.html) - [Learn PHP](https://www.w3docs.com/learn-php.html) - [Learn python](https://www.w3docs.com/learn-python.html) - [Learn Java](https://www.w3docs.com/learn-java.html) - How To - [How To NodeJs](https://www.w3docs.com/snippets/nodejs.html) - [How To Linux](https://www.w3docs.com/snippets/linux.html) - [How To AngularJs](https://www.w3docs.com/snippets/angularjs.html) - [How To PHP](https://www.w3docs.com/snippets/php.html) - [How To HTML](https://www.w3docs.com/snippets/html.html) - [How To CSS](https://www.w3docs.com/snippets/css.html) - [How To Symfony](https://www.w3docs.com/snippets/symfony.html) - [How To Git](https://www.w3docs.com/snippets/git.html) - [How To Apache](https://www.w3docs.com/snippets/apache.html) - [How To JavaScript](https://www.w3docs.com/snippets/javascript.html) - [How To Java](https://www.w3docs.com/snippets/java.html) - [How To Vue.js](https://www.w3docs.com/snippets/vue-js.html) - [How To Python](https://www.w3docs.com/snippets/python.html) 1. [Snippets](https://www.w3docs.com/snippets) 2. [Git](https://www.w3docs.com/snippets/git.html) 3. How to Revert a Git Repository to a Previous Commit # How to Revert a Git Repository to a Previous Commit 1. [How to temporarily switch to a different commit](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#how-to-temporarily-switch-to-a-different-commit-1) 2. [How to remove unpublished commits](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#how-to-remove-unpublished-commits-7) 3. [How to undo published commits](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#how-to-undo-published-commits-13) - [Remove the published commits](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#remove-the-published-commits-13) - [Revert the published commits](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#revert-the-published-commits-18) 4. [The git revert Command](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#the-git-revert-command-22) 5. [The git revert and git reset Commands](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#the-git-revert-and-git-reset-commands-22) 6. [Related Resources](https://www.w3docs.com/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html#related-resources) While working with [Git](https://www.w3docs.com/learn-git.html), it is often required to get back to one of the older commits to view its state or to delete the commits coming after it. Below, we will discuss the mentioned scenarios. ## How to temporarily switch to a different commit To go back to an older commit temporarily, you can use the [git checkout](https://www.w3docs.com/learn-git/git-checkout.html) command by mentioning the commit hash: ``` git checkout <sha1-commit-hash> ``` The command above will detach your `HEAD`, that is, leave you with no branch checked out. To make commits after detaching from your branch, you need to create a new branch on the basis of that commit by using the command below: ``` git switch -c <new-branch-name> ``` Instead of using the 2 commands above, you can run the command below to create a new branch on the basis of an older commit : ``` git checkout -b <new-branch-name> <sha1-commit-hash> ``` Copy ## How to remove unpublished commits If you want to delete the recent commits existing only on your local repository, run the command below: ``` git reset --hard <sha1-commit-hash> ``` Copy The command above will delete all the recent commits up to the one you have mentioned the hash for. The mentioned commit will be the most recent one In case you have uncommitted local changes on your branch, this command will delete all of them. If there are changes that you want to keep, act as follows: ``` git stash git reset --hard <sha1-commit-hash> git stash pop ``` This saves the modifications, then re-applies that patch after resetting. Merge conflicts may arise if you have modified things which were changed since the commit you reset to. If you mess it up, you can go back anytime. To learn how to do that, check out [How to Undo Git Reset](https://www.w3docs.com/snippets/git/how-to-undo-git-reset.html). ## How to undo published commits In this section, we will demonstrate two ways of undoing the published commits, depending on whether you want to save the history or not. ### Remove the published commits As it was demonstrated in the previous section, it is possible to remove local commits with the `git reset --hard` command. After that, you can run [git push](https://www.w3docs.com/learn-git/git-push.html) with `--force` option and remove the commits from the remote repository. ``` git push --force origin HEAD ``` Copy **\--force** overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch. ``` git push --force-with-lease origin HEAD ``` Copy **\--force-with-lease** is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's. ### Revert the published commits If you have published your work and don't want to reset the branch, you can revert the commits. Use `git revert` so as not to rewrite any history: ``` git revert <sha1-commit-hash> ``` Copy If you don't want to add a separate commit for revert, after reverting you can run `git rebase` in the interactive mode to combine the latest commit with the commit of the revert. To learn how to do that check out [How to Combine Multiple Git Commits into One](https://www.w3docs.com/snippets/git/how-to-combine-multiple-commits-into-one-with-3-steps.html). ``` git rebase -i HEAD~2 ``` Copy ## The git revert Command `Git revert` belongs to the “undo” category operations, yet it’s not a classic undo command. The `git revert` command is applied to undo changes to the commit history of the repository. The `git revert` command allows you to pick a single commit, converse the changes and, finally, generate a completely new one. In the scope of this tutorial, you will learn how to revert to the previous state by using [Git](https://www.w3docs.com/learn-git.html) commands. Generally, we can distinguish 2 significant advantages of `git revert`. They are as follows: 1. The revert command can be effectively applied to a specific commit. 2. It doesn’t change the history of the project. This means that it’s a completely secure operation for commits, which were already sent to the shared repository. ## The git revert and git reset Commands The most significant difference between `git revert` and [git reset](https://www.w3docs.com/learn-git.html) is that the `git revert` command targets a specific commit not removing all the coming commits. On the contrary, using the `git reset` command will remove the overall coming commits. Let’s say you are going to undo a commit with the usage of the `git reset` command, you need to remove the whole commits coming after the targeted one. Generally, `git revert` is the most secure alternative of `git reset`. Although the process may seem quite complicated, it becomes pretty easy after going through it several times. Tags [git](https://www.w3docs.com/snippets-tags/git) [git commit](https://www.w3docs.com/snippets-tags/git%20commit) [git repository](https://www.w3docs.com/snippets-tags/git%20repository) [git revert](https://www.w3docs.com/snippets-tags/git%20revert) [git reset](https://www.w3docs.com/snippets-tags/git%20reset) ## Related Resources - [How to Revert All Local Changes in Git Managed Project to Previous State](https://www.w3docs.com/snippets/git/how-to-revert-all-local-changes-in-git-managed-project-to-previous-state.html) - [How to Make the Current Commit the Only Commit in a Git Repository](https://www.w3docs.com/snippets/git/how-to-make-the-current-commit-the-only-commit-in-a-git-repository.html) - [How to Revert a Merge Commit Already Pushed to the Remote Branch in Git](https://www.w3docs.com/snippets/git/how-to-revert-a-merge-commit-already-pushed-to-the-remote-branch.html) - [How to Reset or Revert a File to a Specific Version in Git](https://www.w3docs.com/snippets/git/how-to-reset-or-revert-a-file-to-a-specific-revision.html) - [How to Move Git Branch Pointer to Different Commit](https://www.w3docs.com/snippets/git/how-to-move-branch-pointer-to-different-commit.html) - [How to Change Older or Multiple Git Commit Messages](https://www.w3docs.com/snippets/git/how-to-change-older-or-multiple-commits.html) - [How to Retrieve Hash for Commits in Git](https://www.w3docs.com/snippets/git/how-to-retrieve-hash-for-commits-in-git.html) - [How to Merge a Specific Commit in Git](https://www.w3docs.com/snippets/git/how-to-merge-a-specific-commit-in-git.html) - [How to Reference the Initial Git Commit](https://www.w3docs.com/snippets/git/how-to-reference-the-initial-commit.html) - [How to Undo Pushed Git Commits](https://www.w3docs.com/snippets/git/how-to-undo-pushed-commits.html) **Sorry about that** How can we improve it? Submit Thanks for your feedback\! Thanks for your feedback\! Do you find this helpful? Yes No [Quizzes](https://www.w3docs.com/quiz/) - [PHP basics](https://www.w3docs.com/quiz-start/php-basic) - [HTML Basics](https://www.w3docs.com/quiz-start/html-basic) - [Javascript Basics](https://www.w3docs.com/quiz-start/javascript-basic) - [CSS Basics](https://www.w3docs.com/quiz-start/css-basic) - [ES6 Basics](https://www.w3docs.com/quiz-start/es6-basic) - [TypeScript Basics](https://www.w3docs.com/quiz-start/ts-quiz) - [React Basics](https://www.w3docs.com/quiz-start/react) - [Angular Basics](https://www.w3docs.com/quiz-start/angular) - [Sass Basics](https://www.w3docs.com/quiz-start/sass) - [Git Basics](https://www.w3docs.com/quiz-start/git) - [Vue.js Basics](https://www.w3docs.com/quiz-start/vuejs) - [SQL Basics](https://www.w3docs.com/quiz-start/sql) - [Python Basics](https://www.w3docs.com/quiz-start/python-basics) - [Java Basics](https://www.w3docs.com/quiz-start/java) - [NodeJS Basics](https://www.w3docs.com/quiz-start/node) - Quizzes - [HTML Basics](https://www.w3docs.com/quiz-start/html-basic) - [CSS Basics](https://www.w3docs.com/quiz-start/css-basic) - [Javascript Basics](https://www.w3docs.com/quiz-start/javascript-basic) - [PHP basics](https://www.w3docs.com/quiz-start/php-basic) - [ES6 Basics](https://www.w3docs.com/quiz-start/es6-basic) - [Java Basics](https://www.w3docs.com/quiz-start/java) - [TypeScript Basics](https://www.w3docs.com/quiz-start/ts-quiz) - [Angular Basics](https://www.w3docs.com/quiz-start/angular) - [React Basics](https://www.w3docs.com/quiz-start/react) - [Sass Basics](https://www.w3docs.com/quiz-start/sass) - [Vue.js Basics](https://www.w3docs.com/quiz-start/vuejs) - [NodeJS Basics](https://www.w3docs.com/quiz-start/node) - [Git Basics](https://www.w3docs.com/quiz-start/git) - [SQL Basics](https://www.w3docs.com/quiz-start/sql) - [Python Basics](https://www.w3docs.com/quiz-start/python-basics) - Snippets - [How To NodeJs](https://www.w3docs.com/snippets/nodejs.html) - [How To Linux](https://www.w3docs.com/snippets/linux.html) - [How To AngularJs](https://www.w3docs.com/snippets/angularjs.html) - [How To PHP](https://www.w3docs.com/snippets/php.html) - [How To HTML](https://www.w3docs.com/snippets/html.html) - [How To CSS](https://www.w3docs.com/snippets/css.html) - [How To Symfony](https://www.w3docs.com/snippets/symfony.html) - [How To Git](https://www.w3docs.com/snippets/git.html) - [How To Apache](https://www.w3docs.com/snippets/apache.html) - [How To JavaScript](https://www.w3docs.com/snippets/javascript.html) - [How To Java](https://www.w3docs.com/snippets/java.html) - [How To Vue.js](https://www.w3docs.com/snippets/vue-js.html) - [How To Python](https://www.w3docs.com/snippets/python.html) - Our Books - [Learn HTML](https://www.w3docs.com/learn-html.html) - [Learn CSS](https://www.w3docs.com/learn-css.html) - [Learn Git](https://www.w3docs.com/learn-git.html) - [Learn Javascript](https://www.w3docs.com/learn-javascript.html) - [Learn PHP](https://www.w3docs.com/learn-php.html) - [Learn python](https://www.w3docs.com/learn-python.html) - [Learn Java](https://www.w3docs.com/learn-java.html) - Exercises - [HTML](https://www.w3docs.com/exercise/html) - [JavaScript](https://www.w3docs.com/exercise/javascript) - [Git](https://www.w3docs.com/exercise/git) - [CSS](https://www.w3docs.com/exercise/css) - [PHP](https://www.w3docs.com/exercise/php) - Our Tools - [Code Diff](https://www.w3docs.com/tools/code-diff/) - [JTW Decoder](https://www.w3docs.com/nx/jwt-decoder) - [Color Picker](https://www.w3docs.com/nx/color/color-picker) - [HTML Encoder](https://www.w3docs.com/tools/html-encoder/) - [Browser Feature Detection](https://www.w3docs.com/nx/feature-detection) - [HTML Editor](https://www.w3docs.com/tools/editor) - [Javascript Formatter](https://www.w3docs.com/nx/formatter/javascript) - [Password Generator](https://www.w3docs.com/tools/password-generator) - [Base 64](https://www.w3docs.com/tools/image-base64) - [Number convertor](https://www.w3docs.com/nx/number/base-converter) - [JSON Beautifier](https://www.w3docs.com/nx/json-beautify) - [CSS Beautifier](https://www.w3docs.com/nx/css-beautify) - [Find the Closest Tailwind CSS Color](https://www.w3docs.com/nx/tailwindcss-closest-color) - [Markdown Convertor](https://www.w3docs.com/nx/marked) - [About Us](https://www.w3docs.com/about-us) - [Privacy Policy for W3Docs](https://www.w3docs.com/privacy-policy) [![logo](https://www.w3docs.com/build/images/footer-logo.png)](https://www.w3docs.com/) **Follow Us** [![facebook](https://www.w3docs.com/build/images/facebook.svg)](https://www.facebook.com/w3docs) Company \&copy; W3docs. All rights reserved.
Readable Markdown
While working with [Git](https://www.w3docs.com/learn-git.html), it is often required to get back to one of the older commits to view its state or to delete the commits coming after it. Below, we will discuss the mentioned scenarios. To go back to an older commit temporarily, you can use the [git checkout](https://www.w3docs.com/learn-git/git-checkout.html) command by mentioning the commit hash: ``` git checkout <sha1-commit-hash> ``` The command above will detach your `HEAD`, that is, leave you with no branch checked out. To make commits after detaching from your branch, you need to create a new branch on the basis of that commit by using the command below: ``` git switch -c <new-branch-name> ``` Instead of using the 2 commands above, you can run the command below to create a new branch on the basis of an older commit : ``` git checkout -b <new-branch-name> <sha1-commit-hash> ``` If you want to delete the recent commits existing only on your local repository, run the command below: ``` git reset --hard <sha1-commit-hash> ``` The command above will delete all the recent commits up to the one you have mentioned the hash for. The mentioned commit will be the most recent one In case you have uncommitted local changes on your branch, this command will delete all of them. If there are changes that you want to keep, act as follows: ``` git stash git reset --hard <sha1-commit-hash> git stash pop ``` This saves the modifications, then re-applies that patch after resetting. Merge conflicts may arise if you have modified things which were changed since the commit you reset to. If you mess it up, you can go back anytime. To learn how to do that, check out [How to Undo Git Reset](https://www.w3docs.com/snippets/git/how-to-undo-git-reset.html). In this section, we will demonstrate two ways of undoing the published commits, depending on whether you want to save the history or not. ### Remove the published commits As it was demonstrated in the previous section, it is possible to remove local commits with the `git reset --hard` command. After that, you can run [git push](https://www.w3docs.com/learn-git/git-push.html) with `--force` option and remove the commits from the remote repository. ``` git push --force origin HEAD ``` **\--force** overwrites the remote branch on the basis of your local branch. It destroys all the pushed changes made by other developers. It refers to the changes that you don't have in your local branch. ``` git push --force-with-lease origin HEAD ``` **\--force-with-lease** is considered a safer option that will not overwrite the work done on the remote branch in case more commits were attached to it (for instance, by another developer). Moreover, it helps you to avoid overwriting another developer's. ### Revert the published commits If you have published your work and don't want to reset the branch, you can revert the commits. Use `git revert` so as not to rewrite any history: ``` git revert <sha1-commit-hash> ``` If you don't want to add a separate commit for revert, after reverting you can run `git rebase` in the interactive mode to combine the latest commit with the commit of the revert. To learn how to do that check out [How to Combine Multiple Git Commits into One](https://www.w3docs.com/snippets/git/how-to-combine-multiple-commits-into-one-with-3-steps.html). ``` git rebase -i HEAD~2 ``` `Git revert` belongs to the “undo” category operations, yet it’s not a classic undo command. The `git revert` command is applied to undo changes to the commit history of the repository. The `git revert` command allows you to pick a single commit, converse the changes and, finally, generate a completely new one. In the scope of this tutorial, you will learn how to revert to the previous state by using [Git](https://www.w3docs.com/learn-git.html) commands. Generally, we can distinguish 2 significant advantages of `git revert`. They are as follows: 1. The revert command can be effectively applied to a specific commit. 2. It doesn’t change the history of the project. This means that it’s a completely secure operation for commits, which were already sent to the shared repository. The most significant difference between `git revert` and [git reset](https://www.w3docs.com/learn-git.html) is that the `git revert` command targets a specific commit not removing all the coming commits. On the contrary, using the `git reset` command will remove the overall coming commits. Let’s say you are going to undo a commit with the usage of the `git reset` command, you need to remove the whole commits coming after the targeted one. Generally, `git revert` is the most secure alternative of `git reset`. Although the process may seem quite complicated, it becomes pretty easy after going through it several times.
Shard2 (laksa)
Root Hash9556372864834080602
Unparsed URLcom,w3docs!www,/snippets/git/how-to-revert-a-git-repository-to-a-previous-commit.html s443