Programming and IT

git commit

A commit is a saved state of your project with an author, a timestamp and a message. git commit takes what you selected beforehand with git add and writes it into history as one indivisible piece.

Updated
In this article

A commit takes the index, not the folder#

Between your files and the history there is an intermediate layer — the index, also called the staging area. git add copies the current contents of a file there, and git commit writes into history exactly what is in the index at the moment you run it. This is where beginners get surprised: if you edit a file again after git add, the commit gets the earlier version, and the new one stays in the working directory. git status shows both lists separately — "Changes to be committed" and "Changes not staged for commit".

So the order is: look at git status, select what you need with git add, check with git diff --staged, and only then commit.

Syntax#

git add README.md app.py
git commit -m "Add first version of argument parsing"
Option What it does
-m "text" the message right in the command, no editor
-a take every change to already tracked files, skipping git add
--amend replace the last commit with a new one instead of adding another
--no-edit with --amend, keep the previous message
--allow-empty make a commit with no changes (sometimes needed to trigger checks)
-v show the diff itself in the editor — handy for a last read-through

-a does not pick up new files: anything Git does not know about yet has to be added by hand. Write messages in the imperative mood and to the point — a first line of about 50 characters, details after a blank line.

Example: the first commit in a new repository#

git init
git add README.md
git commit -m "Describe the project"
[main (root-commit) 4f1c2ab] Describe the project
 1 file changed, 12 insertions(+)
 create mode 100644 README.md

In the brackets are the branch and the short hash. The very first commit is marked root-commit: it has no parent.

Example: fixing the commit you just made#

Forgot a file or made a typo in the message? You do not need another commit called "fix typo":

git add tests/test_args.py
git commit --amend --no-edit
[main 9d31c07] Add first version of argument parsing
 Date: Sat Sep 20 11:04:18 2026 +0300
 2 files changed, 41 insertions(+)

The hash has changed — this is a different commit, and the old one is simply no longer needed. Hence the rule: --amend is safe as long as the commit has not been pushed. What to do if it already has is covered in the article on git push.

What Git complains about#

nothing to commit, working tree clean — the index is empty and there is nothing to change. Usually you forgot git add.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	notes.txt

nothing added to commit but untracked files present

The file is new and Git does not track it. Either add it, or put a pattern into .gitignore if it ended up in the folder by accident.

Please tell me who you are — your name and email are not set. You fix this once per machine:

git config --global user.name "Jane Doe"
git config --global user.email "jane@example.com"

Aborting commit due to empty commit message — you closed the editor without writing anything. Git deliberately refuses to record a commit without a message.

How to undo a commit#

It all depends on whether the commit has reached the server.

git reset --soft HEAD~1

This removes the last commit but keeps the changes in the index — you can commit again right away. --mixed (the default) puts the edits back into the working directory, and --hard erases them together with the commit; it is the only really dangerous one of the three.

git revert 9d31c07

This creates a new commit that cancels the given one. It does not rewrite history, so it is the right tool for something your teammates have already seen. All the options are compared in how to undo a commit.

If you are lost and it feels like work is gone, look at git reflog: it lists every state of HEAD over the last few weeks, and almost any "missing" commit can be found there by its hash.

Step-by-step plan

  1. See what changedgit status and git diff: first understand what the commit should be made of.
  2. Stage your selectiongit add file by file; check the selection with git diff --staged.
  3. Record with a messagegit commit -m with a meaningful first line of up to 50 characters.
  4. Check the historygit log --oneline -3: the commit is there, the branch has moved.
  5. Fix it if neededBefore pushing — git commit --amend; after pushing — git revert.

Start learning this in your own space

The plan goes into your repository: tick off stages, keep notes — the change history shows how far you have come.

Start the plan

Check yourself

1.Which command commits changes to all tracked files without a separate git add?

2.A commit is made but not pushed; you want to remove it and keep the edits in your files. What do you use?

3.What does git commit --amend do?

Sources

Was this helpful?

More articles

Programming and IT How to undo a commit The question "how do I undo a commit" actually hides four different ones: fix the last commit, take it back and redo it, erase several, or cancel an old commit in the middle of history. The answer depends above all on one thing — whether anyone besides you has seen that commit. Programming and IT git log git log shows history: which commits brought the repository to its current state. Without options the output is wordy and nearly useless, but a few flags turn it into a search tool — by author, by date, by file and even by a line of code that has disappeared. Programming and IT git merge Merging brings work from one branch into another. git merge takes the branch you name and joins its history with the current one — sometimes by just moving a pointer, sometimes by creating a separate commit, and sometimes by stopping and asking for help. Programming and IT Git branches A Git branch is a movable pointer to a commit, not a copy of your files. That is why creating one costs nothing, and the git branch command is what you use to list, create, rename and delete branches. Programming and IT git checkout Historically `git checkout` does several unrelated things at once: it moves your working copy to another branch, creates a branch and pulls a single file out of a commit. Because it was so overloaded, Git 2.23 added two separate commands — `switch` and `restore`. Here are both sides: the old way and its modern replacement. Programming and IT git clone git clone takes an entire repository from a server — with all its history, branches and tags — and puts a copy into a new folder on your computer. It is how you start working on any existing project, yours or someone else's.

More solutions