DEV Community

Discussion on: Git [Script] Aliases That Could Be Helpful

Collapse
 
pmcgowan profile image
p-mcgowan • Edited

Thought I'd chime in - never looked into git aliases much, but I made a bash script containing all my helper commands a while back when I was learning git, and since moved them into bash aliases for ones I use frequently. At this point, they are just convenience since I know the base commands, so I would recommend learning the underlying commands first, but these are just lazy helper aliases.

alias gst='git status'
# Git log
glog() {
  case $1 in
    -s | --short | s)
      git log --pretty=format:'%h %ad | %s%d [%an]' $colour --graph --date=short;;
    -d | --diff | d)
      git log -p;;
    -f | --from | f)
      shaOrDate="$2"
      if [[ "$shaOrDate" =~ ^[[:alnum:]]{40}$ ]] || [[ $shaOrDate =~ ^[[:alnum:]]{6}$ ]]; then
        cmd="$shaOrDate^..$(git rev-parse HEAD)"
        # Could add --to=$3
        # cmd="$shaOrDate^..${3-$(git rev-parse HEAD)}"
      elif [[ "$shaOrDate" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
        cmd="--since $shaOrDate"
      else
        echo "Unsupported bound: '$2'"
        echo "Expecting SHA hash (6 or 40 chars) or date YYYY-MM-DD"
        return 1
      fi
      git log $cmd --date=short --pretty=format:'%h %ad %an | %s%d'
    ;;
    -h | --help | h)
      echo "glog [-s | --short | -d | --diff | -h | --help] [-f | --from [SHA or date]]";;

    *)
      git log -p --stat --graph;;
  esac
}

alias gmerge='git merge --no-ff'

gpull() {
  branch=${1-$(git branch |grep '\*' |sed 's/\*\ //g')}
  remote=$(git remote |tail -n1)
  echo Pulling from $remote $branch
  git pull $remote $branch
}

gpush() {
  branch=${1-$(git branch |grep '\*' |sed 's/\*\ //g')}
  remote=$(git remote |tail -n1)
  echo Pushing to $remote $branch
  git push $remote $branch
}

gcam() {
  git add -A
  git commit -am "$*"
}

gcpush() {
  git add -A
  git commit -am "$*"
  gpush
}

# Edit merge conflicts - tries to use sub (may me subl on yours) or vim
geditmerge() {
  if [ -n "$(which sub)" ]; then
    editor=sub
  else
    editor=vim
  fi
  toedit=$(git diff --name-only --diff-filter=U)
  $editor $toedit
}
Collapse
 
sarathsantoshdamaraju profile image
Krishna Damaraju • Edited

Exactly, even for me they are just convenience, I'd never recommend them for beginners. Why did I forget to mention it in the post? Will do it now.

Collapse
 
pmcgowan profile image
p-mcgowan

Bash was built for convenience... Almost every script / alias / program I write is simply to make my life easier.

I'm lazy, so I have aliases like .. for cd .. and ... for cd ../.. etc, bashrc for source ~/.bashrc, ebashrc for vim ~/.bashrc... If I type it regularly enough, sooner or later it will be "functioned/aliased". Any time I have to use Windows or Mac I cry a little.