Git

Hard Reset of a Single File | Git

Developers prefer the Git decentralized versioning control system for tracking a code source file of projects. They can add files, make changes, and save them to the remote repository by executing the “$ git commit” command along with the commit message. In some situations, you may want to make additional changes in the committed files and hard reset the most recently committed file. To do so, the “$ git reset –hard HEAD~1” command can be utilized.

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:

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

Step 2: Create File

Create a new file in the local Git repository through the provided command:

$ touch file1.txt

Step 3: Track Created File

Now, execute the “git add” command to track a created file by specifying its name:

$ git add file1.txt

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:

$ git commit -m "First file added"

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:

$ start file1.txt

Step 6: Add Changes to Staging Area

Now, add all made changes to the staging area and update it:

$ git add file1.txt

Step 7: Save Changes

Execute the “git commit” command with the “-m” option to add a message and save all changes:

$ git commit -m "File updated"

Step 8: Git Log History

View the current Git repository log history by executing the “git log .” command:

$ git log .

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:

$ git reset --hard HEAD~1

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:

$ git log .

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.

Conclusion

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.

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.