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
Locate the file: Navigate to the shared GitHub repository and find the file you want to revert.
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
Locate the file path: The file path is displayed on the same GitHub screen where you found the commit ID.
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
Open a terminal: Open a terminal window and navigate to the working directory.
Execute the revert command: Use the following command to revert the file:
git checkout [commit ID] -- path/to/file
- Replace placeholders: Replace
[commit ID]
with the commit ID you noted down andpath/to/file
with the actual file path.
Committing the Change
- Commit the reverted file: Commit the reverted file using the standard commit command:
git commit -m 'commit message'
Replace placeholder: Replace
'commit message'
with a descriptive message explaining the purpose of the commit.Push changes to remote: Push the committed changes to the remote repository to synchronize your local branch with the GitHub version.
Takeaways
Identify the commit ID of the desired file version.
Locate the file path from the working directory.
Navigate to the working directory in the terminal.
Execute the command
git checkout [commit ID] -- path/to/file
to revert the file.Commit the reverted file.
Top comments (0)