JavaScript • Getting Started and Tooling

Git Commits — Commit source code changes with meaningful messages (conceptual workflow)

Learn how developers save snapshots of their code and communicate changes clearly using Git commits.

JavaScript emblem with code brackets, braces, and a code file.
Inside this lesson 10 sections 5 questions 5 figures

SECTION 1

Historical Context & Motivation

Imagine working on a group essay in Google Docs, but every time someone edits the document, the previous version vanishes completely. You could never undo a bad change or figure out who wrote what. Before modern version control systems, software developers faced exactly this problem. Teams would email files back and forth, name them things like project_final_v2_FINAL.js, and hope nothing got lost.

The need to track changes, collaborate safely, and maintain a reliable history of a project's evolution led to the development of version control tools. The most influential of these tools is Git, created in 2005 by Linus Torvalds — the same person who created the Linux operating system. At the heart of Git lies a simple but powerful concept: the commit.

1972 • SCCS — Source Code Control System

Bell Labs creates one of the first systems to track changes to source code files, though it only works on one file at a time.

1986 • CVS — Concurrent Versions System

CVS allows multiple developers to work on the same project simultaneously by using a central server to store the code history.

2000 • Subversion (SVN)

Apache Subversion improves on CVS with better support for renaming files and tracking directories, but still relies on a single central server.

2005 • Git Is Born

Linus Torvalds creates Git as a fast, distributed version control system. Every developer gets a full copy of the project history on their own machine.

2008 • GitHub Launches

GitHub makes Git accessible with a web-based interface, transforming how open-source and professional teams collaborate on code worldwide.

So what exactly is a commit, and why does the message you attach to it matter so much? Understanding this concept is essential for any developer — whether you are building a JavaScript project for a class or contributing to a real-world application.

SECTION 2

Core Principles & Definitions

A Git commit is a snapshot of your project at a specific point in time. When you commit, Git records which files changed, what changed inside them, who made the change, when it happened, and a short message describing why. Think of it as pressing a "save checkpoint" button in a video game — you can always return to that exact state later.

1

Snapshot, Not a Difference

Each commit captures the full state of every tracked file, not just what changed. Git uses efficient compression so this doesn't waste disk space.
2

The Staging Area

Before committing, you choose which changes to include using the staging area (also called the index). This lets you organize changes into logical groups.
3

Unique Hash (SHA-1)

Every commit receives a unique identifier — a 40-character hexadecimal string called a hash. For example: a3f7c92 (typically shortened to 7 characters).
4

Commit Message

A human-readable description of what the commit does and why. Good messages make a project's history understandable months or years later.
5

Linked History

Each commit points back to its parent commit, forming a chain of changes. This linked chain creates the project's complete history.
✦

KEY TAKEAWAY

Think of Git commits like entries in a ship captain's logbook. Each entry records the date, what happened, and why decisions were made. If the ship runs into trouble later, the crew can look back through the log to understand the journey. A commit without a clear message is like a blank log entry — technically present, but useless when you need it most.

SECTION 3

Visual Explanation — The Git Commit Workflow

Understanding how files move through Git's three main areas is essential. Your code lives in the working directory, gets organized in the staging area, and is permanently recorded in the repository. The diagram below shows this three-step flow.

The Three Areas of GitWORKING DIRECTORYYour project filesindex.js ✏️style.css ✏️app.js✏️ = modifiedSTAGING AREAChanges ready to commitindex.js ✅style.css ✅✅ = stagedREPOSITORYPermanent commit historyC3C2C1git addgit commitFiles flow left to right: edit → stage → commit
Files in the working directory are edited freely. Use git add to move selected changes into the staging area. Then git commit saves that snapshot permanently into the repository.

Notice that the staging area acts as a deliberate checkpoint. You don't have to commit every change you've made — you can pick and choose which files or even which lines of code to include. This gives you control over how your project history reads. Each commit in the repository (shown as C1, C2, C3) links back to the one before it, forming a chain that tells the story of your project from its very first line of code.

SECTION 4

How a Commit Works Under the Hood

When you run git commit -m "your message", Git does several things behind the scenes. Understanding this process helps you appreciate why commits are so reliable and why the commit message is a critical part of the package.

What Git Records in Each Commit

  • Tree object — a snapshot of every file and folder in the project at that moment.
  • Parent hash — the unique ID of the previous commit, linking the new commit to the history chain.
  • Author and timestamp — who made the commit and exactly when it happened.
  • Commit message — the human-readable description you provide with the -m flag.

The Three-Command Workflow

In practice, the typical workflow for making a commit involves three Git commands. First, git status shows you which files have been modified since your last commit. Second, git add <filename> (or git add . to stage everything) moves your chosen changes into the staging area. Third, git commit -m "Fix navigation bar alignment" creates the commit with a descriptive message. This three-step process — check, stage, commit — becomes second nature with practice.

💡

Why Not Just Commit Everything at Once?

Imagine you fixed a bug in login.js and also redesigned the homepage in home.css. These are two unrelated changes. If you bundle them into one commit, it becomes harder to undo just the homepage redesign if something goes wrong. The staging area lets you create two separate, focused commits — one for each change.

