Revert Single File in GIT Commit

R

Step by step how to revert a file from a commit:

Where to find the commit ID you’ll need
  1. Navigate to the repo which has the file version you are looking for
  2. Click through until you get to the file in question
  3. Top right of the table you will see the last commit with a commit ID, 7 characters long and cryptic looking
  4. Copy that number (example: 491ce18) and remember the path/to/your/file.php
  5. In your terminal (I like to do this in VSC in the project I’m in), use the following command git checkout 491ce18 -- /path/to/my/file.php
  6. You should now have some changes to stage and commit, these are the revert of the file you picked to the commit you specified
  7. Now check those changes in with git commit -m "Revert of file.php"
  8. You’re all done.

Things to note:

If you’re like me, you only half pay attention most of the time or are going too fast for your own good. It’s ok, it happens to the best of us.

Make sure you don’t grab the commit ID of the commit you need to revert, I’ve done this and it results in several revert commits and mass confusion. Plus you look like an idiot, so that’s not helpful at all.

I’m a big fan of GUI for git but have struggled with this exact operation in most clients, so you may want to get comfortable with command line for stuff like this. I fought it for years but it gets easier by just doing it. I promise.

About the author

bmoredrew

I'm a software developer, product creator, and entrepreneur who focuses on Open Source software, specifically WordPress.

Add Comment

By bmoredrew

Follow Me