Git Commit – Creating Clean, Effective & Professional Commits

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:


    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.txt

    Then commit:

    git commit -m "Add new feature"

    Commit all staged files

    git commit -m "Your message"
    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 fields
    fix: correct variable reference in ansible playbook
    docs: update Git installation instructions

    Best 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 log

    Compact, one-line:

    git log --oneline

    With graph tree:

    git log --oneline --graph

    If using alias:

    git lg

    5. 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~1

    Mixed reset (default — keep changes unstaged)

    git reset HEAD~1

    Hard reset (dangerous — deletes changes)

    git reset --hard HEAD~1

    Hard 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~3

    Mark 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 true

    This 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 --oneline

    11. 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

    ActionCommand
    Create a commitgit commit -m "msg"
    Skip staging for modified filesgit commit -a -m "msg"
    Amend last commitgit commit --amend
    View historygit log
    View graphgit log --oneline --graph
    Revert commitgit revert <hash>
    Reset commit (local only)git reset <mode>
    Squash commitsgit 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:

    ➡️ Part 7 – Git Tagging: Creating, Listing & Managing Tags (Lightweight & Annotated)
    Learn how to version your releases the proper way, including semantic versioning (v1.0.0), annotated tags, pushing tags to GitHub, and deleting remote tags.

    ________________________________________________

    Leave a Reply

    Your email address will not be published. Required fields are marked *