fbpx
Clear Measure

WEBINAR: You Have What It Takes to Oversee a Software Project. REGISTER TODAY!

Get Aliases

Git Aliases: Be more productive with the Git Command Line

Share This Article:

Here at Clear Measure we like to think we know what’s what and how’s how. When it comes to being powerful with Git, there’s still (often) no beating the command line by leveraging Git aliases. Here are some we won’t live without.

Disclaimer

We can’t and won’t take credit for all these great Git gems. We’re all indebted to Phil Haack.

Include my Git Aliases | You’ve Been Haacked

Git Aliases

Mike Sigsworth
I have two aliases from Phil Haack that I use pretty regularly.

up = !git pull --rebase --prune $@ && git submodule update --init --recursive
bclean = "!f() { git branch --merged ${1-main} | grep -v "${1-main}$" | xargs -r git branch -d; }; f"
bdone = "!f() { git checkout ${1-main} && git up && git bclean ${1-main}; }; f"

git bclean deletes all merged branches.

git bdone checks out the main branch, does a few things to update the local working copy, then does a git bclean.

Whenever I am done with a PR and it’s been merged I always do a git bdone. Keeps things tidy.

Corey Keller

git checkout -
git switch -

Returns to the last branch you were working on. That is the one I use all the time that I was not aware of for a while.

Quinn Wilson
I made this alias.

pushu = !git push -u origin HEAD

It’s for when you are pushing to a new branch and you don’t have an upstream repo set.

Charles Flatt
Here’s one I use almost every day. It shows a list of different files between two commits. Great for comparing branches.

diffshow =  "!f() { git diff $1 ${2-HEAD} --name-status ; }; f"

# example 1
git switch clf/mybranch
git diffshow main

# example 2
git diffshow head~1 head~2

Leveraging some of Phil Haack’s work, I use git rbm to rebase a branch onto mainline without leaving the branch. I use the -q option a lot to reduce messages. Using a Powerline prompt tells me if I need to git push --force.

# Get the mainline branch name
default = !git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@'
# Fetch remote mainline without leaving branch
fm = "!f() { DEFAULT=$(git default); git fetch -q origin $DEFAULT:$DEFAULT; }; f"
# Rebase onto mainline
rbm  = "!f() { DEFAULT=$(git default); git fm; git rebase -q $DEFAULT; }; f"

This show me a pretty graph history of ten commits by default. Appending a number changes the amount of history.

hist = "!f() { git log -${1-10} --graph --date=short --pretty=format:\"%C(auto) %h %ad | %d %s [%an]\"; }; f"

# usage
git hist
git hist 20

You’ll hopefully never need this, but maybe it’ll inspire something similar. These steps create a Markdown report of a repository’s branches sorted by last committed date.

Check out all branches

git branch --list --all | clip
# paste into editor
# delete lines without "remote/origin/"
# replace "remote/origin/" with "git checkout "
# copy/paste commands to run in terminal
git checkout main

Then

echo "| LastCommittedOn | LastCommitedBy | Branch" > branch-history.md
echo "|-----------------|----------------|-----------------------" >> branch-history.md
git for-each-ref --sort=committerdate refs/heads/ --format='| %(committerdate:short) | %(authorname) | %(refname:short)' >> branch-history.md

Finally

# replace "git checkout" with "git branch -D"
# copy/paste commands to clean up

Favorite GUI Tools?

Charles Flatt

  • KDiff3 for merging. It makes merge conflicts very clear. I have it set as my Git mergetool. Source on GitHub

Scott Wilson

  • Tortoise Git, which has a great way to compare branches, and it has ~80% feature parity with the CLI client.
  • Beyond Compare, my preferred Git Merge/Diff tool.

Mike Sigsworth

About Us

We’re Clear Measure, a consultancy helping you learn to deliver high quality code faster. Call us if you feel like your company’s coding in a Model T and instead want ride a rocket.

Related Articles

Need Help with an Upcoming Project

Terms of Use | Privacy Policy | Cookies Policy

Copyright © Clear Measure. All rights reserved.