DEV Community

Trent Yang
Trent Yang

Posted on

Git ABC - Start remembering and stop googling

I was once a Git googler (I mean I google Git a lot :P) until one day I sit down and decide to spend a bit of time remembering the most common commands. My approach was to develop an easy-to-understand framework and use systematic aliases to replace Git commands. If that sounds interesting, I present you the Git ABC.
git abc

Git ABC

There are three stages a file change (for example adding a new line) need to go through in Git. I use A, B and C to represent them.

Stage A

Unstaged changes and untracked files are in stage A. git status will show them in red.
Stage A

Stage B

Once we git add -A, changes will be moved to stage B. git status will show them in green.
In Git lingo, these changes are now 'staged'.
Stage B

Stage C

After we git commit -m, changes will be moved to stage C (committed stage).

Alias

Now comes the fun part.

Git ABC

Move forward

Git users should already be familiar with git add and git commit so I choose the following aliases to move changes forward

  • alias ga='git add -A' (Git Add) move changes from stage A to B
  • alias gc='git commit -m' (Git Commit) move changes from stage B to C

Move backward

I use go to undo changes. We can think of it as 'go away' :)

  • alias go='git checkout' undo changes from A
  • alias gou='git clean -id' (GO Untracked) remove un-tracked files from A
  • alias gob='git reset HEAD' (GO from B) move changes from B to A
  • alias goc='git reset --soft HEAD~1' (GO from C) move changes from C to B
  • alias gocc='git reset --hard HEAD~1' (GO from C Confirm) remove the whole commit from C

Check differences between stages

I use gd (Git Diff) to check the difference.

  • alias gd='git diff' Difference between A and B
  • alias gdbc='git diff --cached' Difference between B and C
  • alias gdac='git diff HEAD' Difference between A and C
  • alias gdc='git diff HEAD^ HEAD' All changes from the last Commit

Here I listed all the aliases bellow so that you can copy and paste into your shell config files.

# move forward
alias ga='git add -A'
alias gc='git commit -m'

# move backward / undo
alias go='git checkout'
alias gou='git clean -id' # u means Untracked files
alias gob='git reset HEAD'
alias goc='git reset --soft HEAD~1'
alias gocc='git reset --hard HEAD~1' # c means Confirm

# check difference
alias gd='git diff'
alias gdbc='git diff --cached'
alias gdac='git diff HEAD'
alias gdc='git diff HEAD^ HEAD'

Bonus aliases

Some of my fun aliases do not fit into the framework above so I lump them here :)

# status
alias gs='git status'

# log
alias gl='git log'
alias glo='git log --oneline --decorate'

# stash and apply
alias gst='git stash'
alias gap='git stash apply --index'

# rebase
alias gr='git rebase'
alias grm='git fetch origin master:master && git rebase master'
alias grs='git rebase --skip'
alias grc='git rebase --continue'
alias gri='git rebase -i master'

Go above and beyond

At last, I encourage you to go crazy and create your own aliases for profit and fun :)

go crazy

Top comments (1)

Collapse
 
rjmoise profile image
Ryan

Helpful post.
I for one have been trying to learn Git recently (which is no easy feat) and I never knew you could assign aliases like that! Definitely something I'll make a note of and add to my arsenal.