Developing software in the Real World

A few Git tips

I don’t do that much that’s clever with git, but I’ve found the following helpful.

Automatically prune

When you do a git fetch or git pull, you can ask it to remove remote tracking branches for a branch that has been removed on the remote by using the --prune flag.

This can be automated globally with:

and if you only want it for a specific repository, you can use:

One line log

I like a sensible one line log format, so I regularly use:

This can be aliased to oneline using:

Limit log to a certain number of commits

Just add dash, followed by the number of commits. e.g. to limit to the last half dozen commits:

I do this all the time!

No paging when diffing

I don’t need my diff paged as I can use my terminal to scroll up. Hence, I use cat as my pager which doesn’t do anything clever!.

See diff while writing the commit message

When writing the commit message, I sometimes find it helpful to have the diff in the editor window both as a reminder of what I’m committing and as a final check that it’s what I expect. This is done with the -v flag and the diff is not part of the commit message that’s saved.

Rebase with confidence

I regularly rebase my local branches before pushing to tidy them up or move them up to the latest commit on develop. I can’t recommend learning how git rebase works enough. This is not the article for it though. Try Chris Tankersley‘s Git rebase is your friend as a starting point.

Elsewhere on the Internet

I’m sure that there’s many many more tips and tricks available for git that make workflows easier. In my experience, try to add only a couple at a time until they stick. Many more than that and you’ll forget the to use them!

If you are into vidoes, then Lorna Mitchell‘s Advanced Git for Developers is a must-watch.

3 thoughts on “A few Git tips

  1. Thank you; a nice summary of some git gems.

    One typo to be fixed:

    old: "you can ask it to remote remote tracking branches…"
    new: "you can ask it to remove remote tracking branches … "

Thoughts? Leave a reply

Your email address will not be published. Required fields are marked *