Git aliases in bashrc

Git alias that help you save time interacting with git.

# GIT
alias gs="git status"
alias ga="git add"
alias gaa="git add ."
alias diff="git diff"
alias rebase="rebasefrom master"
# Pull the remote revision by rebasing local changes on top of the 
# remote ones, if any.
alias gdown="git pull -r"
alias gup="git push"
alias grb='git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d'

checkout() {
   git checkout "$1"
}

mergewith() {
   gdown && git merge "$1" "$2"
}

# Merge the param branch with A SINGLE COMMIT to the current branch.
mergesingle() {
  mergewith "--ff-only" $1
}

# Merge the param branch with MORE THAN ONE commit to the current branch.
mergebranch() {
  mergewith "--no-ff" $1
}

# Rebase the current branch based on the remote version of the param branch.
rebasefrom() {
  git fetch --all && git pull -r && git rebase origin/"$1"
}

# Hard set the current branch to the remote version of the param branch.
resetto() {
  git fetch origin && git reset --hard origin/"$1"
}

Git Config file

You can also define the aliases on your git config file, usually at location “~/.gitconfig”

[alias]
  br = branch
  co = checkout
  cp = cherry-pick
  st = status
  l = log --color --graph --format='%C(magenta)%h%Creset %s %C(cyan)<%an>%Creset' -n 20 --abbrev-commit
  # View abbreviated SHA, description, history graph, time and author
  lg = log --color --graph --format='%C(magenta)%h%Creset -%C(green)%d%Creset %s %C(yellow)(%cr) %C(cyan)<%an>%Creset' --abbrev-commit --
  branches = branch -a
  remotes = remote -v
[core]
  editor = code --wait
[log]
  date = relative
[pull]
        rebase = true

Useful Git Commands

git init
git status
git log
git add <file/folder/patterns>
git commit -m "<message commit>"
git remote -v
git remote add <remote name> <remote link>
git push <remote name> <branch name>
git diff // See diff between staging area and working dir
git diff --cached // See diff between staging area and last commit


Restoring/reverting

Revert all changes in folder and subfolder to last commit
git checkout .
Revert specific file to last commit
git checkout [some_dir|file.txt]
Unstage files staged for commit using git add
git reset

Rollback commits

F is the state of your files.

Nuke last commit
   (F)
A-B-C
    ↑
  master
git reset --hard HEAD~1
 (F)
A-B
  ↑
master
Undo the commit but keep your changes
   (F)
A-B-C
    ↑
  master
git reset HEAD~1
   (F)
A-B-C
  ↑
master