My Favorite Git Helpers
Tags: git, cli
Published: 2017-06-08 15:47:31

---
title: My Favorite Git Helpers
published: true
description: Git aliases and functions for use from the command line.
tags: git, cli
---

You can find all of these aliases and functions (and then some!) on my Github here:Â [https://github.com/Andrew565/dotfiles](https://github.com/Andrew565/dotfiles). Read on for detailed explanations of what they are and what they do, and be sure to also check out my [preferred git workflow](https://andrewscripts.wordpress.com/2017/06/08/my-one-way-git-workflow-or-how-to-git-well/).

I use bash in my terminal. I know ZSH and fish and all the rest have their niceties, but I started with bash, and I like it fine enough, so that's what I use. 

My favorite thing to do in bash? Create aliases and helper functions for everything, especially Git commands. So, here's a list of some of my favs.

# Bash Aliases

```shell
# Git Aliases
alias g="git"
alias gs="git status"
alias gl="git log"
alias grh="git reset --hard"
alias gcmm="git checkout --"
alias gsm="git stash && git co master && git stash pop"
alias gsd="git stash && git co development"
alias gmd="git merge development"
alias gmm="git merge master"
alias gupdate="git co development && git pull"
alias glatest="git co development && git pull"
alias gmupdate="git co master && git pull"
alias gcm="gmupdate"
alias gphm="git push heroku master"
```

Most of these are probably self-explanatory. Here are a couple commands that may not be immediately obvious:

`alias gsm="git stash && git co master && git stash pop"`

This is invaluable when I do work inÂ a feature branch that I actually wanted in the master branch. This doesn't happen as much anymore since most of the time I never commit directly to master, but it happens.

`alias gcmm="git checkout --"`

This command is when you have a fileÂ staged for commit and you don't actually want it to be part of the current commit. It could also be aliased as 'unstage'.

# Bash Functions

Aliases will only get you so far. Sometimes you want to be able to bundle function calls together, or use variables. That's where bash functions come in.

## gnew - Create aÂ new branch

```shell
# create new branch from a default named branch
# modify the first line to make it an argument if desired
gnew() {
 DEST_BRANCH=$2
 : ${DEST_BRANCH:='master'}
 git co $DEST_BRANCH
 git pull
 git co -b "$1"
 # bundle install # If using Bundler (ruby)
 # bundle exec rake db:migrate
 git push -u
}
```

As it says, this creates a new branch based off of another branch. If you don't specify which branch to branch off of, it assumes/defaults to the 'master' branch. Use like this:

`gnew new-branch-name old-branch-name`

It then checks out the old branch/master, pulls to make sure you're branching off of the latest revision, then creates a new branch using the name you provided as the first argument. Commented out are commands to run bundler and rake db:migrate, only useful if you're working on a Ruby/Rails project. Finally, it does `git push -u`, which pushes up the new branch and sets up the local branch to track the remote version, which makes future push and pull commands more seamless.

## grmÂ - Git Rebase Master

```shell
# Get the latest changes on master pulled down locally
# and then rebase them into/onto the current branch
grm() {
  CURRENT=`git rev-parse --abbrev-ref HEAD` # figures out the current branch
  git checkout master
  git pull
  git checkout $CURRENT
  git rebase master
}
```

In [my preferred workflow](https://andrewscripts.wordpress.com/2017/06/08/my-one-way-git-workflow-or-how-to-git-well) I always rebase the latest commits from master into my feature branch before I commit it. This keeps merge conflicts to a minimum and helps to keep the commit history clean.

## gsrm - Git Stash && Rebase Master

```shell
# Stash current, then update to latest, then pop the stash
gsrm() {
  git stash
  grm
  git stash pop
}
```

Similar to grmÂ (and it even makes use of grm), this stashes whatever you're currently working that hasn't been staged/committed yet, then does the rebase, and then pops it out again. SUPER USEFUL if you know of upstream changes that you want to bring into your feature branch but what you're working on isn't ready to be committed. Side Note: don't use this as an excuse NOT to commit often. You should definitely still do that.

## gswitch - Quickly checkout branches

```shell
gswitch() {
  BRANCH=`git branch | grep "$1"`
  git checkout $BRANCH
}
```

Switch branches by issue number or feature keyword. For example:

`gswitch 1808 == git checkout features/1808-click-on-avatar`

NOTE: only works well if you use a unique description, thus the recommendation to use issue numbers. Also works best if you delete your already-merged local branches regularly.

# .gitconfig

If you're not taking advantage of a global [.gitconfig](https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration)Â file, you're doing Git wrong. Here too I have a bunch of aliases, and a couple functions.

## aliases - Lookup what aliases you have

`aliases = !git config --get-regexp 'alias.*' | colrm 1 6 | sed 's/[ ]/ = /'`

I _so_ need to recreate this in bash, I have a hard time remembering all of the aliases I've created (there *are* a lot of them). ðŸ˜

## cam - Commit while Adding files and attach a Message

`cam = commit -am`

This is (essentially) the same as:

`git add . && git commit -m "your message"`

Be careful that you don't use this if you have any unstaged files you don't want to be committed, as it will add everything to the commit.

## last - What is the last thing I committed?

`last = cat-file commit HEAD`

## co - Checkout

`co = checkout`

## delete-local - Clear out old branches quicker

`delete-local = branch -d`

Somehow it's easier for me to remember `git delete-local`Â than `git branch -d`.

## delete-remote - Clear out remote branches quicker

`delete-remote = "!sh -c 'git push origin :$1' -"`

CAUTION: This doesn't ask if you know what you're doing before you do it, so be careful!

## branches - Get a list of all of your local branches

`branches = "!sh -c 'git branch -a | grep -v remotes'"`

You can get rid of the `grep -v remotes`Â if you want a list of remote branches too. This is another alias for 'natural language' purposes, just feels better to type `git branches`.

## edit-unmerged & add-unmerged - Fix merge conflicts like a boss

```shell
# use like `git edit-unmerged; ...edit..., ...test...
# git add-unmerged; get commit || rebase --continue
edit-unmerged = "!f() { git diff --name-status --diff-filter=U | cut -f2 ; }; code `f`"
add-unmerged = "!f() { git diff --name-status --diff-filter=U | cut -f2 ; }; git add `f`"
```

This saves me so much time it's ridiculous. Basically, `edit-unmerged` looks at all of your files that are marked as "unmerged", meaning they have conflicts that need to be resolved. Then it opens them up in your code editor (replace `code`Â above with the cli alias for your preferred editor, `vi`, `emacs`, `subl`, etc.). When you're done, `add-unmerged`Â finds that list of unmerged files again and does `git add`Â on them (and only them, so you don't have to worry what else might get added). At this point, everything should be staged, so you can run `git commit`Â or `git rebase --continue`.

# Final Notes

Remember that a good craftsman never blames his tools. These helpers are not intended to be substitutes for actually knowing what you're doing. ðŸ˜‰ Here again is a link to my Github repo with these files:Â [https://github.com/Andrew565/dotfiles](https://github.com/Andrew565/dotfiles).