Report inadequate content

Terminal tuning for Git developers in Mac

{
}

 TAGS:undefinedIf you work with Git in the terminal there are some tweaks you might want to apply to your prompt for safer and faster coding. The following lines are part of my ~/.bash_profile file. I use it on Mac although that might work in Linux as well.

You can copy and paste this code in your ~/.bash_profile (create it if it doesn't exist), save and open a new terminal to see the changes every time you save. What you get of this script is:

  • Prompt with colors
  • Show the current git branch in parenthesis
  • Autocomplete previous ssh hosts when you press TAB
  • Autocomplete branches with TAB. Useful when your branches have names like feature/PRJ-540--simplification-of-the-user-interface
# I like tunning the colors of the prompt in the first place:
export CLICOLOR='true'
export  LSCOLORS="gxfxcxdxbxCgCdabagacad"
export EDITOR=vi

# Git branch in good-looking prompt.
parse_git_branch() {
    git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}

# Prompt with Git branch
# Explanation of the weird lines: \u Username, \h Host, \w Path, tput setaf is the color definition

export PS1='\[$(tput setaf 7)\]\u@\[$(tput setaf 2)\]\h:\[$(tput setaf 4)\]\w\[$(tput setaf 1)\]$(parse_git_branch)\[$(tput sgr0)\] $ '

# Bonus track: SSH autocompleting hostnames, write ssh and press tab
complete -W "$(while read line; do echo ${line%%[, ]*}; done < ~/.ssh/known_hosts)" ssh

# Git autocompletion
# Requires that you execute the following commented line:
# curl https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash -o ~/.git-completion.bash
test -f ~/.git-completion.bash && . $_

Another useful thing is having an improved git log like the one in the following image: TAGS:undefinedYou can invoke this git log with the command git lg if you write once the following:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)%Creset' --abbrev-commit"
{
}

Image gallery for Terminal tuning for Git developers in Mac

2 about 2

Show pictures of Terminal tuning for Git developers in Mac

{
}

Comments Terminal tuning for Git developers in Mac

your export PS1= ...
as an error that causes the line to wrap around it self if the line is too long
its missing one \[
i added it below:
export PS1='\[$(tput setaf 7)\]\u@\[$(tput setaf 2)\]\h:\[$(tput setaf 4)\]\w\[$(tput setaf 1)\]$(parse_git_branch)\[$(tput sgr0)\] $ '
Tiago Tiago 04/06/2015 at 11:58
Thank you Tiago! I've updated the post. Well noticed!
Hello. Thanks for this article.
Why is it not working for me? I pasted your git log code in my bash_profile. Saved. and open new terminal window. Colors are not changed...
When I go back to my bash profile the code are still there so it was really saved..
GirlKnowsTech GirlKnowsTech 10/04/2016 at 03:03

Leave your comment Terminal tuning for Git developers in Mac

Log in to Obolog, or create your free blog if you are not registered yet.

User avatar Your name