Coder Social home page Coder Social logo

git-aware-prompt's Introduction

Git Aware Prompt

Working with Git and its great branching/merging features is amazing. Constantly switching branches can be confusing though as you have to run git status to see which branch you're currently on.

The solution to this is to have your terminal prompt display the current branch. There's a number of articles available online about how to achieve this. This project is an attempt to make an easy to install/configure solution.

Overview

If you cd to a Git working directory, you will see the current Git branch name displayed in your terminal prompt. When you're not in a Git working directory, your prompt works like normal.

Git Branch in Prompt

Installation

Clone the project to a .bash folder in your home directory:

mkdir ~/.bash
cd ~/.bash
git clone https://github.com/jimeh/git-aware-prompt.git

Edit your ~/.bash_profile or ~/.profile or ~/.bashrc (for Ubuntu) and add the following to the top:

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"

Configuring

Once installed, there will be new $git_branch and $git_dirty variables available to use in the PS1 environment variable, along with a number of color helper variables which you can see a list of in colors.sh.

If you want to know more about how to customize your prompt, I recommend this article: How to: Change / Setup bash custom prompt (PS1)

Suggested Prompts

Below are a few suggested prompt configurations. Simply paste the code at the end of the same file you pasted the installation code into earlier.

Mac OS X

export PS1="\u@\h \W \[\$txtcyn\]\$git_branch\[\$txtred\]\$git_dirty\[\$txtrst\]\$ "

Optionally, if you want a nice pretty prompt when using sudo -s, also add this line:

export SUDO_PS1="\[$bakred\]\u@\h\[$txtrst\] \w\$ "

Ubuntu

Standard:

export PS1="\${debian_chroot:+(\$debian_chroot)}\u@\h:\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

Colorized:

export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

Windows

export PS1="\[\033]0;$MSYSTEM:\w\007\033[32m\]\u@\h:\[\033[33m\w \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\033[0m\]\$ "

Updating

Assuming you followed the default installation instructions and cloned this repo to ~/.bash/git-aware-prompt:

cd ~/.bash/git-aware-prompt
git pull

Usage Tips

To view other user's tips, please check the Usage Tips wiki page. Or if you have tips of your own, feel free to add them :)

License

CC0 1.0 Universal

git-aware-prompt's People

Contributors

amattheisen avatar eacousineau avatar edwinksl avatar iegik avatar jimeh avatar lethosor avatar matt2000 avatar mikesten avatar nykanenj avatar olafurg avatar spoike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

git-aware-prompt's Issues

CRLF not working properly for long commands with custom PS1

In case of long commands, instead of writing it on next line (Use CRLF i.e., \r\n) when current window is over, prompt starts writing on the same line (just uses LF i.e., \r). Something similar might be happening with vim when I use it. I have registered same issue on unix stackexchange also, see it here. Please help

Cannot resolve $git_dirty inside of $GIT_DIR

find_git_dir() does not play nicely inside of $GIT_DIR, hence the error message checking.
A possible solution would be to resolve the proper work tree directory. Not sure how to do that with the plumbing / porcelain stuff (realized I should have said "plumbing" in that first commit haha). Trying out the "root" alias trick did not seem to work either.

Did not work with Ubuntu 18.04

Hi There,

While it is/was working with other Debian, it did not work with Ubuntu 18.04. Is there anything that I should take care of ?

Value of PS1 variable not set and the three .sh files don't have +x

When pulling the project, the git aware prompt doesn't work "out of the box". When I looked at the value of PS1, it wasn't getting assigned by the values from prompt.sh. Turns out PS1 wasn't being acted on by prompt.sh, so I uncommented the first PS1 example in the script comments at the bottom and it worked.

As a side note, the three .sh scripts don't have eXecute bits set.

They are also missing #!/bin/bash at the top, but they work OK without it.

Prompt shows wrong branch

Here's my configuration:

export PS1="\u@\h \W \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\n\$ "

Right now in my terminal I see the following prompt:

samstern@samstern-macbookpro firebase-tools (master)
$

But when I run git branch you can see I am not on master:

samstern@samstern-macbookpro firebase-tools (master)

$ git branch
  fix-firestore-delete-escaping
  master
* ss-firestore-delete-bugfix

Any idea what's going on here?

Getting some weird prompts.

Hey guys,
Not sure if this is just me but can anyone confirm if it's working for them or it's just me?
I'm using Ubuntu 14.10 and it was working fine but it just stopped working one day. I don't know what I did.
I have cloned git-aware-promp in .bash directory. The following lines of codes are at the end of my.bashrc file

