Introduction
Git Hygiene
- Global configuration is via
.gitconfig
- Local configuration is via
.git/config
and takes precedence over Global. - Configuration can be done at the command line or by editing files.
- Ignore files using
.gitignore
. - Make commits atomic, i.e. small and focused using
git commit --amend
andgit commit --fixup
, better still make life easier usinggit absorb
. -
git rebase --interactive
can be used to squash commits. - Keeping the commit history atomic and clean makes it easier to understand what work has been undertaken.
- Git periodically tidies things up for you with
git gc
. - You can and should enable further automated cleaning by enabling
git mainenance
on a repository.
Branching
- Branches and how they relate to each other are fundamental to collaborating using Git.
- The history of a branch is a series of commits and extends all the way back to the very first commit and not the point at which it forked from its parents.
- Branches can be easily created, merged and deleted.
- Commits all have references and Git can move you between these
references using
git commit
or compare them usinggit diff
.
Diverging Branches
- Branches can become outdated as work progresses
- Branches can be brought up-to-date with either
git merge
orgit rebase
.
Hooks
- Hooks are actions run by Git before or after particular events such
as
commit
,push
andpull
via scripts. - They are defined in Bash scripts in the
.git/hooks
directory. - The
pre-commit
framework provides a wealth of hooks that can be enabled to run, by default, before commits are made. - Each hook can be configured to run on specific files, or to take additional arguments.
- Local hooks can be configured to run when dependencies that will only be found on your system/virtual environment are required.
- Use hooks liberally as you develop your code locally, they save you time.
Continuous Integration
Continuous Integration/Delivery is a useful method of checking code before it enters the
main
branch.GitHub uses Actions that are defined by YAML configuration files under
.github/workflow/
.Actions can be restricted to events/branches/tags.
pre-commit.ci allows integration of pre-commit hooks in GitHub Actions.