Git provides different commands to reset a file to the master. In this tutorial we examine the “git checkout” and “git restore” commands in order to reset or revert a file to the master.
Reset File To Master with “git checkout”
The git checkout
command is the most used command to reset file to master in Git. The origin/master
is provided as the commit and branch name and the file name is provided as parameters to the “git checkout” command. In the following example we reset the “main.c” into the master.
$ git checkout origin/master main.c
Alternatively, we can provide the relative path and file name like below.
$ git checkout origin/master engine/main.c
Reset File To Master with “git restore”
Recent versions of the git also provide an alternative way to reset file to master. The git restore
command is provided with Git 2.21 and the “git restore” command can be used to reset the file to master. We should provide the --source HEAD
as a parameter to the “git restore” command.
$ git restore --source HEAD main.c
Alternatively, we can provide the relative path and file name like below.
$ git restore --source HEAD engine/main.c