Lesson 13 of 60 – Git Commit
22%

Git Commit

The git commit command is used to save staged changes into the Git repository as a new commit. A commit represents a recorded point in the project's history.

Note: You normally use git add before git commit. The add command stages changes, while the commit command records those staged changes in the repository.

1. What is git commit?

git commit records the changes that are currently in the staging area.

git commit -m "Initial commit"

The commit becomes part of the repository's history.

2. Why Do We Need Commits?

Commits allow you to create a history of changes made to your project.

Each commit represents a point in the development of the project.

Version 1
   ↓
Version 2
   ↓
Version 3

3. Basic Syntax

The basic syntax of a commit is:

git commit -m "commit message"

The -m option allows you to provide a commit message directly from the command line.

4. The -m Option

The -m option is used to specify a commit message.

git commit -m "Add homepage"

The text inside quotation marks describes what the commit contains.

5. Creating Your First Commit

After initializing a repository and staging files, you can create your first commit:

git init
git add .
git commit -m "Initial commit"

This records the staged project files in Git history.

6. Commit Message

A commit message explains what the commit represents.

For example:

git commit -m "Add student registration form"

A clear message makes project history easier to understand.

7. Why Good Commit Messages Matter

A repository may contain hundreds or thousands of commits. Clear commit messages make it easier to understand what changed.

For example:

Add login page
Fix registration validation
Update database connection

8. Commit Only Staged Changes

A normal git commit records changes that are in the staging area.

git add index.html
git commit -m "Update homepage"

Other unstaged changes remain in the working directory.

9. Checking Before Commit

Before committing, it is useful to check the repository status:

git status

This helps you see which changes are staged and which are not staged.

10. Commit Workflow

Edit Files
    ↓
git status
    ↓
git add
    ↓
git status
    ↓
git commit
    ↓
Repository History

This is the basic workflow for creating commits.

11. Viewing Commits with git log

After creating a commit, you can view it using:

git log

The log displays commit history, including commit identifiers, authors, dates and messages.

12. Commit Hash

Each Git commit has a unique identifier commonly displayed as a commit hash.

For example, a log may contain:

commit 8f3a2b1...

The hash helps Git identify a particular commit.

13. Author Information

Git records author information with commits.

You can configure your name and email using:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

14. Commit Date

Git records date and time information associated with commits. This helps developers understand when changes were made.

The information can be viewed using:

git log

15. Multiple Commits

A project can contain many commits. For example:

git commit -m "Create homepage"
git commit -m "Add navigation"
git commit -m "Add contact page"

Each commit creates another point in the project history.

16. Small and Focused Commits

It is often easier to understand project history when commits represent focused changes.

For example:

Add login form
Fix login validation

This is more descriptive than using a vague message for unrelated changes.

17. Commit After Staging

A common sequence is:

git add index.html
git commit -m "Update index page"

The staged version of the file is recorded in the commit.

18. Checking the Working Tree After Commit

After committing, use:

git status

If there are no remaining changes, Git may display:

nothing to commit, working tree clean

19. Commit All Tracked Changes with -a

The -a option can automatically stage modifications and deletions of already tracked files before committing them.

git commit -a -m "Update project"

This does not include newly created untracked files. New files must be added explicitly.

20. Editing the Last Commit Message

If you want to change the message of the most recent commit, you can use:

git commit --amend -m "Correct commit message"

This modifies the most recent commit rather than creating another commit.

21. Adding Changes to the Last Commit

You can stage additional changes and use --amend to include them in the most recent commit.

git add index.html
git commit --amend --no-edit

The --no-edit option keeps the existing commit message.

22. Viewing the Latest Commit

You can inspect the latest commit using:

git show

This can display information about the latest commit and its changes.

23. Commit History

Git maintains a history of commits. You can view the history with:

git log

For a compact history, you can use:

git log --oneline

24. Commit and Branches

Commits are connected to branches. A branch usually points to a commit, and new commits move the branch forward.

main
 |
 A → B → C

25. Commit Does Not Upload to GitHub

A commit records changes in your local Git repository. It does not automatically upload those changes to GitHub.

git add .
git commit -m "Update project"

Uploading changes to a remote repository is normally done with git push.

26. Commit Best Practices

  • Write clear commit messages.
  • Keep commits focused on related changes.
  • Check git status before committing.
  • Review what you are staging.
  • Commit regularly during development.
  • Avoid vague messages such as "changes" or "update".

27. Common Commit Mistakes

  • Forgetting to stage changes.
  • Using unclear commit messages.
  • Expecting a commit to upload changes to GitHub.
  • Accidentally including unrelated changes.
  • Forgetting that new files need to be staged.
  • Using amend without understanding which commit is being changed.

28. Useful Commit Commands

git commit -m "Message"
git commit -a -m "Message"
git commit --amend -m "New message"
git commit --amend --no-edit
git log
git log --oneline
git show

29. Complete Commit Example

mkdir student-project
cd student-project

git init

touch index.html

git add index.html

git status

git commit -m "Create student project"

git log --oneline

git status

This example creates a repository, stages a file, creates a commit, views the commit history and checks the repository status.

30. Summary of git commit

The git commit command records staged changes in the local Git repository.

git add .
git commit -m "Initial commit"

A commit contains a snapshot of the staged project state along with information such as a commit message, author information and repository history data.

After creating a commit, you can inspect it using commands such as git log and git show.

📌 Key Points

  • git commit records staged changes.
  • Use -m to provide a commit message.
  • Commits create points in project history.
  • Each commit has a unique identifier.
  • Git records author and date information for commits.
  • Use git log to view commit history.
  • Use git show to inspect a commit.
  • git commit -a stages modifications and deletions of tracked files before committing.
  • New untracked files still need to be added explicitly.
  • git commit --amend can modify the most recent commit.
  • A commit is local and does not automatically push changes to GitHub.

🧠 Quick Quiz

Question: Which command is used to record staged changes in the Git repository?