Wednesday 16 March 2022

More fun with Git and ZSH, this time branch naming ...

Following on from my earlier post: -

ZSH and Git and auto-complete

I wanted to add the capability to display the name of the current GitHub repository branch in my Terminal.

Thankfully, GitHub already allows this, and I had their repository cloned to my Mac.

A quick update: -

cd ~/Documents/GitHub/github.com/git

git fetch origin

git rebase origin/master

and then I copied a pair of scripts: -

cp ~/Documents/GitHub/github.com/git/contrib/completion/git-completion.zsh ~/.zsh/_git

cp ~/Documents/GitHub/github.com/git/contrib/completion/git-prompt.sh ~

noting that git-completion.zsh goes into ~/.zsh/_git as per this: -

# The recommended way to install this script is to make a copy of it as a
# file named '_git' inside any directory in your fpath.
#
# For example, create a directory '~/.zsh/', copy this file to '~/.zsh/_git',
# and then add the following to your ~/.zshrc file:
#
#  fpath=(~/.zsh $fpath)

( source: git-completion.zsh )

This is now my ~/.zshrc file: -

if type brew &>/dev/null; then
    FPATH=$(brew --prefix)/share/zsh-completions:$FPATH

    autoload -Uz compinit
    compinit -u
fi

setopt prompt_subst
. ~/git-prompt.sh
export RPROMPT=$'$(__git_ps1 "%s")'

and, having restarted my Terminal, this is what I see: -


noting that the branch name appears/changes on the right-hand side of the screen

Which is nice

No comments:

Visual Studio Code - Wow 🙀

Why did I not know that I can merely hit [cmd] [p]  to bring up a search box allowing me to search my project e.g. a repo cloned from GitHub...