Anatomy of a Commit Hash

COMMIT HASH GENERATION

SHA−1(tree+parent+author+timestamp+message)→40−characterhexstringSHA-1( tree + parent + author + timestamp + message ) → 40-character hex string

Git runs the SHA-1 algorithm on the commit's contents. Even a one-character change in the message produces a completely different hash, making every commit uniquely identifiable.

SECTION 5

Writing Meaningful Commit Messages

The commit message is arguably the most important part of a commit for collaboration. A well-written message lets any team member (or your future self) understand what changed and why, without having to read every line of code. The software industry has developed widely accepted conventions for writing effective commit messages.

Good vs. Bad Commit Messages✅ GOOD MESSAGESAdd email validation tosignup formFix crash when user submitsempty search queryRefactor header componentfor mobile responsivenessRemove deprecated API callin user profile module• Starts with a verb (imperative mood)• Explains what AND why• Under 50 characters (subject line)• Specific and searchable❌ BAD MESSAGESfix stuffasdfghupdated codeWIPchanges• Vague and unhelpful• No context for teammates• Impossible to search later• Doesn't describe the change
Good commit messages use the imperative mood ("Add feature" not "Added feature"), stay under 50 characters for the subject line, and clearly describe both what changed and why.

The Seven Rules of Great Commit Messages

  1. Use the imperative mood: "Add feature" not "Added feature" or "Adding feature."
  2. Keep the subject line under 50 characters so it displays cleanly in Git logs and on GitHub.
  3. Capitalize the first word of the subject line.
  4. Do not end the subject line with a period.
  5. Separate the subject line from the body with a blank line (when writing a longer message).
  6. Explain what and why, not how — the code itself shows how.
  7. Each commit should represent one logical change — avoid mixing unrelated fixes.

SECTION 6

Worked Example — Making Your First Commits

Let's walk through a realistic scenario. You're building a simple JavaScript to-do list application. You've already initialized a Git repository with git init. Now you want to make your first few commits as you build out the project.

Creating Three Focused Commits for a To-Do App

  1. 1

    Step 1 — Create the HTML structure

    You create index.html with a basic page layout including a heading, an input field, and an empty list. You check your changes with git status, which shows index.html as an untracked file.
    git add index.html → git commit -m "Create HTML skeleton with input field and task list"
  2. 2

    Step 2 — Add JavaScript functionality

    You create app.js and write a function that lets users type a task and press Enter to add it to the list. This is a separate logical change from the HTML, so it deserves its own commit.
    git add app.js → git commit -m "Add JavaScript handler to create new tasks on Enter key"
  3. 3

    Step 3 — Style the application

    You create style.css and add basic styling. You also update index.html to link the new stylesheet. Both files are related to the same change (styling), so you stage them together.
    git add style.css index.html → git commit -m "Add CSS styling and link stylesheet in HTML"
  4. 4

    Step 4 — Verify your history

    Run git log --oneline to see a clean, readable history of all three commits. Each message clearly describes one logical change, and anyone reading the log can understand the project's evolution.
    a3f7c92 Add CSS styling and link stylesheet in HTML b1d4e56 Add JavaScript handler to create new tasks on Enter key c8a2f01 Create HTML skeleton with input field and task list
✦

KEY TAKEAWAY

Notice how each commit addresses exactly one concern — structure, behavior, and style. This is like writing a research paper where each paragraph has one main idea. If you mixed everything into a single commit called "Built the app," it would be like writing your entire paper as one giant paragraph — technically complete, but impossible to navigate or revise.

SECTION 7

Good Commits vs. Bad Commits — Strengths & Pitfalls

Not all commits are created equal. The table below contrasts disciplined commit practices with common mistakes that new developers often make. Recognizing these patterns early will save you — and your teammates — significant frustration.

AspectGood PracticeCommon Mistake
Commit sizeSmall, focused — one logical change per commitHuge commits with dozens of unrelated changes bundled together
Message clarity"Fix null pointer error in user authentication" — specific and descriptive"fix bug" or "stuff" — vague and unhelpful
Commit frequencyCommit after each meaningful milestone, even if smallWorking for hours and committing everything at the end of the day
Broken codeEvery commit should leave the project in a working stateCommitting half-finished code that crashes when run
History readabilityReading the log tells a clear story of the project's developmentThe log is a jumbled mess that provides no useful information

Comparing disciplined commit habits with common beginner mistakes

🔑

THE BIGGER PICTURE

Professional software teams treat their commit history like a permanent record. During code reviews, bug investigations, and new team member onboarding, clear commit messages are invaluable. Developing good commit habits now — even in your personal projects — will set you apart as you enter the software industry or contribute to open-source projects.

SECTION 8

Connection to Advanced Git Concepts

The commit is the fundamental building block of Git, but as your projects grow more complex, you'll encounter more powerful features that build directly on commits. Understanding commits deeply makes these advanced concepts much easier to learn.

