A commit is a saved snapshot of your project, with an author, a date, a message and a link to the commit before it. Together these links form the history.
Stage, then commit
First choose what goes into the snapshot with git add, then record it:
$ git add index.html style.css
$ git commit -m "Add home page"
[main 4e1c2a9] Add home page
2 files changed, 18 insertions(+)
create mode 100644 index.html
create mode 100644 style.cssOnly staged changes are included. This lets you edit five files and commit the two that belong together.
git add . stages the changes under the current directory, including new files. Run git status first so you do not pick up something by accident.
Write a useful message
Without -m, Git opens a text editor. Write a short summary line, leave a blank line, then explain why the change was needed. A summary such as "Fix login redirect loop" is far more helpful later than "changes".
A shortcut with a catch
git commit -a -m "message" stages modified and deleted tracked files and commits them in one step. It does not include untracked files, so a brand new file still needs git add.
Read the history
git log lists commits, newest first:
$ git log --oneline
4e1c2a9 (HEAD -> main) Add home page
a07b3f5 Initial commitThe short code is the start of the commit's hash, which identifies it. HEAD marks where you are now. Use git show 4e1c2a9 to see one commit with its changes. Your hashes will be different.
Fix the last commit
If you forgot a file or mistyped the message, stage what is missing and run:
$ git commit --amend -m "Add home page and styles"Amending replaces the last commit with a new one that has a different hash. Avoid it on commits that other people already have, because their history will no longer match yours.
A commit is local. Nothing reaches a server until you push it. More options are in the git commit documentation.
Try it
Make two separate edits in two files. Commit them as two commits with clear messages, then check the result with git log --oneline and git show HEAD.
Check your understanding
Git Commits
All 3 sections · no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.