export GITAWAREPROMPT=~/.bash/git-aware-prompt
source $GITAWAREPROMPT/main.sh
export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]@\h\[\033[00m\]:\[\033[01;34m\]t_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ ""]]]"

but I get this:-
weird-prompt
That's the prompt I get without even typing anything.
I'm not super good at changing prompt shells. 😢

git status modifies .git/index - changing ownership

In it's current form, this tool causes negative impact if run as a user that has privileges - it winds up changing the ownership of .git/index in repos if you cd into another account.

It can be mitigated by removing/disabling the 'dirty repo' support by removing the call to 'find_git_dirty' in prompt.sh PROMPT_COMMAND.

I do not know yet of any way to keep git status from modifying that file. Opened a stackoverflow question on the topic, will see if anyone has a suggestion:

https://stackoverflow.com/questions/47436939/how-to-run-git-status-without-modifying-git-index-such-as-in-a-prompt-command

Underline in all elements

When the underline style is apply in one element, for example, in path of the files, All the others elements too stay with underline style.
My personalization: export PS1="\n \[$undblu\]\w \[$txtylw\]\$git_branch\[$txtred\]\$git_dirty\[$bldblu\] \$ \[$txtrst\]"

Result:
bash

I wear Ubuntu 16.04 with native bash

Not show path tittle terminer

When use PS1 = export PS1="${debian_chroot:+($debian_chroot)}[\033[01;32m]\u@\h[\033[00m]:[\033[01;34m]\w[\033[00m] [$txtcyn]$git_branch[$txtred]$git_dirty[$txtrst]$ "
below bashrc,
Teminer lose path folder
lose
notlose

Improvements to the installation documentation

I was using Lubuntu 14.04 and according to the documentation I should

Edit your ~/.bash_profile or ~/.profile and add the following to the top:

However, this does not work. I needed to edit my .bashrc file to make it work. Also, the documentation does not mention that after cloning the repository you should make the scripts executable with chmod command.

Title in Terminator becomes "/bin/bash"

Title of the Terminator in Ubuntu becomes "/bin/bash" after changing PS1 to "${debian_chroot:+($debian_chroot)}\u@\h:\w [$txtcyn]$git_branch[$txtred]$git_dirty[$txtrst]$ "

But adding "[\e]0;\u@\h: \w\a]${debian_chroot:+($debian_chroot)}\u@\h:\w [$txtcyn]$git_branch[$txtred]$git_dirty[$txtrst]$ " as the PS1 variable resolves the issue. I have modified the default PS1 variable with the value provided in readme.

Issue in mac command

Mac OS X

export PS1="\u@\h \W \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

Sq brackets are missing around $git_branch and $git_dirty

\[$txtcyn\]\[$git_branch\]\[$txtred\]\[$git_dirty\]\[$txtrst\]\$

Ubuntu colorized prompt PS1 causes export command to be outputted at start of every shell

I'm a bit clueless when it comes to Bash/Linux so this is probably due to user error, but when I use the colorized ubuntu PS1 I see the following:

�]0;export PS1="\${debian_chroot:+(\$debian_chroot)}\[\]\u@\h\[\]:\[\]\w\[\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "user@host:~ $

whenever I start a new shell.

I copy and pasted:
export PS1="\${debian_chroot:+(\$debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$ "

Directly from the instructions. The non-colorized version has no issue.

Does not work on Ubuntu 18.04

I really loved this little helper, but it seems not to work anymore.

Anyone got an idea how to fix it?

Manually typing the source-command didn't work...
I added the crucial lines to my bashrc-file.

Spacing control

I just found this repo. Very nice! Much appreciated!

Normally, I have this for a prompt:

PS1='[\u@\h \w]\$ '

Using what was provided here, I created this prompt:

PS1='[\u@\h \w]\['\$txtcyn'\]'\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]\$ '

However, I'd prefer to have a space before the name of the git branch. If I insert one into PS1, I always have a space after the normal closing bracket and the $.

So what I did was this:

PS1='[\u@\h \w]\['\$txtcyn'\]'\$git_space\$git_branch'\['\$txtred'\]'\$git_dirty'\['\$txtrst'\]\$ '

The git_space is an empty string when git_branch is empty and contains one space when git_branch is non-empty. I did this by first setting PROMPT_COMMAND to my own script that will inspect $git_branch and assign $git_space appropriately. It works, but I'd prefer to see if I could have something added to the repo so I don't have to do that as an extension.

