1

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.

3
  • Why not? It's a simple solution that lets you control the result. If you don't want an alias, then my next best off-the-cuff solution would be a pretty ugly kluge - I'd probably set a trap on debug to scan the last executed command and run git status if it was a cd into */work. That's automatic, but subject to bugs and a lot of tinkering. Commented Oct 6, 2017 at 14:55
  • I try to force myself to make scripts. But I'll make an alias! Commented Oct 6, 2017 at 14:58
  • 1
    Exercise all the bash muscles! :D Commented Oct 6, 2017 at 15:11

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Don't forget to accept the answer that did what you wanted.
2

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 } 

4 Comments

Thank you, that's interesting, I won't have to make different aliases for other repos. I didn't even think about overwriting the default command, I'll remember that possibility next time!
I like this. I think it would drive a lot of people crazy because they hop around directories inside a repo, but I almost never do; I have aliases that scan for relevant files, and I open them with relative path so that one alias can add/commit/push from the repo root and get all my changes without having to climb in and out of subdirs. For me, this is awesome.
@PaulHodges If you just take the first part of the conditional, it will just work on the root directory. I'll post an update.
personal edit - cd () { [[ -n "$1" ]] && builtin cd $1 || builtin cd; [[ -d .git ]] && git status; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.