Awesome Git Commit Message
Last updated: Aug 7, 2019
Why care about commit message anyways? What’s the need?
We write commit message to get a glance of development activity. Hence writing Awesome commit message might mean people scouring through git logs would be able to understand development activity better and faster.
some tips to write awesome git commit messages.
- Use present tense: add awesome-package, not added …
- Use preceding subsystem when applicable: registry: add theme foobar
- 50 characters or less for the first line of your commit
- Use of emoji is free1
Anatomy of a commit message
Examples of well crafted repositories
While many repositories’ logs look like the former, there are exceptions. The Linux kernel and Git itself are great examples. Look at Spring Boot, or any repository managed by Tim Pope.
Subject in IMPERATIVE
Explanation, what, why
- imperative verbs
- Soften
- Add
- Fix
- Rewrite
- Avoid
- Close
- Document
- Refactor
Optimize
Reference
- To reference an issue:
#123
- To reference a MR:
!123
- To reference a snippet
$123
- To reference an issue:
strive for atomic commits
How to automate this process?
- commit-msg
- git commit hook to check/lint commit messages for issue IDs
-
git log --pretty=oneline --abbrev-commit \ | cut --delimiter=' ' --fields=1 --complement \ | grep '\[.*\]' --count
git staged | unstaged files
Even after git add
ing your files and later changing them, git may again ask you to stage
your files! Why is that?
when you change a file which is already in the repository, you have to git add it again if you want it to be staged.
This allows you to commit only a subset of the changes you made since the last commit. For example, let’s say you have file a, file b and file c. You modify file a and file b but the changes are very different in nature and you don’t want all of them to be in one single commit. You issue
git add a
git commit file_a -m "bugfix, in a"
git add b
git commit file_b -m "new feature, in b"
# if you want to commit everything you can just type
git commit -a
How to unstage a file?
git rm --cached <filePath>
does not unstage a file, it actually stages the removal of the file(s) from the repo (assuming it was already committed before) but leaves the file in your working tree (leaving you with an untracked file). will unstage the file for future commits also. It’s unstaged until it gets added again with git add
git reset -- <filePath>
will unstages the file in the current commit.
Gerrit
Web based code-review system. Read the code and comment on it. Learn from code
- First started as the code-review for Android project, 500+ repositories
- Has git ACLs, restricts branch, repo views.
- Code reviews are at commit level unlike branch level @github
- Single view for multiple repositories
- Gerrit intercepts the commit and make a code-review
- Reviwed changes are
amended
instead of new commit Pushing is gerrits speciality. When pushing for code-review
git push origin HEAD:refs/for/master
When by-passing code-review stage
git push origin HEAD:master