Would you entertain this change, just adding two git_space assignments?

find_git_branch() {
  # Based on: http://stackoverflow.com/a/13003854/170413
  local branch
  if branch=$(git rev-parse --abbrev-ref HEAD 2> /dev/null); then
    if [[ "$branch" == "HEAD" ]]; then
      branch='detached*'
    fi
    git_branch="($branch)"
    git_space=" "
  else
    git_branch=""
    git_space=""
  fi
}

One could then insert a space before and/or after the repo name as I have done in my PS1 example above. Some might want the same kind of thing between the branch name and $git_dirty. So perhaps we might have $git_branch_space and $git_dirty_space.

I'd be happy to do a PR is this would be of interest.

getting long bash prompt with whole dir hierarchy

Hi Jimeh,

your plugin is great & it is working but I'm getting the whole directory structure in bash prompt like:
mac-user-name: /Applications/XAMPP/htdocs/projectname (branch-name) $

Can I just have the branch-name like:
mac-user-name:projectname (branch-name) $

I'm on OSX El Capitan.
Thanks & thank you for this plugin

Consider CC0 as an alternative to WTFPL

CC0 is effectively equivalent to WTFPL, but sets out the terms of use in more formal language. I've heard that some companies refuse to use certain open-source projects because it's licensed under WTFPL (not because they're prudes, but because it's language is not legally reassuring).

While WTFPL is fine for small hobby projects, larger and more popular projects will benefit from the precise legal language of CC0. (As a side, CC0 is the only Creative Commons license that is recommended for use with software.)

v1.0 / The Future

It's a bit of an understatement that I've mostly ignored this project for the last few years. I haven't personally used git-aware-prompt since I switched from bash to zsh about 5 years ago. And the project was always a bit of quickly put together hack.

However due how much attention it's getting as of late, I want to try to make a more decent effort at it. Lots of people have created their own forks with tweaks and new features, some of which I'd like to incorporate in this "rewrite".

This issue will serve as a initial planning session of the overall goals of this update/rewrite. As such if you're reading this and have anything you'd like to add, please do leave a comment :)

Below is the initial rough draft list of goals in no specific order:

  • Make the prompt fully customizable. Using environment variables to define prefix/suffix, tag prefix/suffix, dirty characters, and more to allow you to fully customize exactly how the git information looks.
  • Officially support zsh. This includes the ability to install git-aware-prompt as a plugin for oh-my-zsh, antigen, zgen, zplug, etc. This will allow me to personally use the project again.
  • Be compatible with oh-my-zsh themes. Allows people who use oh-my-zsh's git plugin and a oh-my-zsh theme to just swap out the git plugin for git-aware-prompt.
  • Fancier dirty tracking. Ability to show the number of modified/untracked/staged files, etc.
  • Retain backwards compatibility as much as possible (for now).
  • Start using semantic versioning.
  • Unit tests?
  • Look at git's own __git_ps1 thing via git-prompt.sh. I've never looked at it, so no clue if it's of any use to us. Also if it is of use, do we want to depend on it, internalize it, or simply have our own implementation like right now to keep dependencies at zero.
  • Deal with timeouts. Sometimes the git dirty status check takes a long time first time around for very large repos. Having some kind of timeout might be a good idea. Overall requires more research.
  • Keep the code very clean and lean, typically not having any code running that the end user doesn't specifically use. This could be done with if statements (but it means if statements run), or potentially through code generation.
  • Modular design. Related to clean code above. Basically it means features are separate files in source, and the scripts that are used are built from all of the modules. This should give us a flexible base to work with, and it means those who wish can create custom builds with only the modules/features they want.
  • Indicate if current branch has a upstream branch or not.

tput error: no value for $TERM and no -T specified

I'm getting error when loging to Ubuntu 14.04 LTS. I suspect git-aware-prompt. could it be?
login error screenshot

Im not a progamator, but I was searching and I found this. It can not be a solution? Testing for an interactive shell?

# Test for an interactive shell.  There is no need to set anything
# past this point for scp and rcp, and it's important to refrain from
# outputting anything in those cases.
if [[ $- != *i* ]] ; then
    # Shell is non-interactive.  Be done now!
    return
fi

Not working on my Ubuntu 14.04 LTS

I put this settings on my .bashrc

