Git

How to Reset File to be Same as Master Branch in Git

On Git, users can access one file on multiple branches and update changes several times and save them. After changing the Git local repository, users must commit changes to the Git remote repository to permanently save and update them for the other project members. This process can be performed using Git commands without hassle.

In this guide, you will learn the procedure to reset files to be the same as the master branch in Git.

How to Reset File to Be Same as Master Branch in Git?

Suppose we have an important file in our Git “master” branch that was not updated. However, it has already been updated and committed multiple times in another branch. Now, we are required to remove all the changes made on the file and revert it to its same state as in the master branch.

Move toward the below-given procedure to understand the above scenario!

Step 1: Launch Git Bash
Open the Git terminal with the help of the “Startup” menu:

Step 2: Move to Git Directory
Navigate to the Git local directory using the “cd” command:

$ cd "C:\Users\nazma\Git\Revert"

Step 3: Create and Switch Branch
Now, create and switch to the new branch immediately by utilizing the provided command:

$ git checkout -b master

Step 4: Create File
Execute the “touch” command to create a new file named “file1.txt”:

$ touch file1.txt

Step 5: Track File
Next, track the file from working directory to the staging area:

$ git add file1.txt

Step 6: Commit Changes
Commit the changes made to the Git repository with a commit message:

$ git commit -m "1 file added"

As you can see, changes are committed successfully to the Git repository:

Step 7: Update File
Open the file to make some changes to it:

$ start file1.txt

Step 8: Commit Changes
Now, save changes using the “git commit” command with “-m” option to specify any message:

$ git commit -m "file1.txt updated"

Step 9: Switch Branch
Next, execute the “git checkout” command to switch back to the previous “main” branch:

$ git checkout main

Step 10: Update File
Open the file using “start” command to update it and press “CTRL + S” key to save changes:

$ start file1.txt

Step 11: Reset File
To reset the changes, execute the “git checkout” command:

$ git checkout master -- file1.txt

Here, we have specified the branch name “master” and used “” before the file name which indicates that the specified text will be interpreted as a file name rather than branch name:

Step 12: Verify Reset Operation
Now, open the file to verify the reset operation:

$ start file1.txt

As you can see, the specified file is opened in the default editor, and it is reset to same as master branch:

Step 13: View Changes
Lastly, execute the “git diff” command to view the difference between the content in the newly reset file:

$ git diff --cached

Here, the “–cached” option is used to display the reset changes:

Let’s check out the procedure to reset the file to a specific commit in the Git.

How to Reset File to Specific Commit in the Git?

Sometimes, users need to reset the file to a particular commit. To achieve that, users can view the changes made between the file versions. Let’s try the below-provided instructions for this purpose.

Step 1: Move to Folder
Execute the “cd” command to navigate to the specific folder:

$ cd "C:\Users\nazma\Git"

Step 2: Create Directory
Create a new Git local directory utilizing provided command:

$ mkdir Linux-Hint

After that, navigate to the newly created Git local repository:

$ cd Linux-Hint

Step 3: Create and Switch Branch
Now, create a new branch and switch to it immediately:

$ git checkout -b alpha

Here, the “-b” flag represents the branch:

Step 4: Create File
Create a new file named “file1.txt” using the following command:

$ touch file1.txt

Step 5: Track File
Next, execute the “git add” command to track the file to the staging area:

$ git add file1.txt

Step 6: Open File
Open the created file, add some text and save it:

$ start file1.txt

Step 7: Create and Switch Branch
Next, create and switch to the new branch named “beta” with the help of “git checkout” command:

$ git checkout -b beta

Step 8: Open and Update File
Open the file, update it in the new branch and save it:

$ start file1.txt

Step 9: Commit Changes
Commit all changes to the Git repository:

$ git commit -m "file1 updated"

Step 10: Check Log History
View the log history of the Git local repository:

$ git log

The output refers to the most recent commit changes:

Step 11: Reset File Using Commit Hash
Execute the “git checkout” command with the commit hash and file name to reset it:

$ git checkout f0e09032ee7cc71e7181f8f4e1e9816f973915c0 file1.txt

As you can see, the changes are reset successfully:

You can also perform the same task using the “git reset” command as follow:

$ git reset f0e09032ee7cc71e7181f8f4e1e9816f973915c0 file1.txt

As you can, the current status of “file1.txt” is “M” which indicates that it is modified and is reset as before:

We have compiled the method to reset files to be same as a master branch in Git.

Conclusion

To reset the file to be the same as the master branch in Git, create a Git local repository. Then, create and add files to it. Next, create and switch to the new branch immediately. Open the file in a new branch, update it, and save it in Git. Execute the “$ git checkout master — file_name” command to reset the file. To reset the file to a specific commit in Git, utilize the “$ git reset ” or “$ git checkout” command. This guide illustrated the procedure to reset files to be the same as the master branch in Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.