DevOps · Infrastructure as Code (IaC) · Git Series – Part 6
Now that you understand the staging area, it’s time to learn the most important action in Git: committing.
A commit is a snapshot of your project at a point in time. Commits help you track history, revert changes, collaborate effectively — and maintain project quality.
In this chapter, you’ll learn:
Table of Contents
1. What Is a Git Commit?
A commit is a stored record of:
- What changed
- Who made the change
- When the change was made
- Why the change was made
Each commit has:
- A commit hash
- An author
- A timestamp
- A commit message
- A snapshot of staged changes
Commits form a linked chain (this is Git’s DAG structure), enabling powerful version control.
2. How to Create a Commit
First stage changes:
git add file.txtThen commit:
git commit -m "Add new feature"Commit all staged files
git commit -m "Your message"Commit and skip staging (not recommended for big changes)
git commit -a -m "Quick update"This auto-stages modified files but does not include new files.
3. Writing Professional Commit Messages
Good commit messages are essential for clarity in DevOps, CI/CD, and collaboration.
✔ Follow the standard structure:
<type>: <short description>
<body (optional)>
<footer (optional)>
Common commit types:
- feat: new feature
- fix: bug fix
- docs: documentation
- style: formatting (no code changes)
- refactor: code restructuring
- test: adding or updating tests
- chore: maintenance tasks
- ci: CI/CD updates
Examples:
feat: add login validation for empty fieldsfix: correct variable reference in ansible playbookdocs: update Git installation instructionsBest practices:
✔ Use present tense (“add” not “added”)
✔ Keep subject line ≤ 50 characters
✔ First letter lowercase (common in DevOps teams)
✔ Use the body when needed to explain why
✔ Ensure each commit is cohesive and focused
4. Viewing Commit History
Basic log:
git logCompact, one-line:
git log --onelineWith graph tree:
git log --oneline --graphIf using alias:
git lg5. Amending the Last Commit
Used to:
- Fix a typo in the message
- Add missed changes
- Combine staged changes with the previous commit
Change commit message
git commit --amend -m "new message"Add more changes to last commit
Stage the new changes:
git add file.txt
git commit --amend⚠️ Warning
If the commit is already pushed to a shared branch, amending rewrites history → risky.
6. Reverting a Commit (Safe Undo)
If a commit has gone wrong, revert creates a new commit that undoes it.
Revert a specific commit:
git revert <commit-hash>This is preferred for production workflows because it does not rewrite history.
7. Resetting Commits (History Rewrite)
⚠️ For local work only — do NOT use on shared branches.
Soft reset (keep changes staged)
git reset --soft HEAD~1Mixed reset (default — keep changes unstaged)
git reset HEAD~1Hard reset (dangerous — deletes changes)
git reset --hard HEAD~1Hard reset rewrites history and deletes modifications — avoid unless necessary.
8. Squashing Commits
Used to combine multiple commits into one clean commit.
Interactive rebase:
git rebase -i HEAD~3Mark commits as squash (s) or fixup (f), then save.
Useful for:
- Organizing messy commits
- Cleaning up PR history
- Preparing code for production
9. Signing Commits (Advanced but Recommended)
Git supports GPG or SSH signing to verify commit authenticity.
Enable signing globally:
git config --global commit.gpgsign trueThis is often used in enterprises, DevOps pipelines, or open-source projects.
10. Real Hands-On Workflow Example
git init
echo "Line 1" > demo.txt
git add demo.txt
git commit -m "feat: add initial demo file"
echo "Line 2" >> demo.txt
git add demo.txt
git commit -m "feat: update demo with line 2"
git log --graph --oneline11. Git Commit Best Practices (DevOps-Grade)
✔ Make small, focused commits
Easier to debug, revert, and review.
✔ Use meaningful messages
Explain what and why.
✔ Commit early, commit often
Avoid large, hard-to-review commits.
✔ Avoid committing secrets
Use .gitignore and secret scanners.
✔ Don’t commit directly to main/master
Use branches → PR → review → merge.
✔ Don’t amend or reset pushed commits
Breaks team workflows.
12. Summary Table – Commit Commands
| Action | Command |
|---|---|
| Create a commit | git commit -m "msg" |
| Skip staging for modified files | git commit -a -m "msg" |
| Amend last commit | git commit --amend |
| View history | git log |
| View graph | git log --oneline --graph |
| Revert commit | git revert <hash> |
| Reset commit (local only) | git reset <mode> |
| Squash commits | git rebase -i HEAD~N |
13. What’s Next?
You now understand:
✔ How commits work
✔ How to write good commit messages
✔ Amend, revert, reset, squash
✔ Best practices for DevOps & IaC
Next up:
________________________________________________