- Published on
Cleansing the Code - Mastering Single File Reversion in Git
- Authors
- Name
- Jeevan Wijerathna
- @iamjeevanvj
Problem
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 with the actual path to the file you're interested in:
<path/to/file>
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 with the actual commit ID and
[commit ID]
with the file path:path/to/file
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
- Find the Commit ID:
git log --follow -- src/js/script.js
Take note of the commit ID. Example
ec3c5c5fe7efae1846840758d26628529a3ccd8b
- Revert the File
git checkout ec3c5c5fe7efae1846840758d26628529a3ccd8b -- src/js/script.js
This command reverts the
src/js/script.js
ec3c5c5fe7efae1846840758d26628529a3ccd8b
- Commit the Change: Commit the reverted file using the standard commit command:
git commit -m 'Revert accidental line break in script.js'
- 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.