Razzi's guide to Git

I use git for version control because it makes it easy to iterate on changes and share code. It is very powerful albeit a bit user-unfriendly.

Here are some tips to help you get the most out of git.


Git documentation can be viewed with the man command: man git for example.

Even the official documentation is just a directory listing of manual pages.

Subcommands documentation is accessible by putting a - between the parts:

$ man git-diff

man git diff, by contrast, will show documentation for git, then diff (on MacOS at least; Ubuntu corrects this to git-diff).

Once you understand how to make local changes, the real power of git comes with its collaboration workflows. Here’s a post I recommend with some workflows illustrated.



I configure git heavily; the default cli has some rough edges.

git also helps here, as it allows adding aliases in a ~/.gitconfig file, seamlessly integrating your commands into the git toolkit. Here are some simple examples:

$ cat ~/.gitconfig
	# git current - output current branch name
	current = rev-parse --abbrev-ref HEAD

	# git new - show staged changes
	new = diff --cached

	# git root - output the directory at the root of the current repository
	root = rev-parse --show-toplevel

	# git stash-unstaged - stash unstaged changes, leaving staged changes alone
	stash-unstaged = stash --keep-index --include-untracked

For commands that are more complicated than aliases, use a ! at the start of the alias value:

	# git commit-id - output the last commit id
	commit-id = "!git rev-parse @ | tr -d '\n'"

You can even implement commands with multiple lines by escaping newlines:

	# git stash-staged: stash staged changes only
    stash-staged = "!\
        git stash-unstaged > /dev/null &&\
        git stash save > /dev/null &&\
        git stash apply stash@{1} > /dev/null &&\
        git stash show -p | git apply -R &&\
        git stash drop stash@{1} > /dev/null \

Once commands get to a certain complexity, consider making a standalone script. But you can still use it like an alias: if you have a command named git-something on your path, running git something will invoke your command.

Other configuration

There are some simple configuration options I also recommend putting in your ~/.gitconfig.

	# Show dates relative to the current date, such as "5 minutes ago"
	date = relative
	# Show diff stat upon rebase, similar to `git merge`
	stat = true

I track my gitconfig in git, of course. You can view it here.

See Also

Depends On