export GITAWAREPROMPT=~/bin/git-aware-prompt
source "${GITAWAREPROMPT}/main.sh"
export PS1='$(basename "$PWD")\[\033[00m\]: \[$txtcyn\]\$git_branch\[$txtred\]\$git_dirty\[$txtrst\]\$:'

I guess that the problem was main.sh, colors.sh, prompt.sh don't have permission, then I executed:

chmod +x main.sh colors.sh prompt.sh

Even, after it, don't work. Result in my bash:

git-aware-prompt: $git_branch$git_dirty$:

Prompt slows down noticeably

First of all thank you for this sweet tool. However, I'm having a problem where when using Git aware prompt my command prompt slows down noticeably. As in, when I press enter with no command entered it doesn't instantly show a new $ prompt. Something to note is that my repository is huge, so that might affect things. I believe the issue is with git diff-files --quiet 2>&1. Here's some timing information:

wcauchois@fenris:~/repo (master)$ time git rev-parse --abbrev-ref HEAD 2> /dev/null

real    0m0.004s
user    0m0.002s
sys 0m0.002s
wcauchois@fenris:~/repo (master)$ time git diff-files --quiet 2>&1

real    0m0.051s
user    0m0.012s
sys 0m0.038s

As you can see, 0.05s is unacceptable. But this is probably because my repo is huge:

$ git bundle create /tmp/tmp.bundle --all && du -sh /tmp/tmp.bundle
Counting objects: 1123831, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (338881/338881), done.
Writing objects: 100% (1123831/1123831), 602.05 MiB | 52.58 MiB/s, done.
Total 1123831 (delta 659830), reused 1118676 (delta 655338)
602M    /tmp/tmp.bundle

Also, I'm on Mac OSX 10.8.2.

Anyway, if you can think of a workaround that would be awesome, since I'd love to have this prompt.

File status with branch name and status.

Git shell on windows shows file status as well with the branch info. We could easily add that by applying parsing to the git status call where we check for a dirty branch. For performance reasons we could keep it optional. If you dont get time to do that, I might in a little while.

Resizing Terminal Prompt Window Causing Spacing Issues

If I resize my terminal window so that the current path I'm on is split between two lines, then typing causes strange behavior.

Example:

rr@Macintosh ~/dropbox/Source_Code/dev_bootcamp/phase1/week1/sudoku-2-guessing-challenge (rickr)*$ l                 s

instead of:

rr@Macintosh ~/dropbox/Source_Code/dev_bootcamp/phase1/week1/sudoku-2-guessing-challenge (rickr)*$ ls

Question about PROMPT_COMMAND variable

Why in prompt.sh we need PROMPT_COMMAND="find_git_branch; find_git_dirty; $PROMPT_COMMAND" ?

More specifically why we add the existing PROMPT_COMMAND making it grow?

rewrites virtualenv env out to show just current branch

after activate a virtual environment in python it gives you an indication of which branch you currently are by pre-pending the current environment name to you prompt. Example if you environment's name is env
then you get a prompt like
(env) home/user/path_to_environment/src

However after running git-aware-prompt, script that is in my .profile file because am using ubuntu, the prompt changes to

home/user/path_to_environment/src (master)

What i expected was to get something like

(env) home/user/path_to_environment/src (master)

This happens when you execute execute

source $HOME/.profile

after runing
source env/bin/activate

Doesn't work with git submodules

My project folder, "Project1", has a submodule folder inside of it, named "Submodule1".

If I'm on the 'develop' branch of Project1, but the 'master' branch of Submodule1, the prompt only displays the branch of the Project1. Therefore, it always shows that I'm on the 'develop' branch.

Call from .bashrc instead of .bash_profile?

The instructions say to install in .bash_profile. If you do this, then find_git_branch and find_git_dirty functions aren't defined in any bash/xterm started from a login shell, so git_branch and git_dirty variables don't get updated.

I think the instructions should be changed to recommend installation in .bashrc instead

Really slow

Hey,

I just implemented this in a vagrant box and it works perfectly but it's really slow. I even removed the git_dirty from PS1 but that didn't improve things much.

This is what my prompt looks like:

export PS1="\${debian_chroot:+(\$debian_chroot)}\[$txtgrn\]@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\] \[$txtylw\]\$git_branch\[$txtrst\]\$ "

Reopen terminal, don't have anything change ?

Hi,

I have install success on my terminal mac os.

But at this terminal i cant view git branch is beautiful.

But I close this terminal and reopen, I cd to git folder. I don't see git branch current show ?.

Can't I help me?.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.