Introduction
Git Hygiene
- Global configuration is via
.gitconfig - Local configuration is via
.git/configand 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 --amendandgit commit --fixup, better still make life easier usinggit absorb. -
git rebase --interactivecan be used to squash commits. - Use
git add --patchto group changes into atomic commits. - Use Conventional commits and write informative messages.
- Keeping the commit history atomic and clean makes it easier to understand what work has been undertaken.
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 commitor compare them usinggit diff.
Diverging Branches
- Branches can become outdated as work progresses
- Branches can be brought up-to-date with either
git mergeorgit rebase.
Hooks
- Hooks are actions run by Git before or after particular events such
as
commit,pushandpullvia scripts. - They are defined in Bash scripts in the
.git/hooksdirectory. - The
pre-commitframework 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
mainbranch.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.