Once you have carefully selected the changes you want to include in the next project snapshot using git add
(placing them into the staging area), the next step is to permanently store that snapshot in your repository's history. This is accomplished with the git commit
command. Think of a commit as saving your progress in a game; it creates a safe point you can return to later. Each commit represents a version of your project at a specific point in time.
The fundamental command is simply:
git commit
When you run this command without any additional options, Git will open the text editor you configured during setup (or a default editor like Vim or Nano). This editor prompts you to enter a commit message.
The commit message is a description of the changes you've made in this specific commit. It's a note to your future self and collaborators explaining why these changes were made. While we'll cover writing effective messages in the next section, the basic structure typically involves:
After you write your message, save the file and close the editor. Git will then create the commit, incorporating the changes currently in the staging area.
-m
ShortcutFor simpler changes where a short summary line is sufficient, you can bypass the text editor by providing the message directly on the command line using the -m
flag:
git commit -m "Add initial project structure with README"
This command performs the commit immediately using the provided string as the message. This is convenient for small, straightforward changes, but for more complex updates, taking the time to write a detailed message in the editor is often better practice.
When you execute git commit
, Git performs several actions:
main
) to point to this newly created commit.This process ensures that each commit is a complete, identifiable snapshot of your project, linked chronologically to previous versions.
The
git commit
command creates a new commit object (e.g.,9e1d5
) using the staged changes. TheHEAD
reference, which indicates your current position, along with the branch pointer (main
), moves forward to point to this new commit.
Let's walk through a typical sequence:
README.md
file.git status
. Git will report that README.md
is modified but not staged.
$ git status
On branch main
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
no changes added to commit (use "git add" and/or "git commit -a")
git add README.md
.git status
. Git now shows the file is staged and ready for commit.
$ git status
On branch main
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: README.md
git commit -m "Update README with project description"
.
$ git commit -m "Update README with project description"
[main 9e1d5f0] Update README with project description
1 file changed, 2 insertions(+)
git status
. Git now reports a clean working directory, indicating that all tracked changes have been committed.
$ git status
On branch main
nothing to commit, working tree clean
You have successfully recorded a snapshot of your project! The commit, identified by its unique SHA-1 hash (like 9e1d5f0
in the example output, though yours will differ), is now part of your project's history. You can view this history using the git log
command, which we will explore soon.
Remember, git commit
only records the changes that have been staged using git add
. Any modifications you made to files in your working directory after the last git add
will not be included in the commit unless you stage them first. This two-step process (staging then committing) gives you precise control over exactly what goes into each snapshot of your project history.
© 2025 ApX Machine Learning