Handy Git tricks

This is a collection of tricks I use to make my life with Git easier while maintaining a clean history that helps to understand the code. Which is the whole point of version control, isn’t it?

Transplant a branch

To transplant a Git branch from one branch to another, use

git rebase --onto

Example

git rebase --onto beta develop fix

Update without checkout

To update a branch without checking it out first – e.g. to rebase onto the latest version of the parent branch, use

git fetch origin <branch>:<branch>

You really need to type the target branch twice!

Example

git fetch origin develop:develop

Rewrite history

Before I merge I rewrite my history so that each commit is clean and there are no „changes after review“ commits: they don’t help to understand why the code is the way it is.

git add .
git commit --fixup
git rebase -i --autosquash develop

Read more about this in the Git Book

Know the version

We use annotated tags to mark each release. We also never merge back but only merge older branches into newer branches. This allows us to easily determine the version number based on the tags.

git describe --first-parent [branch]

If you don’t give the name of a branch (or hash or similar), you get the version number of the current branch.

Example

git describe --first-parent beta