How to revert to origin's master branch's version of file
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When working with Git, you might find yourself needing to revert a file to a specific version from a branch, such as the master branch on the origin remote. This can be particularly useful when you've made changes to a file, but later decide that the version in the master branch is the one you need.
This article will walk you through the steps required to revert a file to the version in the origin's master branch using Git. By the end, you'll understand not only the commands required but also the underlying concepts of how Git handles branches, commits, and checking out files.
Key Concepts
Before diving into the steps, it's essential to understand a few basic concepts:
- Git Repository: A storage space for your project which includes all the files needed along with a complete history of changes.
- Remote: A common shared repository where team members can push/pull changes. It's typically hosted on platforms like GitHub or GitLab.
- Master Branch: Often used as the main or default branch in many repositories, though newer repositories may use
mainas the default branch name. - Checkout: A Git command used to switch branches or restore working tree files.
Step-by-Step Guide
- Ensure Up-to-Date Local RepositoryFirst, ensure your local repository is up-to-date with the remote:
- Difference Between
resetandcheckout: While both commands can be used for reverting changes,resetaffects the state of branches and can rewrite commit history, whereascheckoutis used mainly for changing the state of files or switching branches. - Conflict Handling: Should you encounter conflicts during the checkout, Git will alert you. You must manually resolve these file conflicts before proceeding with committing the reverted file.
- Reverting Entire Directory: If needed, you can revert an entire directory:
- Alternative Method Using
git restore(Git 2.23+):

