Git provides different commands to reset a file to the HEAD. HEAD is the latest commit to the repository. After committing, a file can be changed or edited. If we want to clear or remove all changes and edits for a file we can reset the file to the HEAD. In this tutorial, we examine different ways and commands to reset files to HEAD.
Reset File To Head Using “git checkout”
git checkout command can be used to reset files to HEAD. We should provide the
HEAD and file name to this command like below. In the following example, we reset the file named “main.c” to the HEAD.
$ git checkout HEAD -- main.c
Reset File To Head Using Head Commit ID
Another way to reset file to HEAD is using the HEAD commit ID with the
git checkout command.
$ git checkout 232edaf2452 -- main.c
Reset File To Head Using “git restore”
Git introduced the
git restore command with version 2.23 . We can use the “git restore” command in order to reset the file to the head. We just need to provide the file name to the “git restore” command as a parameter.
$ git restore main.c