Git Tricks

Git is a great  SCM / VCS, but sometimes it can be scary: unless you start keep trace of those useful commands which can save your day!


If your git knowledge is a little on the beginner side you might start with the following tutorials:


Ever needed to get back to a clean repo to apply a quick fix while you are in the middle of something and plenty of changes waiting to be committed?

# stash any changes to tracked files
$ git stash

# stash only unstaged changes to tracked files
$ git stash --keep-index

# stash untracked and tracked files
$ git stash --include-untracked

# stash ignored, untracked, and tracked files
$ git stash --all


The default fast-forward merge strategy is not always the most advice-able with regards to your repo history tree:

git merge ff.gif

Often a non fast-forward merge strategy generates an easier to follow history tree:

git merge no-ff

# merge with no fast-forward
$ git merge --no-ff

Condensed status

Git status is quite verbose, but it’s easy to make it condensed, if you can handle the compressed display format:

$ git status --short --branch

Proper init

Creating the central repo is an administrative task you don’t want to leave to the occasional developer: git first commit cannot be rebased.

$ git init
$ git commit -m “root” --allow-empty

Fix last commit

How many times you discovered you missed something in your last commit? This will fix it without adding a new commit to your history and messing it up.

$ git commit -m 'fixed stylesheet'
# (facepalm)
$ git add css/main.css
$ git commit --amend --no-edit

Force, delicately

It’s sometimes inevitable to force a push, but you can try to do it in the most delicate manner possible:

# force your push, but first ensure you are up to date with the remote
$ git push --force-with-lease


Make all the above commands shorter using aliases:

# git `please`
$ git config --global alias.please 'push --force-with-lease'

# git `commend` aka commit and amend
$ git config --global alias.commend 'commit --amend --no-edit'

# git `staaash`
$ git config --global alias.stsh 'stash --keep-index'
$ git config --global alias.staash 'stash --include-untracked'
$ git config --global alias.staaash 'stash --all'

# git `st` aka short status
$ git config --global 'status --short --branch'

# git `grog` aka graphical log
$ git config --global alias.grog 'log --graph --abbrev-commit --decorate --all --format=format:"%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(dim white) - %an%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n %C(white)%s%C(reset)"'

One thought on “Git Tricks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s