DEV Community

Cover image for Github : Reverting a Single File to a Specific Version
Abhineet Raj
Abhineet Raj

Posted on

Github : Reverting a Single File to a Specific Version

In collaborative development, reverting a single file to a particular commit is crucial. This is often necessary when modifying files unrelated to your pull request to test the feature you're working on. Manually changing these files back to their original state can create a messy commit history. A cleaner approach is to revert the file.

Finding the Commit ID

  1. Locate the file: Navigate to the shared GitHub repository and find the file you want to revert.

  2. Identify the commit ID: Above the file, you'll see a 7-digit commit ID and a date. This is the commit ID for the most recent modification of that file. Copy or note this ID.

Finding the File Path

  1. Locate the file path: The file path is displayed on the same GitHub screen where you found the commit ID.

  2. Identify the working directory: Notice that only a portion of the path is underlined. The first directory listed is the working directory name, which is your current directory when using this file path. Therefore, only copy the underlined portion.

Reverting the File

  1. Open a terminal: Open a terminal window and navigate to the working directory.

  2. Execute the revert command: Use the following command to revert the file:

git checkout [commit ID] -- path/to/file
Enter fullscreen mode Exit fullscreen mode
  1. Replace placeholders: Replace [commit ID] with the commit ID you noted down and path/to/file with the actual file path.

Committing the Change

  1. Commit the reverted file: Commit the reverted file using the standard commit command:
git commit -m 'commit message'
Enter fullscreen mode Exit fullscreen mode
  1. Replace placeholder: Replace 'commit message' with a descriptive message explaining the purpose of the commit.

  2. Push changes to remote: Push the committed changes to the remote repository to synchronize your local branch with the GitHub version.

Takeaways

  1. Identify the commit ID of the desired file version.

  2. Locate the file path from the working directory.

  3. Navigate to the working directory in the terminal.

  4. Execute the command git checkout [commit ID] -- path/to/file to revert the file.

  5. Commit the reverted file.

Top comments (0)