Skip to content

Instantly share code, notes, and snippets.

@timbaileyjones
Created November 20, 2024 07:33
Show Gist options
  • Save timbaileyjones/bb063f0e0ddcbd54fffa92e6c817dc95 to your computer and use it in GitHub Desktop.
Save timbaileyjones/bb063f0e0ddcbd54fffa92e6c817dc95 to your computer and use it in GitHub Desktop.
Quick script to help you keep track of any uncommitted or unpush changes to ANY repo on your workstation.
#!/bin/bash
for REPO in $( find ~/[A-Za-z]* -type d -depth 2 -name .git )
do
cd "${REPO}"/..
if [ ! -z "$(git status --porcelain)" >/dev/null ]
then
echo ${PWD} ------------
git status --porcelain
echo
if [ ! -z "${*}" ];
then
echo "running ${*} within ${PWD}... type 'exit' to continue, if this command is interactive..."
${*}
fi
fi
done
@timbaileyjones
Copy link
Author

timbaileyjones commented Nov 20, 2024

This is a classic "I scratched my itch, hope it helps" story.

I sometimes lose track of unpushed/uncommitted changes, across multiple projects / clients. To get a handle on this, I wrote this script called git-dirty and put it at /usr/local/bin, with execute permissions. Feel free to change the argument after find to match where you keep your code projects. This version starts with your $HOME directory. If you mostly work with golang, you might set it to $HOME/go/src, or if you work at Amazon, you might find $HOME/workplace/[A-Za-z]*/. useful.

It finds all your git repos under your home directory. For each repo it:

  • checks to see if there are any pending changes.
  • If there are, it will:
    • output the full path of the repo
    • run git status --procelain, and ...
    • if you provide an argument to it, also run that command.

So you could use this script in combination with other commands to do some useful stuff:

  • git-dirty -- just show what you have that isn't committed, no action taken.
  • git-dirty git pull -- make sure you have the latest for each repo. I keep forgetting to git-pull after merging CRs in the web console.
  • git-dirty git reset --hard -- reset each repo back to whatever the latest local commit is.
  • git-dirty git clean -- remove untracked files from the working tree.
  • git-dirty zsh -- spawn into a shell into each "dirty" directory, where you can take any action you want interactively (i.e. git diff, git reset, make edits, commit/push your changes, or even use the Github CLI to create a PR. Just type exit from that shell, and git-dirty will find the next repo with uncommitted or unpushed changes, if any.

Comments welcome...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment