{"id":1878,"date":"2021-06-10T13:22:39","date_gmt":"2021-06-10T13:22:39","guid":{"rendered":"https:\/\/blog.gitprotect.io\/?p=1878"},"modified":"2025-06-13T07:36:36","modified_gmt":"2025-06-13T07:36:36","slug":"git-revert-file-to-previous-commit","status":"publish","type":"post","link":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/","title":{"rendered":"Git Revert File to Previous Commit: How to Do It?"},"content":{"rendered":"\n<p>Everyone who uses version control systems sooner or later has to face the task of restoring a specific version of one particular file at some point. Understanding the commit history is crucial when using <a href=\"https:\/\/gitprotect.io\/blog\/git-commands-list-with-examples\/\" target=\"_blank\" rel=\"noreferrer noopener\">Git commands<\/a> to manage changes effectively. Frequently, it must also be a <a href=\"https:\/\/gitprotect.io\/blog\/github-backup-essentials-download-a-single-file-from-github\/\" target=\"_blank\" rel=\"noreferrer noopener\">single file<\/a> from several versions before. The built-in tools in Git allow such operations, but whoever uses them often knows that the&nbsp;<em>\u2013force<\/em>&nbsp;flag when using&nbsp;<em>git checkout<\/em>&nbsp;or the&nbsp;<em>git reset \u2013hard<\/em>&nbsp;git command is handy.<\/p>\n\n\n\n<p>Let&#8217;s solve our problem once and for all. So, there is no need for you to search for &#8216;how to git revert file to the previous commit (or last commit)&#8221; on the internet. <\/p>\n\n\n\n<!--more-->\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/gitprotect.io\/sign-up.html\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" width=\"1024\" height=\"536\" src=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template-1024x536.png\" alt=\"Get free trial\" class=\"wp-image-2555\" style=\"width:512px;height:268px\" srcset=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template-1024x536.png 1024w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template-300x157.png 300w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template-768x402.png 768w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template-400x209.png 400w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2022\/03\/LinkedIn-ads-template.png 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to git revert file to previous commit<\/h2>\n\n\n\n<p>In the case of Git, we know that we have the<strong>&nbsp;git checkout<\/strong>&nbsp;command at our disposal, and thanks to it, we can restore the specific file to the desired state using the script presented in Listing 1.<\/p>\n\n\n\n<p>The script presents various approaches to searching for a change of interest and uses the&nbsp;<em>git checkout<\/em>&nbsp;command. The command git checkout file can revert a file to a previously committed version without affecting the HEAD reference, allowing users to discard recent changes while still being able to inspect previous states of the file. Additionally, the command git checkout HEAD foo can be used to manage file revisions by checking out a specific file or version from the HEAD of the repository, making it practical for recovering previous versions of files without committing unwanted changes. In the first and third approaches, we have to choose which&nbsp;specific <em>commit&nbsp;<\/em>is the target&nbsp;<em>commit&nbsp;<\/em>at some point. Thanks to this, we can find and select the most appropriate version, but it should be noted that we have to choose the correct change ourselves using the&nbsp;<strong>git log and git diff commands<\/strong>. You should also remember that&nbsp;<strong>we can restore with the command git ls-files -d | xargs git checkout<\/strong><em>&nbsp;\u2014<\/em>&nbsp;files in a given folder.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">What with special cases?<\/h3>\n\n\n\n<p>Here&#8217;s a little mystery, does this command work on deleted files with the&nbsp;<em>git rm<\/em>&nbsp;command? The truth is, however, that we don&#8217;t have to worry about such questions anymore because&nbsp;<a href=\"https:\/\/gitprotect.io\/pricing.html\" target=\"_blank\" rel=\"noreferrer noopener\">GitProtect<\/a> <a href=\"https:\/\/gitprotect.io\/azure-devops-backup.html\" target=\"_blank\" rel=\"noreferrer noopener\">DevOps backup<\/a> is there to solve it for us!<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git init\n# We need to find here a change that interests us.\n$ git log --diff-filter=D --summary\n$ git checkout 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86~1 -- kafej.xml\n\n--or--\n\n$ git init\n# We will restore to the last version of the file here.\n$ git rev-list -n 1 HEAD -- kafej.xml\n$ git checkout 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86~1 -- kafej.xml\n\n--or--\n\n$ git init\n# find interesting us commit\n$ git log\n# review the changes made to a file\n$ git diff 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86 kafej.xml\n$ git reset 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86 kafej.xml --hard\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"listing-1-finding-and-restoring-files-with-the-git-checkout-command\" style=\"font-size:14px\">Listing 1. Finding and restoring files with the git checkout command.<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">Git checkout and git restore &#8211; a quick introduction<\/h2>\n\n\n\n<p>We must also remember that the&nbsp;<em>git checkout<\/em>&nbsp;command is not the only built-in tool in Git that will allow us to restore changes, undo things or modified file, etc.&nbsp;<\/p>\n\n\n\n<p><strong>Another exciting command that we can use is <a href=\"https:\/\/gitprotect.io\/blog\/can-git-restore-a-file\/\" target=\"_blank\" rel=\"noreferrer noopener\">git restore<\/a><\/strong>. Still, you have to remember that even the <a href=\"https:\/\/git-scm.com\/docs\/git-restore\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">official Git documentation<\/a> says that this tool is experimental, and you should never forget that. The name can be confusing, too, as using&nbsp;<em>git restore<\/em>&nbsp;is not used to&nbsp;<a href=\"https:\/\/gitprotect.io\/blog\/how-to-undo-a-commit-in-git\/\" target=\"_blank\" rel=\"noreferrer noopener\">undo changes to a repository<\/a>&nbsp;but to undo local changes to a local disk. This means that we can, for example, undelete deleted files from the local disk from the git repo, but not files in the <a href=\"https:\/\/gitprotect.io\/blog\/github-repository-vs-project\/\" target=\"_blank\" rel=\"noreferrer noopener\">git repository<\/a> itself.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git status\nOn branch master\nYour branch is ahead of 'origin\/master' by 3 commits.\n\t(use \"git push\" to publish your local commits)\n\nChanges are not staged for commit:\n\t(use \"git add &lt;file&gt;...\" to update what will be committed)\n\t(use \"git restore &lt;file&gt;... \" to discard changes in the working directory)\n        \tmodified:   kafej.xml\n$ git restore kafej.xml\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"listing-2-using-the-git-restore-command-to-restore-a-local-file-from-a-repository\" style=\"font-size:14px\">Listing 2. <strong>Using the git restore command to restore a local file from a repository.<\/strong><\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large is-resized\"><a href=\"https:\/\/gitprotect.io\/devops-backup-academy.html\" target=\"_blank\" rel=\" noreferrer noopener\"><img decoding=\"async\" width=\"1024\" height=\"346\" src=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange-1024x346.png\" alt=\"DevOps Backup Academy\" class=\"wp-image-6683\" style=\"width:500px;height:auto\" srcset=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange-1024x346.png 1024w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange-300x102.png 300w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange-768x260.png 768w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange-400x135.png 400w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/banner_orange.png 1200w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to revert a file with git revert?<\/h2>\n\n\n\n<p>Another interesting tool is <strong>git revert, which will allow us to undo the changes and leave a note about them<\/strong>. This means that it can make a git restore of the changes from a given git <em>commit<\/em> and at the same time make a new commit that undoes the changes made by a previous commit. It is important to include a clear commit message when committing changes after a revert to document the nature of the changes made, particularly when pushing to a remote repository. Thanks to this property this command is used very often. Thus, we can keep documentation and information about our project according to actual events.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why choose git revert<\/h3>\n\n\n\n<p>This approach is very desirable and very similar to supporting Electronic Document Management (EDM), one of the main assumptions of complete transparency about changes in documents. This approach is mentioned as one of the best practices in creating projects.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ git init\n$ git add kafej.xml\n$ git commit\nhint: Waiting for your editor to close the file...\n&#91;master (root-commit) 0672413] Add file\n 1 file changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 kafej.xml\n\n$ git log\ncommit 06724135d660b9afbde334e61f98113349b52f48 (HEAD -&gt; master)\nAuthor: Kafej &lt;michal.zbyl@gmail.com&gt;\nDate:   Sat May 29 16:55:14 2021 +0200\n\n\tAdd file\n\n$ git revert 06724135d660b9afbde334e61f98113349b52f48\nRemoving kafej.xml\nhint: Waiting for your editor to close the file...\n&#91;master 8c9440d] Revert \"Add file\"\n 1 file changed, 0 insertions(+), 0 deletions(-)\n delete mode 100644 kafej.xml\n\n$ git log\nAuthor: Kafej &lt;michal.zbyl@gmail.com&gt;\nDate:   Sat May 29 16:56:38 2021 +0200\n\n\tRevert \"Add file\"\n\n\tThis reverts commit 06724135d660b9afbde334e61f98113349b52f48.\n\ncommit 06724135d660b9afbde334e61f98113349b52f48\nAuthor: Kafej &lt;michal.zbyl@gmail.com&gt;\nDate:   Sat May 29 16:55:14 2021 +0200\n\n\tAdd file\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"listing-3-an-example-of-using-the-git-revert-command\" style=\"font-size:14px\">Listing 3. An example of using the git revert command.<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><a href=\"https:\/\/gitprotect.io\/git-backup-guide.html?utm_source=blog&amp;utm_medium=blog\" target=\"_blank\" rel=\"noreferrer noopener\"><img decoding=\"async\" width=\"580\" height=\"400\" src=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2024\/03\/gitbackup-guide.png\" alt=\"Git backup Guide\" class=\"wp-image-5056\" style=\"width:460px;height:auto\" srcset=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2024\/03\/gitbackup-guide.png 580w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2024\/03\/gitbackup-guide-300x207.png 300w, https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2024\/03\/gitbackup-guide-400x276.png 400w\" sizes=\"(max-width: 580px) 100vw, 580px\" \/><\/a><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\">Beyond git revert and co. &#8211; trust in backup<\/h2>\n\n\n\n<p>Okay, but is it not the case that all these commands can be confusing, and what if we use&nbsp;<em>git reset \u2013hard<\/em>&nbsp;and understand that this isn&#8217;t the git commit command that we wanted after all?<\/p>\n\n\n\n<p>Git commit, git rollback, git force rollback, git reset command and more&#8230; &#8211; it&#8217;s always good to know your git commands. But there is a fine line between day-to-day programming and security policies and data protection in general. A professional backup solution heavily oriented on the DevOps ecosystem is what you really need when it comes to securing your source code and related metadata.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">GitProtect &#8211; the 1st professional DevOps backup with true Disaster Recovery<\/h3>\n\n\n\n<p>GitProtect backup and Disaster Recovery software offers you support for <a href=\"https:\/\/gitprotect.io\/blog\/top-git-hosting-services\/\" target=\"_blank\" rel=\"noreferrer noopener\">git hosting<\/a> providers like GitHub, Bitbucket and GitLab, Jira. You get automatic backups with the automatic repository sync \u2013 by specific time interval or on-demand. GitProtect gives you full control over retention, compression, encryption, backup window, start delay, and much more. This is also the only solution on the market that offers real Disaster Recovery, ransomware protection, and many restore option, like granular restore, multiple repositories restore, and crossover recovery.<\/p>\n\n\n\n<p>What to read next? <a href=\"https:\/\/gitprotect.io\/blog\/pull-request-vs-merge-request-whats-the-difference\/\" target=\"_blank\" rel=\"noreferrer noopener\">Pull request vs. merge request<\/a>: what\u2019s the difference, best use cases, and examples.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions:<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How do I revert an amended commit?<\/h3>\n\n\n\n<p>To revert a git commit -amend, you should first check the commit history and locate the amended commit. For that, you can use \u2018git log\u2019. Thus, you will identify both the amended commit and the one before it.&nbsp;<\/p>\n\n\n\n<p>Then, use \u2018reset -hard\u2019 or \u2018git reset -soft\u2019&nbsp; to reset the previous commit. Be cautious with \u2018git reset -hard\u2019 though, since it will also reset your actual working directory to that previous commit, as well. It is always better to back up work whenever you use hard resets to make sure that your branch and other related data are safe. In case you want to keep your working directory changes, use \u2018git reset -soft\u2019.&nbsp;<\/p>\n\n\n\n<p>Finally, verify if the reset process was successful with \u2018git reflog\u2019. You should see that the amended commit is not at the top anymore. It\u2019s a good practice to run \u2018git status\u2019 and check if the changes from the commit you amended are staged to avoid potential mistakes.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I remove a file from a commit?<\/h3>\n\n\n\n<p>To remove a file from a commit you can use \u2018git reset -soft\u2019 and then determine the commit before HEAD. The command is as follows: \u2018git reset &#8211;soft HEAD~1\u2019. As a result, you should see files from your most recent commit and therefore, you can commit them. You have your files in the staging area now, so you can move on to removing or unstaging them, with the aforementioned \u2018git reset\u2019 command: \u2018git reset HEAD &lt;file&gt;\u2019.&nbsp;<\/p>\n\n\n\n<p>Then, to delete the file completely from your staging area, you can do so with the \u2018git rm\u2019 command: \u2018git rm &#8211;cached &lt;file&gt;\u2019. Once that\u2019s done, you can move on to committing the changes made by using the \u2018amend\u2019 option: \u2018git commit &#8211;amend\u2019. And finally, you should make sure the files were successfully deleted from your repo. To do so, run the \u2018git ls-files\u2019 command &#8211; which will show you if the file still exists in the file:&nbsp;<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#f4fafe\">\u2018git ls-files<br>&lt;file1&gt;<br>&lt;file2&gt;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I remove a file from the git commit after push?<\/h3>\n\n\n\n<p>The initial steps to delete a file from a git commit are the same whether it has been pushed or not. To start off, you need to check out the past, unchanged state of your file: \u2018git checkout HEAD^ &#8212; \/path\/to\/file\u2019. Then, you commit: \u2018git commit -am &#8220;revert changes on this file, not finished with it yet&#8221;\u2019. Now, you can simply push it without the need for force push: \u2018git push\u2019. Once these steps are done, you can refer back to your unfinished work, and run the following: \u2018git checkout HEAD^ &#8212; \/path\/to\/file\u2019.&nbsp;<\/p>\n\n\n\n<p>Another method to remove a file change from the last commit is \u2018uncommitting\u2019. To revert your file to the state prior to the last commit, run: \u2018git checkout HEAD^ \/path\/to\/file\u2019. Next, you can update the last commit with your reverted file with: \u2018git commit &#8211;amend\u2019. Finally, you push the updated commit to your repository using: \u2018git push -f\u2019.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Can you revert a single file from a commit?<\/h3>\n\n\n\n<p>Yes, you can. The simplest method is \u2018git checkout\u2019. Find the commit hash and the path to the file you want to revert, then run the following command: \u2018git checkout &lt;commit_hash&gt; &#8212; &lt;path\/to\/file&gt;\u2019. Replace \u2018&lt;commit_hash&gt;\u2019 with the actual commit hash and \u2018&lt;path\/to\/file&gt;\u2019 with the path to the file you seek to revert. Next, stage the changes with \u2018git add\u2019, commit them using \u2018git commit -m &#8220;Revert changes to &lt;file&gt;&#8221; and you can now push the changes to your remote repo with \u2018git push\u2019.&nbsp;<\/p>\n\n\n\n<p>Another way is through the use of \u2018git revert\u2019 &#8211; this method grants a cleaner commit history, as when changes are undone from a chosen commit you get a new commit with the revert. Locate the commit hash that brought in the unwanted changes. Then, run \u2018git revert &lt;commit_hash&gt; &#8211;no-commit &#8212; &lt;file_path&gt;\u2019. This reverts changes for the specified file without committing straight away. Same as with the \u201cgit checkout\u201d method, you stage the reverted file and commit the change.&nbsp;<\/p>\n\n\n\n<p>Alternatively, you can leverage backups, by using scripts or opting for a backup vendor such as GitProtect.io, which gives you the possibility of point-in-time restores and granular restores. The solution gives you the option to restore to different destinations, including your original organization, a new organization, and an on-prem device. Also, you can specify what you actually want to restore, like whole projects or just some metadata such as attachments.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to undo the merge of a single file in git?&nbsp;<\/h3>\n\n\n\n<p>To undo the merge of a single file in git, run: \u2018git reset HEAD~1 public\/stylesheet\/application.css\u2019. This way you change the version of a file in the staged version to the version in \u2018HEAD~1\u2019, but your working copy stays unaltered.&nbsp;<\/p>\n\n\n\n<p>Alternatively, you could check out first: \u2018git checkout HEAD~1 &#8212; public\/stylesheet\/application.css\u2019. This changes your working copy along with the staged version to the version from \u2018HEAD~1. The \u2018&#8211;\u2019 is there as a security measure to avoid mistaking your file name with a commit. After that, you can amend your merge commit using: \u2018git commit &#8211;amend\u2019. This will effectively undo the merge of a single file in git, provided that you have not pushed the merge anywhere, as the merge will still just contain the changes from one of the branches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Before you go:<\/h2>\n\n\n\n<p class=\"has-background\" style=\"background-color:#f4fafe\">\u270d\ufe0f Subscribe to <a href=\"https:\/\/gitprotect.io\/gitprotect-newsletter.html?utm_source=blog&amp;utm_medium=blog\" target=\"_blank\" rel=\"noreferrer noopener\">GitProtect DevSecOps X-Ray Newsletter<\/a> and stay current with the newest insights in DevSecOps<br><br>\ud83d\udd0e Check out <a href=\"https:\/\/gitprotect.io\/blog\/why-back-up-devops-tools-what-is-worth-remembering\/\" target=\"_blank\" rel=\"noreferrer noopener\">5 top arguments to start backing up DevOps tools<\/a> as soon as possible<br><br>\ud83d\udc40 Read our blog post&nbsp; <a href=\"https:\/\/gitprotect.io\/blog\/your-own-git-backup-script-vs-repository-backup-software\/\" target=\"_blank\" rel=\"noreferrer noopener\">Your own Git backup script vs. repository backup software<\/a>, and see which backup option suits your needs<br><br>\ud83d\udcc5 Schedule <a href=\"https:\/\/calendly.com\/d\/3s9-n9z-pgc\/gitprotect-live-demo?month=2024-03\" target=\"_blank\" rel=\"noreferrer noopener\">a live custom demo<\/a> and learn more about GitProtect backups to protect your source code on GitHub, Bitbucket, or GitLab<br><br>\ud83d\udccc Or try <a href=\"https:\/\/gitprotect.io\/sign-up.html?utm_source=blog&amp;utm_medium=blog\" target=\"_blank\" rel=\"noreferrer noopener\">GitProtect backups for your GitLab, Bitbucket, GitHub, or Jira ecosystem<\/a> to guarantee data protection and ensure continuous workflow<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Everyone who uses version control systems sooner or later has to face the task of restoring a specific version of one particular file at some point. Understanding the commit history is crucial when using Git commands to manage changes effectively. Frequently, it must also be a single file from several versions before. The built-in tools in Git allow such operations, but whoever uses them often knows that the&nbsp;\u2013force&nbsp;flag when using&nbsp;git checkout&nbsp;or the&nbsp;git reset \u2013hard&nbsp;git command is handy. Let&#8217;s solve our problem once and for all. So, there is no need for you to search for &#8216;how to git revert file [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":1884,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1878","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-git-backup-101","post--single"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io\" \/>\n<meta property=\"og:description\" content=\"Everyone who uses version control systems sooner or later has to face the task of restoring a specific version of one particular file at some point. Understanding the commit history is crucial when using Git commands to manage changes effectively. Frequently, it must also be a single file from several versions before. The built-in tools in Git allow such operations, but whoever uses them often knows that the&nbsp;\u2013force&nbsp;flag when using&nbsp;git checkout&nbsp;or the&nbsp;git reset \u2013hard&nbsp;git command is handy. Let&#8217;s solve our problem once and for all. So, there is no need for you to search for &#8216;how to git revert file [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog | GitProtect.io\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/XoperoSoftware\/\" \/>\n<meta property=\"article:published_time\" content=\"2021-06-10T13:22:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-06-13T07:36:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Michal Zbyl\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@GitProtectio\" \/>\n<meta name=\"twitter:site\" content=\"@GitProtectio\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michal Zbyl\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"10 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\"},\"author\":{\"name\":\"Michal Zbyl\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/52a5c06583d6cc6e418c1afb37653017\"},\"headline\":\"Git Revert File to Previous Commit: How to Do It?\",\"datePublished\":\"2021-06-10T13:22:39+00:00\",\"dateModified\":\"2025-06-13T07:36:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\"},\"wordCount\":1987,\"publisher\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png\",\"articleSection\":[\"Git Backup 101\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\",\"url\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\",\"name\":\"Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io\",\"isPartOf\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png\",\"datePublished\":\"2021-06-10T13:22:39+00:00\",\"dateModified\":\"2025-06-13T07:36:36+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage\",\"url\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png\",\"contentUrl\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png\",\"width\":1200,\"height\":600,\"caption\":\"How to revert a file to the previous commit?\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\/\/gitprotect.io\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Git Revert File to Previous Commit: How to Do It?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#website\",\"url\":\"https:\/\/gitprotect.io\/blog\/\",\"name\":\"GitProtect.io Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/gitprotect.io\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#organization\",\"name\":\"GitProtect.io\",\"url\":\"https:\/\/gitprotect.io\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/favicon-528x528-1.png\",\"contentUrl\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/favicon-528x528-1.png\",\"width\":528,\"height\":528,\"caption\":\"GitProtect.io\"},\"image\":{\"@id\":\"https:\/\/gitprotect.io\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/XoperoSoftware\/\",\"https:\/\/x.com\/GitProtectio\",\"https:\/\/www.linkedin.com\/company\/xopero-software\/\",\"https:\/\/www.youtube.com\/channel\/UCiEnl6n0mIO6w7twccz-l2w\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/52a5c06583d6cc6e418c1afb37653017\",\"name\":\"Michal Zbyl\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/michal-zbyl_avatar-96x96.jpg\",\"contentUrl\":\"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/michal-zbyl_avatar-96x96.jpg\",\"caption\":\"Michal Zbyl\"},\"url\":\"https:\/\/gitprotect.io\/blog\/author\/michal-zbyl\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/","og_locale":"en_US","og_type":"article","og_title":"Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io","og_description":"Everyone who uses version control systems sooner or later has to face the task of restoring a specific version of one particular file at some point. Understanding the commit history is crucial when using Git commands to manage changes effectively. Frequently, it must also be a single file from several versions before. The built-in tools in Git allow such operations, but whoever uses them often knows that the&nbsp;\u2013force&nbsp;flag when using&nbsp;git checkout&nbsp;or the&nbsp;git reset \u2013hard&nbsp;git command is handy. Let&#8217;s solve our problem once and for all. So, there is no need for you to search for &#8216;how to git revert file [&hellip;]","og_url":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/","og_site_name":"Blog | GitProtect.io","article_publisher":"https:\/\/www.facebook.com\/XoperoSoftware\/","article_published_time":"2021-06-10T13:22:39+00:00","article_modified_time":"2025-06-13T07:36:36+00:00","og_image":[{"width":1200,"height":600,"url":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png","type":"image\/png"}],"author":"Michal Zbyl","twitter_card":"summary_large_image","twitter_creator":"@GitProtectio","twitter_site":"@GitProtectio","twitter_misc":{"Written by":"Michal Zbyl","Est. reading time":"10 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#article","isPartOf":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/"},"author":{"name":"Michal Zbyl","@id":"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/52a5c06583d6cc6e418c1afb37653017"},"headline":"Git Revert File to Previous Commit: How to Do It?","datePublished":"2021-06-10T13:22:39+00:00","dateModified":"2025-06-13T07:36:36+00:00","mainEntityOfPage":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/"},"wordCount":1987,"publisher":{"@id":"https:\/\/gitprotect.io\/blog\/#organization"},"image":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage"},"thumbnailUrl":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png","articleSection":["Git Backup 101"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/","url":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/","name":"Git Revert File to Previous Commit: How to Do It? - Blog | GitProtect.io","isPartOf":{"@id":"https:\/\/gitprotect.io\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage"},"image":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage"},"thumbnailUrl":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png","datePublished":"2021-06-10T13:22:39+00:00","dateModified":"2025-06-13T07:36:36+00:00","breadcrumb":{"@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#primaryimage","url":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png","contentUrl":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2021\/06\/rever-prev-commit.png","width":1200,"height":600,"caption":"How to revert a file to the previous commit?"},{"@type":"BreadcrumbList","@id":"https:\/\/gitprotect.io\/blog\/git-revert-file-to-previous-commit\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/gitprotect.io\/blog\/"},{"@type":"ListItem","position":2,"name":"Git Revert File to Previous Commit: How to Do It?"}]},{"@type":"WebSite","@id":"https:\/\/gitprotect.io\/blog\/#website","url":"https:\/\/gitprotect.io\/blog\/","name":"GitProtect.io Blog","description":"","publisher":{"@id":"https:\/\/gitprotect.io\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gitprotect.io\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/gitprotect.io\/blog\/#organization","name":"GitProtect.io","url":"https:\/\/gitprotect.io\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gitprotect.io\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/favicon-528x528-1.png","contentUrl":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/favicon-528x528-1.png","width":528,"height":528,"caption":"GitProtect.io"},"image":{"@id":"https:\/\/gitprotect.io\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/XoperoSoftware\/","https:\/\/x.com\/GitProtectio","https:\/\/www.linkedin.com\/company\/xopero-software\/","https:\/\/www.youtube.com\/channel\/UCiEnl6n0mIO6w7twccz-l2w"]},{"@type":"Person","@id":"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/52a5c06583d6cc6e418c1afb37653017","name":"Michal Zbyl","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/gitprotect.io\/blog\/#\/schema\/person\/image\/","url":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/michal-zbyl_avatar-96x96.jpg","contentUrl":"https:\/\/gitprotect.io\/blog\/wp-content\/uploads\/2023\/05\/michal-zbyl_avatar-96x96.jpg","caption":"Michal Zbyl"},"url":"https:\/\/gitprotect.io\/blog\/author\/michal-zbyl\/"}]}},"_links":{"self":[{"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/posts\/1878","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/comments?post=1878"}],"version-history":[{"count":21,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions"}],"predecessor-version":[{"id":6689,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/posts\/1878\/revisions\/6689"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/media\/1884"}],"wp:attachment":[{"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/media?parent=1878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/categories?post=1878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gitprotect.io\/blog\/wp-json\/wp\/v2\/tags?post=1878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}