Git recipe: Reset file to master branch

Version control software like git makes a software developer’s life easier by automatically keeping track of file changes as well as allowing every team member to collaborate on the same version. Sometimes, you need to get just a single file back to where you were before, or match the contents of that file in another branch. In this article, we’ll take a quick look at how to reset file contents to match its previous state in master branch.

Scenario : You have a file named cool_stuff.py that was committed several times to dev branch, . Now you want to discard all those changes you’ve made on the file and reset its contents to match the master branch.

In order to reset a single file to match master branch, or any other branch, you have to collect the commit ID of the commit you want to roll back to. Obviously, you will also heed to have the path of the file you want to roll back in hand.

Find the commit ID

Git identifies every commit by an ID, which is SHA-1 hash of important information about the commit. Those information includes the files diff check results, date, time, name/address of the commiter, etc.

If you’re using Github, you can easily find this information by browsing the repository using the web interface.

First you need to go to the repository on GitHub, choose a branch, a commit and find the file that you want to revert. Once you navigate to the file, in the upper right corner of the file preview page, you will see a 7 digit commit ID and a date. Either write it down or copy that commit ID to the clipboard since we will need to put it in the command line later.

Alt Text
In case you don’t use Github or prefer using git CLI, you can run the following command to get all the commit ID as well as commit description right in the terminal. Remember to switch to your desired branch using git checkout before doing this.

git log --oneline

Please note that you can drop the --oneline flag, but the output is going to be significantly longer and harder to get an overview of the branch. The output may look something like below.

Reset single file to master

You can use the following command to reset a file to its previous version in commit commit_id (which git will automatically correlate it to the master branch)

git checkout <commit_id> -- <file_path>
Code language: Shell Session (shell)

Do note that there are two dashes followed by a space in the command above. The -- part tells git to recognize anything after it as filenames (not branch name or switches).

In most cases you want to checkout to the commit before the most recent ones. You can use the following syntax to do that.

git checkout <commit_id>~1 -- path/to/file
Code language: Shell Session (shell)

~1 here is reference to the commit’s first parent, more info.

Alternatively, you can also leave the commit hash out, for example, go back to the most recent commit (the HEAD):

git checkout -- path/to/your/file
Code language: Shell Session (shell)

Commit changes after reset

At this point, the changes in the file are not commited just yet. To see what changes were reset, you can run the following command to see the difference,

git diff --cached
Code language: Shell Session (shell)

Once you’re done reviewing differences, you can commit the changes to the repository like you usually do.

git commit -m 'a random message'
Code language: Shell Session (shell)

We hope that the article helped you learned how to quickly reset a file to master branch in Git. We’ve also written a few other guides for Git, such as .gitignore in VSCode, Fix Git error “fatal: Authentication failed” or accept all current/incoming changes in Git. If you have any suggestion, please feel free to leave a comment below.

Click to rate this post!
[Total: 0 Average: 0]

Leave a Comment