• Level Up Your Git Skills With Aliases!

    Like many programmers I use git constantly. And also like many programmers I get tired of typing the same commands over and over and over. So I’ve added the following aliases to my .zshrc to make common commands easier and quicker to type. You could do something similar via .gitconfig or .bashrc.

    Some of my most used aliases are gpb for pushing a branch, gca for committing all changes, gs for getting the status, and gup for rebasing from master.

    Let me know if you have any suggestions!

    # Git
    alias g='git'
    # Shorten common commands
    alias gst='g status'
    alias gpl='g pull'
    alias gpu='g push'
    alias gf='g fetch'
    alias gco='g checkout'
    alias grb='g rebase'
    alias grbc='g rebase --continue'
    alias grba='g rebase --abort'
    alias ga='g add'
    alias gm='g merge'
    # More succinct status
    alias gs='g status -sb'
    # Show all branches in the order of last change
    alias gb='g for-each-ref --sort=committerdate refs/heads/ --format="%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))"'
    # Nicer diff
    alias gd='g diff --word-diff'
    # Delete a branch
    alias gdb='g branch -D'
    # Fancy git log
    alias gl='g log --graph --stat --abbrev-commit --date=relative'
    # Push branch and set the upstream
    alias gpb='g push --set-upstream origin "$(g rev-parse --abbrev-ref HEAD)"'
    # Safer force push
    alias gpbf='g push --force-with-lease'
    # Pull the latest from master and rebase
    alias gup='g pull --rebase origin master'
    # Pull the latest from master and merge
    alias gum='g pull origin master'
    # Ammend a commit
    alias gam='g commit --amend'
    # Save changes temporarily
    alias gwip='g add -A && g commit --no-verify -m "WIP"'
    # Undo the last commit
    alias gun='g reset HEAD~1 --mixed'
    # Super undo using the reflog...careful. Undo your undo.
    alias gungun='g reset "HEAD@{1}"'
    # Delete all merged branches
    alias gcl='g branch | grep -v "master" | xargs g branch -d'
    # Commit
    alias gc='g commit -v -m'
    # Add all and commit
    alias gca='g add -u && g add . && g commit -v -m'
    # Create new branch
    alias gcob='g checkout -b'
    # Add all
    alias gaa='g add -u && g add . && g status'
    # Interactive adding
    alias gap='g add -p && g status'
    # Interactive rebase for squashing commits
    alias gir="g rebase -i origin/master"
  • git merge --squash

    I’m a fan of rebasing in git. I like being able to make lots of small, messy commits without worrying about a readable history and cleaning it up later. But on occasion I’ve had issues where rebasing and squashing my commits has resulted in many merge conflicts. I’m not really sure why this sometimes happens but it is a pain. Fixing merge conflicts can be error prone and time consuming.

    Thankfully I’ve recently come across git merge --squash which can help in these situations. To use it do the following:

    # Checkout master or whatever you branched off
    git checkout master
    # Create a new, clean branch
    git checkout -b fancy-new-clean-branch
    # Now merge the old, messy branch into the new
    # branch with git merge --squash
    git merge --squash old-messy-branch
    # This will copy all the changes to the current
    # branch but will NOT create a new commit. Add all
    # the changes and then commit.
    git add .
    git commit -m 'New easy to understand commit'

    Now your git history on the new branch will have a new single commit that you can push up and create a pull request for.

  • Create a Vim app for Mac OS X

    On occasion it is nice to have a way to open a file in Vim without having to jump over to the terminal. I researched a few GUI options like VimR or MacVim and others but all had problems. MacVim didn’t support FZF and wasn’t built on Neovim, VimR and others didn’t quite feel polished/stable enough.

    After a bit more research and trial and error I found that I could create an Automator app with an AppleScript which would accomplish what I wanted.

    Here is the script if you want to try it out.

    on run {input, parameters}
        set filePath to POSIX path of input
      on error errMsg
        set filePath to ""
      end try
      tell application "iTerm"
        create window with default profile
        tell front window
          tell current session
            if filePath is "" then
              write text ("vim; exit")
              write text ("vim " & quote & filePath & quote & "; exit")
            end if
          end tell
        end tell
      end tell
    end run

    To use this script, open Automator and create a new Application, then choose “Run AppleScript” and drag it into the workflow. Paste the above script in then save the application and you are ready to go. This script opens iTerm but it could be modified to use Terminal too.