Skip to content

Instantly share code, notes, and snippets.

@leonmax
Last active November 19, 2023 10:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmax/0d6e23b77abe04ed1d6782c88924caf4 to your computer and use it in GitHub Desktop.
Save leonmax/0d6e23b77abe04ed1d6782c88924caf4 to your computer and use it in GitHub Desktop.

Implementing gtree & gcd

TL;DR

add the following line to your ~/.bashrc

alias gtree="fd '\.git$' --prune -utd |  tree --fromfile ."

And then you can use gtree to find all repos under current directory. With some extra effort, one can cd into any repo folder with tab completion.

Details

This gist is from one of my stackoverflow answer for How to quickly find all git repos under a directory

oneliner

I am a big fan of fd as it is so much faster than find.

fd '\.git$' --prune -u -t d -x echo {//}

./group1/repo1
./group1/repo2
./group2/repo3
./group2/repo4

and with tree, it's even better

fd '\.git$' --prune -utd |  tree --fromfile .

.
├── group1
│   ├── repo1
│   ├── repo2
└── group2
    ├── repo3
    └── repo4

alias

now just add an alias to your ~/.bashrc

alias gtree="fd '\.git$' --prune -utd |  tree --fromfile ."

just do gtree to find your repo

bash function w/ completion

If you are crazy like me, you'll write a script like the bash_completion code gcd.sh below

now gcd tab will get you all the repos under the current directory, or gcd when you are deep under a git repo brings you to the root of your git repo.

function gcd {
if [ -z "$1" ]; then
FOLDER=$(git rev-parse --show-toplevel)
else
FOLDER="$1"
fi
cd "$FOLDER"
}
function _list_repos {
fd '\.git$' --prune -utd -x echo {//} | cut -d/ -f2-
}
function _gcd_complete {
local cur=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=( $(compgen -W "$(_list_repos)" -- "${cur}") )
}
complete -F _gcd_complete gcd

To pull from all the repos

for d in `fd '\.git$' --prune -utd`; do
  pushd $d/..
  echo -e "\n${HIGHLIGHT}Updating `pwd`$NORMAL"
  git pull
  popd
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment