Hard Reset of a Single File | Git
The post discusses the procedure to hard reset a single file.
How to do Hard Reset of a Single Git File?
To do a hard reset of a single file, first, navigate to the Git repository and create a new file. Then, track it to the repository. Update changes to the repository by committing along with the commit message. Next, update the created file. Commit changes and check repository log history. Lastly, run the “$ git reset –hard HEAD~1” command to hard reset a single file.
Let’s have a look at the implementation of the above-given instructions!
Step 1: Navigate to Git Repository
Run the “cd” command and navigate to the Git local repository:
Step 2: Create File
Create a new file in the local Git repository through the provided command:
Step 3: Track Created File
Now, execute the “git add” command to track a created file by specifying its name:
Step 4: Update Changes
To update and save the added changes, run the “git commit” command with the “-m” option and add the desired commit message:
Step 5: Update File
Next, open the newly created file using the “start” command along with the file name. Update the file and save it:
Step 6: Add Changes to Staging Area
Now, add all made changes to the staging area and update it:
Step 7: Save Changes
Execute the “git commit” command with the “-m” option to add a message and save all changes:
Step 8: Git Log History
View the current Git repository log history by executing the “git log .” command:
The below-highlighted commit is the previous commit to which we want to reset the HEAD pointer for reverting a single file:
Step 9: Hard Reset
To unstage the single staged file, run the “git reset –hard” command with the “HEAD~1” HEAD pointer:
Here, the “HEAD~1” is a particular commit identifier that indicates the previous commit:
Step 10: Verify Hard Reset
To verify the hard reset, execute the “git log .” command:
It can be observed that the most recent commit is reverted, and HEAD points to the previous commit:
We have elaborated on the method to hard reset a single file.
To hard reset a single file, first, move to the Git repository, create a new file, and track it to the repository. Update changes to the repository by committing along with the commit message. Open the created file and update it by adding some text. Commit changes and check repository log history. Finally, execute the “$ git reset –hard HEAD~1” command to hard reset a single file. The post explained the procedure to hard reset a single file.