This section contains some additional recommendations that are not critical but
considered good practice.
Keep a tidy history
Unlike Subversion, Git is a sort of two phase commit system.
A change is first committed locally
That change is then pushed up to a repository
The locality of commits usually tends to lend itself to multiple commits when performing
a change. For example:
% git commit -m "GEOT-XYZ, fixing a bug"
% git commit -m "GEOT-XYZ, oops, forgot to add a file"
% git commit -m "GEOT-XYZ, oops, forgot to fix that test failure"
Since all these changes are related to a single fix it is ideal to keep them as a single
commit. Since these commits are still local and not yet pushed up the canonical
repository they can be “squashed”. Commit squashing is an interactive rebase that
merges multiple commits into one. For example:
% git log --pretty=oneline --abbrev-commit
ad7sdfd GEOT-XYZ, oops, forgot to fix that test failure
99dhdff GEOT-XYZ, oops, forgot to add a file
8u3n8dd GEOT-XYZ, fixing a bug
882217a tweaking doc version to avoid @RELEASE@ tag in snapshot builds
% git rebase -i 8u3n8dd
An editor is then presented that allows a developer to merge first three commits into
one.
Read more about interactive rebasing.
Avoid merge commits when possible
In git a merge commit results when a branch is merged with another branch. This
commonly occurs when a developer pulls in changes from a branch in the canonical
repository. For example:
% git checkout main
% git commit -m "GEOT-XYZ, making a simple change"
% git pull geotools main
The pull from the canonical repository will cause a merge commit to occur. A simple way
to avoid this is to force a “fast forward” by using the “–rebase” option to the pull
command:
% git checkout main
% git commit -m "GEOT-XYZ, making a simple change"
% git pull --rebase geotools main
The rebase option essentially will stash all local changes before doing the full,
resulting in a fast forward update (avoiding a merge commit), and then replay the local
changes on top of that.