Black Friday: the Laravel testing course is $149 instead of $249, through Dec 7. See the course →
logo

How to revert a commit for just one folder

Not a one-liner, but still easy

Joel Clermont
Joel Clermont
2025-03-04

From our daily Laravel tips email. Get the next one in your inbox.

In a previous tip, I shared why we don't use return types on controller actions.

Recently, I was helping a company upgrade their Laravel app to a newer version, and I was using Laravel Shift to do the upgrade.

I love how Shift uses atomic commits, so if there's anything I don't want to adopt, I can revert that single commit.

In this case, I wanted to revert the atomic commit introducing return types, but only for the app/Http/Controllers folder. All other folders could keep the return types, so I don't want to revert those.

Here's how I did it. Make sure to do this starting with a clean git status:

  1. git revert -n <commit-hash>

This reverts the whole commit, but the -n prevents it from committing the change. The reverted changes are only staged, so we can work with the revert before committing it.

  1. git reset

This unstages all the changes from the revert, so we can pick and choose just the files we want to revert.

  1. git add app/Http/Controllers

We only want to revert the changes in the app/Http/Controllers folder, so we add just that folder to be committed.

  1. git commit -m "Revert return types for controllers"

We make the commit with a descriptive message.

  1. git restore .

Finally, we restore all the other changes that were reverted, since we don't want to revert those. Success!

It would be nice if git revert had the ability to work with a specific path, but it doesn't, so this is the next best thing.

Here to help,

Joel

P.S. Do you have a different way of doing this? Reply and share, or even better, join the community and discuss it with a bunch of fellow Laravel devs.

Want a tip like this in your inbox every weekday?

Each one takes less than 2 minutes to read. Join 4,000+ Laravel developers who already get them.

email
No spam. Only real-world advice.
Toss a coin in the jar if you found this helpful.