Tips using Git
Table of Contents
Tip #1: git hooks
git hooks are scripts that run pre
/post
every git
command you run in your
repository.
Using git hooks you can ensure all team members run certain checks prior to doing
various actions, such as commit
.
To create a git hooks check .git/hooks
directory in your repository.
It contains sample scripts, to enable one, rename the script and remove the .sample
suffix.
Pre-commit
One of the most common hooks is pre-commit
, usually used for running formatters to avoid “formatting wars” between
users.
For example prettier instructions how to enable prettier
as a pre-commit
hook.
This is effectively a must-have for any project with more than one developer.
This blog
has a git submodule
, I have setup
a pre-commit hook to ensure the submodule
is up-to-date.
Pre-rebase
rebase
is a powerful tool, but it can be dangerous and lead to lose of work by accident.
One way to prevent this is to backup your current branch using a
pre-rebase
hook.
This hook
simple creates a new branch
and push
it, and returns to the original branch
.
Simple yet highly effective.
Caveats
git hooks are shared by default when someone clone
a repository, so be careful
and ensure all users set them up.
Tip #2: fsmonitor daemon
fsmonitor–daemon improves performance of git
commands by listening
to file system changes and updating the index accordingly.
This is very significant for large repositories and have little down side to using it.
Tip #3: useful aliases
- Merged branches:
alias gitmerged="git branch --merged master | grep -v master"
- Branches by creation
date:
alias gitbrachbydate="git for-each-ref --sort=committerdate --format='%(committerdate:short) %(refname:short)' refs/heads"
. - Show last commit:
alias gitlastcommit="git log -1 --pretty=format:%H"
. - Verbose remote:
alias gitremoteverbose="git remote --verbose"
. - Git daemon status:
alias gitfsmonitor_status='git fsmonitor--daemon status'