Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

.bash_prompt: better git functions #2 #155

wants to merge 6 commits into


None yet
4 participants

eins78 commented Nov 24, 2012

Duplicate, original closed itself: 😟
PR #143

BTW: if you want to use git outside of your prompt, you can just source the functions provided by git itself
Here the relevant functions are extracted because it would be overkill to source it all.

necolas and others added some commits Nov 4, 2012

bash: Update custom Bash prompt
Use git commands to directly enquire about the state of the working
tree. Where possible, git plumbing commands are used. In some instances,
git porcelain commands are used (where plumbing commands fail on freshly
initialized repos) but there is no reliance on string data in their

The previous method relied on specific strings in the output of commands
(`status` and `branch`) which was very unreliable. Those commands make
no guarantees about the nature of the text they return and should not be
used for scripting purposes.

Enhancements included in this update:

* Display the short SHA of a commit when there is no branch name.
* Display the `$` character when there are stashed changes.

.bash_prompt: call 'git' directly, without aliases
Don't call `git`, call `command git`.

Otherwise the prompt could be slow if you aliased a git helper like hub, see <https://github.com/defunkt/hub#is-your-shell-prompt-slow>.

@mathiasbynens mathiasbynens and 2 others commented on an outdated diff Nov 24, 2012

+ echo $branch_name
+# Git status information
+function prompt_git() {
+ local git_info git_state uc us ut st
+ if ! is_git_repo; then
+ return 1
+ fi
+ git_info=$(get_git_branch)
+ # Check for uncommitted changes in the index
+ if ! `command git diff --quiet --ignore-submodules --cached`; then
+ uc=""

mathiasbynens Nov 24, 2012


I don’t get it. When does uc get a value other than the empty string?


nvartolomei Nov 24, 2012




mathiasbynens Nov 24, 2012


That’s what I’m not getting :) Then why bother?


eins78 Nov 24, 2012


I set to empty string to match your original style, but left it in so it is easier to adapt for other people.
The original style only showed a * when branch is dirty.

The new code checks for much more, if you want to assign a symbol, just pick one.

Compare with @necolas version: https://github.com/necolas/dotfiles/blob/master/bash/bash_prompt#L79-L96


eins78 commented Nov 24, 2012

I added a comment to clarify how to use the different checks with symbols.
If you fill out all the symbols like @necolas did, your branch could look like:


(if your branch has staged, changed but unstaged, untracked and stashed files)

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