Git Advanced Tips

04 Oct 2017:

In this article I collect some commandlines that I find helpful.

This is by no means complete and probably never will be. I will edit and add to this article as I stumble upon things.

Show only your own branches

When you work with projects that has multiple remotes that have only one user e.g. one you push to and then some others:

gitk --all --not --remotes=origin --remotes=...

What this does: We display all commits except the ones that can be found on the remotes: origin, …

git update

This alias is quite handy if you want to update and make sure you do not accidentially merge anything. Put this in your ~/.gitconfig:

    update = pull --ff --ff-only --prune

What this does: We request to do a fast-forward merge which means to simply forward the branch pointer on existing commits. We forbid to do anything else for the update. The last option tells git to cleanup the local cache of remote refs that have been removed on a remote.

git branch-date

This was the answer to a user requesting an option to sort branches by date. Which is useful if you want to find a branchname that you worked on most recently.

An alias to show all local branches ordered by date. Add this to your ~/.gitconfig:

    branch-date = for-each-ref \
        --format='%(committerdate:short) %(refname:short): %(subject)' \
        --sort='-committerdate' refs/heads/

What this does: for-each-ref is a tool to iterate over refs (branches). You can pass it which refs it should iterate (refs/heads/ contains all local branches) a field to sort them by and a format for the output. Presto, you got yourself a branch name output ordered by date. Easy isn’t it :)


Nowadays this can actually be simplified to:

    branch-date = branch --sort="-committerdate"

So you can see, git is not as hard as it used to be…