Last Updated: July 11, 2019
·
42.86K
· helmedeiros

Git Bash - Fixing it with alias and Functions

Perform our daily versioning tasks should not be something difficult. Checking status of your git repository, add files to its stage, confirm them and then push them should not be an arduous task.

Saying that, something that can help us a lot in this process is the definition of functions, alias, among other strategies like install cygwin.

Setting Up your aliases and functions:

In windows, when you open up your Git Bash by default you are at your home directory. Inside it create a .bash_profile file (on Win8 it should be named .bashrc.). If you aren't in your home directory change into it.

WHAT TO DO:

  1. Create the file .bash_profile;
  2. Open the .bash_profile in any editor;
  3. Add your alias into it;
  4. Add your functions into it;
  5. Save the .bash_profile;
  6. Apply changes;

HOW DO IT:

1. touch .bash_profile

2. notepad .bash_profile

3. Add your ALIASES:

# Aliases
alias g='git'
alias gst='git status'
alias gd='git diff'
alias gdc='git diff --cached'
alias gl='git pull'
alias gup='git pull --rebase'
alias gp='git push'
alias gd='git diff'
alias gc='git commit -v'
alias gc!='git commit -v --amend'
alias gca='git commit -v -a'
alias gca!='git commit -v -a --amend'
alias gcmsg='git commit -m'
alias gco='git checkout'
alias gcm='git checkout master'
alias gr='git remote'
alias grv='git remote -v'
alias grmv='git remote rename'
alias grrm='git remote remove'
alias grset='git remote set-url'
alias grup='git remote update'
alias grbi='git rebase -i'
alias grbc='git rebase --continue'
alias grba='git rebase --abort'
alias gb='git branch'
alias gba='git branch -a'
alias gcount='git shortlog -sn'
alias gcl='git config --list'
alias gcp='git cherry-pick'
alias glg='git log --stat --max-count=10'
alias glgg='git log --graph --max-count=10'
alias glgga='git log --graph --decorate --all'
alias glo='git log --oneline --decorate --color'
alias glog='git log --oneline --decorate --color --graph'
alias gss='git status -s'
alias ga='git add'
alias gm='git merge'
alias grh='git reset HEAD'
alias grhh='git reset HEAD --hard'
alias gclean='git reset --hard && git clean -dfx'
alias gwc='git whatchanged -p --abbrev-commit --pretty=medium'

#remove the gf alias
#alias gf='git ls-files | grep'

alias gpoat='git push origin --all && git push origin --tags'
alias gmt='git mergetool --no-prompt'

alias gg='git gui citool'
alias gga='git gui citool --amend'
alias gk='gitk --all --branches'

alias gsts='git stash show --text'
alias gsta='git stash'
alias gstp='git stash pop'
alias gstd='git stash drop'

# Will cd into the top of the current repository
# or submodule.
alias grt='cd $(git rev-parse --show-toplevel || echo ".")'

# Git and svn mix
alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'

alias gsr='git svn rebase'
alias gsd='git svn dcommit'

# these alias commit and uncomit wip branches
alias gwip='git add -A; git ls-files --deleted -z | xargs -r0 git rm; git commit -m "--wip--"'
alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'

# these alias ignore changes to file
alias gignore='git update-index --assume-unchanged'
alias gunignore='git update-index --no-assume-unchanged'
# list temporarily ignored files
alias gignored='git ls-files -v | grep "^[[:lower:]]"'

4. Add your FUNCTIONS:

# functions
# Will return the current branch name
# Usage example: git pull origin $(current_branch)
#
function current_branch() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo ${ref#refs/heads/}
}

function current_repository() {
  ref=$(git symbolic-ref HEAD 2> /dev/null) || \
  ref=$(git rev-parse --short HEAD 2> /dev/null) || return
  echo $(git remote -v | cut -d':' -f 2)
}

# these aliases take advantage of the previous function
alias ggpull='git pull origin $(current_branch)'
alias ggpur='git pull --rebase origin $(current_branch)'
alias ggpush='git push origin $(current_branch)'
alias ggpnp='git pull origin $(current_branch) && git push origin $(current_branch)'

# Pretty log messages
function _git_log_prettily(){
  if ! [ -z $1 ]; then
    git log --pretty=$1
  fi
}
alias glp="_git_log_prettily"

# Work In Progress (wip)
# These features allow to pause a branch development and switch to another one (wip)
# When you want to go back to work, just unwip it
#
# This function return a warning if the current branch is a wip
function work_in_progress() {
  if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then
    echo "WIP!!"
  fi
}

5. Save your file.

6. Source the file our open a new git bash.

source .bash_profile

5 Responses
Add your response

The following two alias' are staples for any .gitconfig I create. Maybe you'll enjoy them as much as I do. They're originally sourced and modified from some other gist somewhere... unfortunately I can't remember where. I wish I could credit the originator.

https://gist.github.com/coderjoe/86fd288d5e6ce3a8a3c1

over 1 year ago ·

Thanks @coderjoe they're great!

over 1 year ago ·

I use SCM Breeze and find it absolutely indispensable. It adds all of the above aliases as well as very useful colorizing of the git output, a beautiful pretty-printed git log, and best of all clever numbered shortcuts to files allowing you to replace git add long/path/to/file/somewhere.something with ga 1 (the numbers are displayed when you do eg. git status or ls )

I really, really love it. The only drawback is that I miss it a lot when it is not available (eg. when logged into a server I don't own).

over 1 year ago ·

Awesome! I always enjoy setting up new bash recipes.

over 1 year ago ·

First of all
step 0: cd ~
because bash_profile do not loads from / but from home folder

over 1 year ago ·