I would like to launch : $ git status when I cd into ~/work
I thought about putting an alias that would cd me into the work directory and launch the git status, but I don't find that solution to be optimal.
Add this to your .bashrc:
gitstatusinwork() { if [[ "$PWD" != "$MYOLDPWD" ]]; then MYOLDPWD="$PWD" if [[ "$PWD/" = ~/work/* ]]; then if [[ "$OLDPWD" != ~/work/* && "$INWORKDIR" == 0 ]]; then git status fi INWORKDIR=1 else INWORKDIR=0 fi fi } export PROMPT_COMMAND="$PROMPT_COMMAND; gitstatusinwork" This function executes git status as soon as your enter your ~/work directory (or any of its subdirectory), then never displays it again, unless you get out of the ~/work directory and re-enter it again.
The easiest way is:
cd ~/work && git status I don't think you're looking for this though.
Another option would be overwriting the default cd command. You could place a bash function at the end of your .bashrc or .bash_profile file like so:
cd() { builtin cd "$1" # detect if the current directory is a git repository if [ -d .git ] || git rev-parse --is-inside-work-tree 2> /dev/null > /dev/null; then echo ""; git status fi } I hope this helps.
Update:
If you just want to see the git status when you cd into the root of your repo, you can just use the first part of the conditional like so:
cd() { builtin cd "$1" # detect if the current directory is a git repository if [ -d .git ]; then echo ""; git status fi } cd () { [[ -n "$1" ]] && builtin cd $1 || builtin cd; [[ -d .git ]] && git status; }
git statusif it was a cd into */work. That's automatic, but subject to bugs and a lot of tinkering.