Published on

Cleansing the Code - Mastering Single File Reversion in Git



When collaborating with other developers, making changes to files unrelated to your pull request for testing purposes can lead to messy commit histories. Manually undoing those changes line by line and creating new commits is time-consuming and cumbersome. To tackle this problem, knowing how to revert a single file to a specific commit in Git becomes crucial. This allows for cleaner handling of unrelated file changes, maintaining a streamlined commit history, and efficient code testing and collaboration.

Step 1: Find the Commit ID

  • Run the following command, replacing <path/to/file> with the actual path to the file you're interested in:
git log --follow -- <path/to/file>

Step 2: Revert the File

  • Open a terminal and navigate to the working directory.
  • Use the following command to revert the file, replacing [commit ID] with the actual commit ID and path/to/file with the file path:
git checkout [commit ID] -- path/to/file

Example Scenario Let's consider a scenario where you accidentally introduced a line break in a file named src/js/script.js while making other changes. The file originally had correct formatting. To revert the file to its previous state:

  1. Find the Commit ID:
git log --follow -- src/js/script.js

Take note of the commit ID. Example ec3c5c5fe7efae1846840758d26628529a3ccd8b

  1. Revert the File
git checkout ec3c5c5fe7efae1846840758d26628529a3ccd8b -- src/js/script.js

This command reverts the src/js/script.js file to the state it was in at commit ec3c5c5fe7efae1846840758d26628529a3ccd8b, removing the accidental line break.

  1. Commit the Change: Commit the reverted file using the standard commit command:
git commit -m 'Revert accidental line break in script.js'
  1. Push the Commit: Push the commit to the remote repository to synchronize your branch with the remote version:
git push origin branch-name

Following these steps, you can effectively revert a single file to a specific commit in Git, restoring its previous state and ensuring the code functions correctly.