Collection of git best practices

This is a random collection of best practices for using git. I use it to keep track of what I’ve tested and used found notable.

Truncating git history

Sometimes, I copy git repositories as a means to speed up setup for repeating tasks. For example, when preparing presentations. The problem with this approach: All prior history will be kept. The tasks is to discard all commits prior to a particular one.

Source: passingcuriosity.com

  1. Get the commit reference. I use my alias gh, which relates to:
git log --graph --decorate --pretty=oneline --abbrev-commit
  1. Use rebase on a separate branch (called “temp”) to clean up all previous commits:
git checkout --orphan temp 14c3e19
git commit -m "Truncate history"
git rebase --onto temp 14c3e19 master

The last command will apply changes back to master, and remove all commits before the specified one.

Remove any uncommited changes

git reset --hard

Aliases

Instead of git aliases, I use the following bash aliases to shorten command line work:

alias gs='git status'
alias gr='git remote --verbose'
alias gc='git commit -m'
alias gp='git push'
alias gd='git diff'
alias ga='git add'
alias gh='git log --graph --decorate --pretty=oneline --abbrev-commit'
alias gb='git branch'
alias gp='git push'
alias gpl='git pull'
alias gprep='git config --local core.filemode false'

Add these to your ~/.bash_aliases file. Make sure that none of these interfere with any other packages or commands on your system.