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.
git add before
git commit. The add command stages changes, while the
commit command records those staged changes in the repository.
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.
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
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.
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.
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.
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.
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
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.
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.
Edit Files
↓
git status
↓
git add
↓
git status
↓
git commit
↓
Repository History
This is the basic workflow for creating commits.
After creating a commit, you can view it using:
git log
The log displays commit history, including commit identifiers, authors, dates and messages.
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.
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"
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
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.
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.
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.
After committing, use:
git status
If there are no remaining changes, Git may display:
nothing to commit, working tree clean
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.
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.
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.
You can inspect the latest commit using:
git show
This can display information about the latest commit and its changes.
Git maintains a history of commits. You can view the history with:
git log
For a compact history, you can use:
git log --oneline
Commits are connected to branches. A branch usually points to a commit, and new commits move the branch forward.
main
|
A → B → C
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.
git status before committing.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
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.
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.
-m to provide a commit message.git commit -a stages modifications and deletions of tracked files before committing.git commit --amend can modify the most recent commit.Question: Which command is used to record staged changes in the Git repository?