A commit message should describe the changes made in the commit and provide context for those changes.
This makes it easier to understand what was changed, when it was changed, and why it was changed when reviewing the commit history.
Good commit messages are consistent, clear and concise.
The Conventional Commits specification provides a set of rules for commit messages:
The types we use the most are feat
, docs
, test
and chore
.
If your development process includes tracking work through Jira tickets or GitHub issues, you can include ticket numbers in your commits to link them to specific tasks:
Making small, frequent commits is a best practice in Git.
This helps keep your changes organized and reduces the risk of losing work if something goes wrong. It’s also easier to review and understand a series of small, focused changes than one large, sprawling change.
With smaller commits, you’ll have more opportunities to review and refine your work, and you’ll be able to roll back to a previous state if needed.
One big commit:
Smaller commits:
It’s important to be mindful of what you commit in Git.
Git remembers everything, so if you commit large files, binary files, or sensitive data, it will be stored in the repository forever.
.gitignore
To avoid this, it’s recommended to use a lang:shell-session:.gitignore
file to exclude files and directories that you don’t want to track in the repository.
A lang:shell-session:.gitignore
will mean that commands like lang:shell-session:git add .
will avoid adding some files.
Ironically the lang:shell-session:.gitignore
file should be checked into the repository.
An example lang:shell-session:.gitignore
is below:
When you’re working with Git, it’s important to be mindful of the commands you use.
Some commands like lang:shell-session: git checkout
and lang:shell-session:git reset
can overwrite your work.
lang:shell-session:git checkout
allows you to switch between branches and restore files to a previous state:
lang:shell-session:git reset
allows you to undo changes and return to a previous commit:
The lang:shell-session:$ git stash
command allows you to save changes that you haven’t committed yet, switch to a different branch, and then restore the changes later.
This is useful when you need to switch to a different branch to work on a bug fix or feature, but don’t want to commit your changes yet.
The stash allows you to save your changes and switch to the other branch without losing any work:
You can stash local, uncommitted changes with:
You can bring these changes back with: