Tips and tricks to be better at using git.
Leverage your tools!
To be efficient at git, you need to be efficient with your terminal.
- You should be able to use autocompletion for your branch names, and git commands.
- Have the name of your current branch be part of your prompt
- Use whatever shell you want, but configure it correctly. If you don't have a preference and/or don't want to spend a lot of time on it, check out oh-my-zsh, it will do a lot of this right out of the box.
Configure your git!
Git is amazing, but doesn't ship with the best default configuration. Here are a few settings I strongly recommend you configure:
# Adds color to your git output git config --global color.ui auto # -> `git push origin name_of_my_current_branch` becomes `git push` git config --global push.default current
- Configure git to use your favorite text editor (sublime for me):
git config --global core.editor 'subl -w'
- Autocorrect typos
git config --global help.autocorrect 1
When there's a typo in your git command and it's not ambiguous, git will autocorrect it.
Git commands can be long and hard to remember, your shell allows you to create
aliases to make your life easier. Here is a few of my favorite ones:
alias gs='git status' # less typing alias gpr='git pull --rebase' # less typing, more rebasing alias gspr='git stash; gpr; git stash apply' # useful for keeping local changes across a rebase alias push='git push' # less typing alias gd='git diff | subl --new-window' # to see my `git diff`s in my editor # because I can't type alias gpt='git' alias got='git' # runs rspec on all the _spec files that have changed. alias gspec='rspec `git ls-files -mo | grep -C0 -E -e '_spec.rb$'`' # opens all the files with conflicts in my text editor alias gsolve="git diff --name-only --diff-filter=U | xargs subl"
Git Knows Better
You will doubt git:
- "I already fixed this conflict!"
- "This should not have changed"
- "That's definitely a bug in git"
- "I didn't have to put up with this when I was using SVN"
Take a deep breath, relax, and say it out loud:
Git Knows Better
It really does. It's a robot that can remember a million variations of your file on a million different computers. You're just a silly human who couldn't even tell the difference between a tab and 4 spaces. It's OK to be wrong, focus your efforts on addressing the issues it points out. Hopefully it will be easier to do with this guide.