Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
A heads up display for git (github.com/michaeldfallen)
242 points by michaelallen on Aug 26, 2015 | hide | past | favorite | 60 comments


I use a modified version of mislav's git prompt https://gist.github.com/mislav/1712320 which is pretty minimal but usually enough for me.

For when I have to wrangle lots of files at once (like during interactive rebase to clean up history before push) I have a git watch alias that shows a high-level overview of changes that refreshes with inotify:

  [alias]
      watch = "!clear;inotifywait --quiet -mr -e modify,move,create,delete --format \"%f %e\" @/.git . | \
  while read file; do \
      clear;\
      git status --short;\
      git --no-pager diff --shortstat;\
  done;"
I leave that running in a visible terminal window. It's more verbose than a prompt and reduces the need for constant git status sanity-checking. Maybe useful for someone.


I like your `git watch` alias! That's very slick.

I like my git prompt to show me my branch name, colored with grey (no changes), red (unsaved changes), or green (staged changes), with the depth of the branch, e.g.:

    gknoy@host:~/dev/foo/[bar-feature:4]$ 

My prompt is based on: https://gist.github.com/tobiassjosten/828432

... but I've made slight modifications:

    # I sometimes have very long branch names.
    # I don't assume it's a hash:
    if [ ${#GIT_BRANCH} -gt 40 ]; then
        # GIT_BRANCH="(no branch)"
	GIT_BRANCH="${GIT_BRANCH:0:40}..."
    fi
and, at the end of `git_prompt`:

    branch_depth=`git rev-list HEAD --not --remotes|wc -l`
    echo "[$git_color$GIT_BRANCH$c_reset:${branch_depth}]"


I was missing a dot before /.git and can't edit the parent anymore… It should read

  [alias]
      watch = "!clear;inotifywait --quiet -mr -e modify,move,create,delete --format \"%f %e\" @./.git . | \
  while read file; do \
      clear;\
      git status --short;\
      git --no-pager diff --shortstat;\
  done;"
Sorry!


I'm going to steal this, thanks. I'll probably make it a script file instead of an alias, though.


Post the Gist when you're done, please?


How would this need to be changed to work on osx?


Just need to replace inotifywait with an FSEvents-based change watcher.


watchman (https://github.com/facebook/watchman) is cross platform.


It's less efficient, but you could just use a timed solution like watch:

   watch -n 2 'git status --short; git --no-pager diff --shortstat;'


This works on OSX:

  while :; do clear; git status --short; git --no-pager diff --shortstat; sleep 2; done


On my machine (archlinux, xterm) it flickers, as if there was a loop. Anybody sees the same ?


If you add an

  echo "$file"
inside the while loop, what do you see?

I deliberately set the .git directory to be ignored (using @) to avoid tons of repeating when the index.lock file is repeatedly recreated. Maybe that's what's going on here.


index.lock CREATE index.lock DELETE

alternating very fast.


Agh, it's my fault.

I must have fumbled a key after pasting, as there was a dot missing; @/.git should read @./.git.

I'll add a note to the ancestor comment if I still can. Thanks and sorry!



This is neat and I think there's a lot of potential here. As with many information displays, though, it's critical to consider what the most important information to convey is and how to effectively do it.

My main question is around the use of color. I'd argue the error states - conflicts, diverging branches, etc - should be the ones in red, since those are the issues you want to call the most attention to.

Getting rid of any chartjunk is the other big thing. Using four characters of every prompt just for `git:` is not reasonable. And as much as I like the idea of being warned about untracked files, I fear that in most real situations you end up with random scratch files in the same directory. My prompt would always say `7A` at the end, wasting more space (and mental effort!).

Good work!


Thanks! Glad you like it.

Fair point on the `git:(`, it's mostly a hold over from robbyrussells oh-my-zsh theme (which inspired me to make the first version of this about 2 years ago (https://github.com/michaeldfallen/oh-my-zsh/blob/master/them...).

The entire thing is composable so if you want a prompt without those bits just fork and modify: https://github.com/michaeldfallen/git-radar/blob/master/prom....

Or should I be making these "pieces" like `git:(` and `)` configurable through args / env vars?

On the untracked files I personally never leave a file untracked. I either commit it or add it to .gitignore. Though I see how you use git differently, how about a --ignore-untracked to ignore untracked files?


You could try to make more of the prompt configurable but that just seems like more work for yourself. It may be that our goals are just different.

I'm totally on board with you about trying not to leave things untracked. It seems like things always accumulate that shouldn't be committed but are useful; I should probably just move them to a different root folder.

Here's the prompt I use[0], adapted from some shell script I found online somewhere. I'm not going to link to the code because it's kind of ugly but it is up on my github.

A clean repository is subdued but readable blue. When there are unstaged changes there's a red plus, and staged changes get a green plus (some of both is yellow). Any branch discrepancies at all are shown by writing the branch name in bold red.

I don't try to give myself too much information, making the assumption that you'll have to do some digging to get enough detail to actually do anything about the situation.

[0]: http://i.imgur.com/sttMpfQ.png


This is good stuff: I've already added it to my dotfiles git repo that sync between machines I use. I would like, however, an option to turn off the 'git:' bit. Chartjunk is exactly what it is :)


Will do. I'll add the ability to change the prefix/suffix.


Consider sending these changes upstream to contrib/completion/git-prompt.sh in git.git. It already has a lot of toggles for adjusting the prompt. These things you've added could be added as options.


If you like this, I recommend oh-my-zsh [1] or prezto[2], both have themes for things like this.

[1]: https://github.com/robbyrussell/oh-my-zsh

[2]: https://github.com/sorin-ionescu/prezto


I personally use the agnoster theme from oh-my-zsh. With the required powerline-patched font, its very readable and has enough git info to keep me informed without taking up too much space:

https://gist.github.com/agnoster/3712874


I like the appearance of prompts like this, and I've tried them a few times, but I always find myself turning them back off the first time I cd into a large git repository and have to wait a full second or two for the prompt to return. git is fast, but the prompt needs to show up instantly, and git isn't instantaneous on repositories the size of Linux or Chrome.


Have you thought about using an asynchronously rendered prompt?


That sounds disconcerting and distracting; I don't want my prompt to change while I'm typing a command. If I already have a command prompt, I can type "git status" easily enough.

Also, how would that work?


It's not that bad actually - I've been using one since February. Here's a demo (+ code): http://www.anishathalye.com/2015/02/07/an-asynchronous-shell...


Ah, I see. Interesting zsh magic, and the right-prompt mechanism makes it more palatable. Two issues, though. First, I use bash. Second, and more importantly, you're using the same temporary file for all shells, so the prompts from different shells (in different working directories) will overwrite each other.


Yeah, not sure if/how to do this in bash :P

Yeah, I thought about using a different temp file per shell, and I did use that for some time, but that got annoying when shells didn't exit gracefully and clean up the temp file.

I don't actually care about race conditions (okay, the wrong prompt may be displayed once, big deal), and it doesn't actually happen in real use because of the way timing works out.


liquidprompt https://github.com/nojhan/liquidprompt is another viable alternative.


I use https://github.com/magicmonty/bash-git-prompt which I also like. It seems to present less information than this one though


I use this one as well and it's great, mostly for that "did I remember to commit and push before I go home" check.


Unfortunately, it is lacking a feedback for conflicts.


I use zsh-vcs-prompt [0], which also supports hg and svn.

[0] https://github.com/yonchu/zsh-vcs-prompt


I use oh-my-git for a similar purpose. https://github.com/arialdomartini/oh-my-git Took some wrangling to get the fonts to work, but I find it to be quite helpful. The README is especially nice.


The comic-book-style intro on that page is awesome!


Wouldn't custom fonts lead to a problem when using ssh ?


I just got the fonts working in iTerm2 and after that I didn't have any issues that I recall.


https://github.com/dahlbyk/posh-git has had something similar for awhile - very useful!


Push Git is great! I've been looking for a comparable solution for *nix for a while. Zsh's git completion/prompt just showing the branch and dirty state isn't enough.


You could negate the need for a --bash or --zsh flag by checking $SHELL:

  echo $SHELL | egrep -o '[a-z]+$'
It might also make sense to bundle everything under one file as well. While I'd normally advocate separating code into smaller and more manageable files, a single file shell script would be more convenient to install and would require less disk reads per every prompt call.

Looks good though. I'm definitely going to use this on my dev boxes.


I did try that on a previous tool Butler (https://github.com/michaeldfallen/butler) but I found that some shells don't actually set `$SHELL`.


Some don't, but $SHELL is generally accurate enough for Bash and Zsh.

You can also check the shell by checking the PID:

  ps -p $$
But then you need to do extra output parsing plus, obviously, ps each time you output $PS1. Which is going to be a little overkill for this project since it's only Zsh and Bash you're wanting to capture and you can always have fallback support for those flags when automatic detection fails.


You could have called it GitHud ;-)



By a 3 year old project with 5 stars..


I use something similar for my bash prompt:

https://github.com/xtrementl/dev-bash-git-ps1

I wonder if this one is any faster. Waiting for a bash prompt in large repos can be frustrating.


I added logic to mine to check for a .noprompt file and disable the git part of my bash prompt as I generally want it enabled, but a few large repos are too slow with it. You could do something similar, I'm sure.


I wonder if an asynchronous approach could be implemented. Like starting the git status and the likes in a separate process and killing it after a certain timeout if it takes too long.

While I like that there is a .noprompt feature it's mainly a workaround. Maybe there could be a toggle feature where the user can turn on and off the git prompt on demand, wouldn't be hard to implement.

Maybe I will start hacking on it, I have some ideas to optimize it further.


It can be done, and it actually works quite well. See this post for a demo + code: http://www.anishathalye.com/2015/02/07/an-asynchronous-shell...


Well this is awesome, thanks for the link.


Looks very promising! Thanks, we really missed something like this in Git.


I find the ZSH git extension to work very well.


Is that configurable? That is to say, can I change what's printed to the prompt to add more info like # of files added/removed/modified? I looked into it a little bit it looked hard-coded.


yeah it's pretty configurable, the syntax to do so is rather ugly though. I'm using it like this: https://gist.github.com/lorenzhs/c8b442ce831f0211f3d8 which shows me whether I have staged and unstaged changes, the current branch and commit, rebase/... status, etc.

I don't remember from where I got this config, it's a mix of things I found online.


Interesting, thanks!


Thanks :-) It's just something I've been using for a while so very tailored to my needs. YMMV.


Anybody have a translation for the fish shell prompt?


Check out the "bash-git-prompt" project, which includes this fish prompt: https://github.com/magicmonty/bash-git-prompt/blob/master/gi...


This is okay, but I wish OS X had something as nice as TortoiseGit.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: