Home Study How to Roll Back Individual Files and Folders in Git

How to Roll Back Individual Files and Folders in Git

by Frank
How to Roll Back Individual Files and Folders in Git

Introduction

Git is an excellent tool for maintaining each version of your codebase, and it is sometimes required to go back in time and restore older versions of files. Git can roll back changes to a single file or folder as well as retract complete commits or reset the entire repository.

Choosing Between Reverting and Resetting

When you “revert” a commit, Git usually applies a new commit with the opposite modifications, essentially canceling it out. This is handy if you make a mistake and need to “remove” a commit even if it remains in the history.

The procedure for resetting the repository is somewhat different. You may only retract one commit at a time, but if you run a git reset, Git will entirely reset the repository to when that commit was created. This is done for a variety of reasons, the most common of which are to erase commits or correct branch history.

Both of these procedures affect the whole repository, but identical commands may be used to accomplish the same effects on individual files or folders. For example, executing git reset on a single file would restore it to the state it was in when the commit was created. This is beneficial if you just want to choose an older version of the file from your Git history.

Examining Old Git Versions

The low-tech approach to restoring a file to its original state is rather simple—Github and most other Git servers maintain track of your file history, and you can simply click on a commit and choose “Browse Files” to see a snapshot of your repository from the past. You may then either download the file or copy the text.

This is particularly handy if you’re working with a big number of code files and want to compare earlier versions of routines you’ve written. In that scenario, you probably don’t want to reverse the whole thing, just the one function. Without touching the Git CLI, you may copy the code from that function.

Reverting a Git File to an Old Version

We made a commit in this test repository that changed the README and added a new file. We want to undo the README modifications, but we don’t want to restore the whole repo to the first commit.

The answer is to reset just the README by downloading an older version of that file. Git’s checkout command can accomplish a variety of things, such as switching branches, although it’s often used to download data based on a commit or branch ID.

To restore a file to an earlier version, locate the commit ID from when you wish to restore it. You may use git log with a single file scoped to see just the changes made to that file:

git log README.md

Copy the commit ID, then run git checkout with the ID and file path:

git checkout 22710694b25d7ce5297559851beb7d3e4de811bb README.md

This will alter the file back, but it will not yet commit the changes. You are free to make changes and commit when you are ready.

In this case, git checkout marked the modifications as ready for the next commit. If you do not wish to commit the modifications, you may do so. This might be handy for temporarily downloading older versions of files without using Github.

Reverting Changes to Individual Files

Similarly, you may use git revert to undo the modifications made in a single commit. There is no way to apply it to a single file, however if the commit impacts other files, just reject the modifications.

Use the —no-commit switch to enable modification of the “revert commit” that Git generates automatically.

git revert de8564b131ca6a15a7e7c73f5ef156b119cc0b93

This allows you to make changes to the files before completing the reversal. If there are any undesired changes that have been staged, you may erase them using your client or an empty git checkout.

git checkout -- file

You may also like

Navhow is dedicated to teaching people all over the world how to do anything.

 

Subscribe

The Best How-To Newsletter Anywhere

©2022 Navhow, All Right Reserved