Almost any repetitive action that you do over and over can be automated. It’s not always the big things that need automation - sometimes all you need is a little helping hand. PowerShell is great for this - here’s an example.
For various reasons, I commonly have the same git repo checked out multiple times in sibling directories - there’s one repo at work that I’ve cloned five separate times. There are times when this works much better than having multiple active development branches in a single working folder.
The major problem with this approach is knowing which directory is being used for which feature. This (abbreviated) directory listing doesn’t really inform, even though it only includes a couple of duplicates:
What I need to know is which branch is checked out in each directory. Having to manually
cd into each folder to check is relatively time consuming, and its just in the way of getting to work.
It would be really helpful to see which git branch is checked out in each folder. The obvious solution would be to create a helper script, perhaps
dir-branches.ps1 that shows a directory listing including branch name.
I took a different route, one that doesn’t require me to remember the name of a custom script.
Did you notice the
.ps folder in the above directory listing? That folder contains a script called
after-cd.ps1 that’s run automatically when I enter the folder.
How does this work? In the midst of my PowerSell prompt function, I added the following snippet:
Whenever I change directory - and the prompt is shown for the first time in the new location - I check to see if there is a script called
after-cd.ps1, and if there is, it is run.
C:\GitHub folder, the
.ps/after-cd.ps1 script queries for all the active git branches and displays a table:
When I enter the folder, a listing of the folders and branches is shown automatically:
Recognition is much more powerful than memory, so I can immediately move into the folder I want and get to work. This is much faster than trying to remember the right directory, and then having to poke around manually when I choose the wrong one.