What is git commit used for?
git commit is used to save staged changes (the staging area) into the repository's history.
A commit is a fixed point in the project's history with a description of what and why was changed.
What git commit does
- Creates a snapshot of the project's state
- Saves a commit message
- Generates a unique hash
- Adds an entry to the Git history
Important: git commit records only what has been added via git add.
Basic usage
bash
git commit -m "Add user authentication"Typical workflow
bash
git status # check changes
git add . # stage changes
git commit -m "Initial commit"Useful variants
Commit with the editor open
bash
git commitAdd changes and commit at once
bash
git commit -am "Fix bug"Works only for tracked files
Amend the last commit
bash
git commit --amendWhat is stored in a commit
- the list of changes
- author and date
- the message
- a reference to the previous commit
Frequent mistakes
- Making meaningless commits:
fix,update - Large "everything at once" commits
- Forgetting
git addbeforegit commit
Short version for an interview
git commitsaves staged changes into the repository's history, creating a logical fixation point with a description of the changes.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.