Using for loops to commit changes

December 20, 2015

Sometimes you need to make small changes across many different files. Before you know it, you’re working on something else without committing those changes.

The good practice would be to keep each commit related to a single feature or fix, but often it’s not the case.

Consider the current directory structure:


Imagine that you made a bunch of changes across many files inside of each component. That might include controllers, services, styles, views, etc. At some point, you need to either stash your changes or commit something incomplete.

My quick approach is to use a simple commit message for each module. Using a for loop to iterate over each file or folder, you can commit the changes with a reference to the component.

for f in * ; do git add $f && git commit -m "small fixes and updates to $f" ; done

The end result would be something like:

"small fixes and updates to one-component"
"small fixes and updates to another-component"
"small fixes and updates to yet-another-component"
"small fixes and updates to so-many-components"

Please note that this is a quick solution. You should keep your commits related to one fix or feature as much as possible.

For more information on that, check out git flow for a more streamlined branching model.


comments powered by Disqus