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. 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 –force flag when using git checkout or the git reset –hard git command is handy.
Let’s solve our problem once and for all. So, there is no need for you to search for ‘how to git revert file to the previous commit (or last commit)” on the internet.
How to git revert file to previous commit
In the case of Git, we know that we have the git checkout 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.
The script presents various approaches to searching for a change of interest and uses the git checkout command. In the first and third approaches, we have to choose which specific commit is the target commit 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 git log and git diff commands. You should also remember that we can restore with the command git ls-files -d | xargs git checkout — files in a given folder.
What with special cases?
Here’s a little mystery, does this command work on deleted files with the git rm command? The truth is, however, that we don’t have to worry about such questions anymore because GitProtect DevOps backup is there to solve it for us!
$ git init # We need to find here a change that interests us. $ git log --diff-filter=D --summary $ git checkout 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86~1 -- kafej.xml --or-- $ git init # We will restore to the last version of the file here. $ git rev-list -n 1 HEAD -- kafej.xml $ git checkout 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86~1 -- kafej.xml --or-- $ git init # find interesting us commit $ git log # review the changes made to a file $ git diff 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86 kafej.xml $ git reset 9ad202d538c6ee6448e2c1ca1529f0e78edd3f86 kafej.xml --hard
Listing 1. Finding and restoring files with the git checkout command.
Git checkout and git restore – a quick introduction
We must also remember that the git checkout command is not the only built-in tool in Git that will allow us to restore changes, undo things or modified file, etc.
Another exciting command that we can use is git restore. Still, you have to remember that even the official Git documentation says that this tool is experimental, and you should never forget that. The name can be confusing, too, as using git restore is not used to undo changes to a repository 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 git repository itself.
$ git status On branch master Your branch is ahead of 'origin/master' by 3 commits. (use "git push" to publish your local commits) Changes are not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>... " to discard changes in the working directory) modified: kafej.xml $ git restore kafej.xml
Listing 2. Using the git restore command to restore a local file from a repository.
How secure are your repos and metadata? Don’t push luck – secure your code with the first professional GitHub, Bitbucket, GitLab, and Jira backup.
How to revert a file with git revert?
Another interesting tool is git revert, which will allow us to undo the changes and leave a note about them. This means that it can make a git restore of the changes from a given git commit and at the same time make a new git commit informing about this event. It is thanks to this property that this command is used very often. Thanks to this, we can keep documentation and information about our project according to actual events.
Why choose git revert
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.
$ git init $ git add kafej.xml $ git commit hint: Waiting for your editor to close the file... [master (root-commit) 0672413] Add file 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 kafej.xml $ git log commit 06724135d660b9afbde334e61f98113349b52f48 (HEAD -> master) Author: Kafej <firstname.lastname@example.org> Date: Sat May 29 16:55:14 2021 +0200 Add file $ git revert 06724135d660b9afbde334e61f98113349b52f48 Removing kafej.xml hint: Waiting for your editor to close the file... [master 8c9440d] Revert "Add file" 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 kafej.xml $ git log Author: Kafej <email@example.com> Date: Sat May 29 16:56:38 2021 +0200 Revert "Add file" This reverts commit 06724135d660b9afbde334e61f98113349b52f48. commit 06724135d660b9afbde334e61f98113349b52f48 Author: Kafej <firstname.lastname@example.org> Date: Sat May 29 16:55:14 2021 +0200 Add file
Listing 3. An example of using the git revert command.
Beyond git revert and co. – trust in backup
Okay, but is it not the case that all these commands can be confusing, and what if we use git reset –hard and understand that this isn’t the git commit command that we wanted after all?
Git commit, git rollback, git force rollback, git reset command and more… – it’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.
GitProtect – the 1st professional DevOps backup with true Disaster Recovery
GitProtect offers you support for git hosting providers like GitHub, Bitbucket and GitLab, Jira, and soon also Kubernetes, Confluence, Zendesk, and more. You get automatic backups with the automatic repository sync – 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.
What to read next? Pull request vs. merge request: what’s the difference, best use cases, and examples.