Programming

Hands-on Git aliases

Git is deservedly the most popular version control system among software developers. You can easily find Git extensions and plugins for any IDE of your choice. However, if you like me, working mostly in the command line git aliases could save you a lot of time and your productivity will be even higher. Here I want to share with you some of my Git aliases which you could find useful for your daily work.

Add to staging area and commit in one go

// Aliases
ac = !git add -A && git commit
acm = !git add -A && git commit -m

// Usage 
git ac
git acm "My awesome commit"

I use it to add all modified files to staging area and commit it to local repository. As you can see there is two variations of this alias: with or without commit message.

Show me the deal

When you need to check the log with a nice colored layout:

lg = !git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit

You will see something like this:

Slightly different version:

graph = !git log --oneline --all --graph --decorate

Shorthand notations

Few simple shorthand notations for familiar commands:

// Aliases
co = !git checkout
st = !git status
n = !git checkout -b

// Usage
git co master
git st
git n feature/my_awesome_new_feature

If you want to add changes from your working directory to the last commit in one go:

amend = !git add -A && git commit --amend --no-edit 

If you want to undo changes you haven’t committed yet in staging area:

discard = !git checkout --

In case you played a while with changes and commits but really want to start all over again:

fuckit = !git reset --hard && git clean -df

To do some cleanup:

cleanup = !git remote prune origin && git gc && git clean -dfx && git stash clear

Fetch/Push/Pull

To quickly get the latest updates from remote:

up = !git fetch origin && git rebase origin/$(git rev-parse --abbrev-ref HEAD)

To push all your commits from local repository to remote (remote branch will be created automatically):

p = !git push origin $(git rev-parse --abbrev-ref HEAD)

Other useful

Sometimes you want to see stats on who is working on that repository and what contribution of that user:

who = shortlog -n -s --no-merges

Often there is a need to check which files changed in the specific commit:

list-changes = !git diff-tree --no-commit-id --name-only -r
git list-changes <SHA>

You just need to specify commit SHA.

Sometimes I’m curious in lines of code stats for my project. So I can do it in this way:

loc = !git ls-files '*.cs' | xargs wc -l
Just change the file extension in that alias to the language of your choice.

Usage patterns

Here is some most common usage patterns with these aliases.

Checkout to master and get latest changes from remote

git co master
git up

Create new feature branch, save changes, commit and push:

git n feature/new_large_hadron_collider 
// Create new collider here...
git acm "New collider is finished"
// More changes 
git amend
// It's time to push now
git p

Fork some repository as playground

git clone https://github.com/dotnet/csharplang.git
// Add proper maybe monad support
git acm "Almost done"
// Add more nice features
git acm "More awesomeness"
// Realize that it doesn't work
git fuckit
git cleanup

The complete list of aliases

	ac = !git add -A && git commit
	acm = !git add -A && git commit -m
	co = !git checkout
	st = !git status
	amend = !git add -A && git commit --amend --no-edit
	discard = !git checkout --
	up = !git fetch origin && git rebase origin/$(git rev-parse --abbrev-ref HEAD)
	p = !git push origin $(git rev-parse --abbrev-ref HEAD)
	n = !git checkout -b
	lg = !git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
	graph = !git log --oneline --all --graph --decorate
	fuckit = !git reset --hard && git clean -df
	who = shortlog -n -s --no-merges
	cleanup = !git remote prune origin && git gc && git clean -dfx && git stash clear
	loc = !git ls-files '*.cs' | xargs wc -l
	mr = !sh -c 'git fetch $1 merge-requests/$2/head:mr-$1-$2 && git checkout mr-$1-$2' -
	list-changes = !git diff-tree --no-commit-id --name-only -r