7 git aliases that make me more productive

We use git to manage our code at CBRE Build NYC, and over time I’ve gathered a little collection of git aliases I love - found by myself, from the internet, or passed on by my amazing coworkers (shoutout especially to Ben LeVeque). Maybe you’ll find some of them useful for yourself! Setting git aliases is a way of defining your own commands to shorten or combine git commands you use frequently. A common one is replacing checkout with co to save yourself a couple of letters when switching branches. Aliases allow you to

  • spend less time typing.
  • not have to google easily forgettable things (is it --oneline or --one-line?).
  • be saved from making typos in long commands and having to correct those.
  • have fun by really making the command line your own.

How to set up git aliases

My favorite way of editing my aliases is by editing the ~/.gitconfig file itself, for example by using vim (type vim ~/.gitconfig in your terminal), and adding new aliases below the [alias] tag. You can also set individual aliases from the terminal by using the command git config --global alias <name> <command>, but I just like seeing all my aliases in one place when I edit them.

My favorite 7 git aliases

  1. What branch am I on again? I use git bnch (a shortened version of git branch) to only print the name of the branch I have currently checked out:

    bnch = rev-parse --abbrev-ref HEAD
  2. When I want to see a short and sweet list of the hashes and commit messages of my 10 most recent commits, I use git last:

    last = log --oneline -n 10
  3. To double check which changes I have made in the last commit, I use difflastcom. git diff <commit1> <commit2> reveals the changes made between two commits, thus this works to only see the changes introduced in the most recent commit:

    difflastcom = diff HEAD~1 HEAD
  4. In a similar manner, I might be wondering what changes I made in any one commit, not just the last one. Given a commit with hash ca4b354, git thatdiff ca4b354 prints the changes introduced in that commit:

    thatdiff = "!f() { git diff $1^ $1; }; f"
  5. Sometimes, I look at my stash list (git stash list), and I wonder “hmmm, what is in that one stash?” With the right alias, a quick git stashread 1 reveals the changes stored in stash@{1}

    stashread = "!f() { git stash show -p stash@{$1};}; f"
  6. I rebase my local branches onto master a fair amount to pull in changes as I go . To rebase a branch I’m working on onto master with just one command, I use git rbm, short for ReBase Master (check out master, pull master, checkout current branch, rebase onto master):

    rbm = "!f() { \
      local b=${1:-$(git rev-parse --abbrev-ref HEAD)}; \
      echo 'Are you sure you want to run this? It will delete your current '$b'.'; \
      read -p 'Enter to continue, ctrl-C to quit: ' response; \
      git checkout master; \
      git pull origin master; \
      git checkout $b; \
      git rebase master; \
    }; f"
  7. You’ve been there: you checked out a coworker’s branch to test it, and while you were doing that, they added things, rebased on master, and force pushed onto the remote branch. As you’re trying to do a regular git pull for the branch to get the updates, git tells you that you have to deal with conflicts. Instead of doing that, I start from scratch and use git smash: it double checks you know what you’re doing, checks out master, deletes my local version of the branch I was testing, fetches the updated one, and re-checks it out.

    smash = "!f() { \
      local b=${1:-$(git rev-parse --abbrev-ref HEAD)}; \
      echo 'Are you sure you want to run this? It will delete your current '$b'.'; \
      read -p 'Enter to continue, ctrl-C to quit: ' response; \
      git checkout master; \
      git branch -D $b; \
      git fetch origin $b; \
      git checkout $b; \
    }; f"

With all those commands, this is what my .gitconfig file looks like: null

Drawbacks to using git aliases

Caveat: there are some solid drawbacks to using your own aliases:

  • You might be rendered a bit helpless when working on any machine but yours, because you can’t remember the full git command.
  • You forget that your commands don’t exist for others and cause some confusion when offering help. Those are valid points, and might be reason enough for you to want to stick with typing everything out. Or you might just love saving yourself an extra couple of letters here and there. To each their own, do what works for you!

Bonus section: bash aliases

You don’t have to end at custom git aliases - bash commands are just as personalizable! Just edit your ~/.bashrc file. I personally use

  • aliases to cd into my different project files

    alias work='cd ~/Documents/...path to your project folder.../workplace'
  • An alias to clear my terminal before I git diff. That way, the diff shown starts at the top of my terminal, and I end up seeing more of it immediately, which I like.

    alias diff='clear; git diff'

    I hope you found a thing or two about our git aliases useful, or got some new ideas for tinkering with yours!

Magdalena is a full stack engineer currently working on our Spacer product. She’s been at CBRE Build full time for a bit over two years, was an intern here before that, and loves that she gets to work with as great a group of humans as she finds in the CBRE Build office every day.