What You Know NowWhat Comes NextHow They Connect
Making commits on a single timelineBranching — creating parallel timelinesA branch is just a pointer to a specific commit. Creating a branch starts a new line of commits from that point.
Linking commits in a chainMerging — combining two commit chainsMerging takes commits from one branch and integrates them into another, creating a merge commit.
Viewing history with git loggit revert / git reset — undoing commitsRevert creates a new commit that undoes a previous one. Reset moves the branch pointer to a different commit.
Committing locallygit push / git pull — sharing commits remotelyPush sends your local commits to a remote server (like GitHub). Pull downloads teammates' commits to your machine.

How basic commit knowledge connects to advanced Git features

As you continue learning, you'll also encounter concepts like pull requests (proposing your commits be added to a shared project), rebasing (rewriting commit history for cleanliness), and cherry-picking (selecting individual commits from one branch to apply to another). All of these features rely on the commit as their fundamental unit. The better your commits are now, the smoother these advanced workflows will be.

SECTION 9

Practice Problems

Question 1: CONCEPTUAL

Explain in your own words why Git uses a staging area instead of committing all changed files automatically. What benefit does this extra step provide?
Show answer
The staging area gives developers control over what goes into each commit. Without it, every changed file would be included, even if the changes are unrelated. For example, if you fixed a bug in one file and started a new feature in another, the staging area lets you commit just the bug fix first. This keeps each commit focused on a single logical change, making the project history easier to read, review, and debug.

Question 2: BASIC CALCULATION

You have modified three files: header.js, footer.js, and utils.js. The changes in header.js and footer.js relate to updating the site navigation, while utils.js has a completely separate bug fix. Write the exact Git commands you would use to create two separate, well-organized commits.
Show answer
First commit (navigation update): git add header.js footer.js followed by git commit -m "Update navigation links in header and footer components". Second commit (bug fix): git add utils.js followed by git commit -m "Fix date formatting bug in utility helper function". By separating these into two commits, each change can be reviewed, reverted, or referenced independently.

Question 3: INTERMEDIATE

A teammate shows you the following commit log: e4f2a1b stuff c3d1b9a fix a1b2c3d WIP f7e8d9c update Identify at least three specific problems with this commit history. Then rewrite the messages assuming the changes were: adding user login, fixing a CSS alignment bug, starting a search feature, and updating the README file.
Show answer
Problems: (1) Every message is vague — none describe what actually changed. (2) "WIP" (work in progress) suggests incomplete code was committed, which could break the project for others. (3) The messages lack imperative mood and specific context, making the history useless for debugging or code review. Rewritten messages: f7e8d9c Update README with installation instructions → a1b2c3d Add search bar component with basic text filtering → c3d1b9a Fix sidebar alignment issue on tablet screen sizes → e4f2a1b Implement user login with email and password validation. Now each message tells a clear story.

Question 4: APPLIED

You are working on a team project — a JavaScript quiz app. You discover that a bug was introduced sometime in the last 10 commits that causes the score counter to show NaN (Not a Number). Describe how well-written commit messages would help you track down the bug more efficiently than poorly written ones. What specific information in the messages would you look for?
Show answer
With well-written commit messages, you would scan the log for commits related to the score counter or any numerical calculations — messages like "Refactor score calculation logic" or "Add bonus points for streak answers" would immediately narrow your search to the relevant commits. You could then inspect just those commits to find where the bug was introduced. With vague messages like "update" or "changes," you would have to open every single commit and read through all the code diffs to find the problem, which could take many times longer. Specific, descriptive messages act like an index in a textbook — they let you jump directly to the relevant section instead of reading the entire book.

Question 5: CRITICAL THINKING

Some developers argue that commit messages don't matter because "the code speaks for itself." Others insist that a project's commit history is just as important as the code itself. Construct an argument for or against the importance of commit messages, using at least two specific scenarios to support your position.
Show answer
Commit messages are essential because code shows what was done but not why. Scenario 1: A developer removes a seemingly useful function. Without a commit message explaining "Remove deprecated getUserData function due to security vulnerability in third-party API," a new team member might reintroduce the vulnerable code, thinking it was deleted by mistake. Scenario 2: Six months from now, you need to add a similar feature to one you built previously. A clear commit history lets you search for "Add pagination" and find exactly how you implemented it before, saving hours of digging through old code. While code is technically readable on its own, commit messages provide the narrative context — the decisions, trade-offs, and reasoning — that no amount of clean code can fully convey.

SUMMARY

Summary — Git Commits and Meaningful Messages

Summary

A Git commit is a permanent snapshot of your project at a specific point in time. Files move through three areas: the working directory (where you edit), the staging area (where you select changes with git add), and the repository (where commits are stored permanently with git commit). Each commit is identified by a unique SHA-1 hash and linked to its parent, forming a traceable chain of project history.

Writing meaningful commit messages is just as important as writing clean code. Follow the imperative mood ("Add feature," not "Added feature"), keep subject lines under 50 characters, and make each commit represent one logical change. This discipline makes your project history readable, searchable, and useful for debugging. These foundational skills prepare you for advanced Git concepts like branching, merging, and collaborating with teams through platforms like GitHub.

Varsity Tutors • JavaScript • Git Commits — Commit source code changes with meaningful messages (conceptual workflow)