Git Tricks

Multiline Commit Messages

The commit command accepts multiple -m flags:

$ git commit -m "Commit title" -m "A new paragraph."

Each use adds a new paragraph to the commit message. A similar effect can be achieved using quotes:

$ git commit -m "Commit title
> A new paragraph."

If you belatedly decide that you should have used an editor after all you can pass the -e flag to open your editor with the partially-complete message intact:

$ git commit -m "Commit title" -m "Now I want an editor..." -e

List Aliases

Adding this snippet to your .gitconfig file creates a git alias command which lists all registered aliases:

    alias = !git config --list | grep alias | sed s/^alias.//

The Missing 'Amend' Command

Write a commit message, hit return, instantly notice the typo...

The fix is to run:

$ git commit --amend

This opens the last commit message in your editor, allowing you to rewrite it. I do this often enough that I've added the following alias to my .gitconfig file:

    amend = commit --amend

This way I can run git amend instead.

Discard All Changes

Discard all changes in the working directory — i.e. restore its state to that at the last commit — by running:

$ git checkout -f