Skip to main content

Dev tools and workflow · Beginner · 16 min

How to Make Your First Git Commits and Read the History

By Padding · Updated 15 Nov 2026
Ubuntu 26.04.1 LTS · Git 2.53 (Ubuntu package)

You will turn a folder into a Git repository, make commits in small steps, check what changed before you commit, ignore files that do not belong in history, rename and delete files, and read the history back in several ways.

On this page

A Git commit is a saved snapshot of your project together with a message that says why you saved it. A sequence of small, well-described commits is the history of the project: you can read it, compare any two points in it, and go back to one of them.

This tutorial makes a small repository from nothing and builds up its history. You decide which changes go into each commit, check them before you save, and then look at the history from a few angles. It assumes the setup from the first tutorial: Git 2.53, your identity, and main as the default branch name. Everything happens in a throwaway folder, ~/git-demo/notes.

Step 1 · Create a repository

Make a folder with a first file in it, then run git init:

Bash
mkdir -p ~/git-demo/notes
cd ~/git-demo/notes
git init
cat > todo.txt <<'TXT'
Buy milk
Write the report
TXT

git init creates a hidden .git folder. That folder is the repository: every commit, branch and setting lives in it. The files around it are your working tree, and Git leaves them alone until you tell it otherwise. Delete .git and the folder is an ordinary folder again, so never run git init in a folder like your home directory.

Check it worked

Bash
git status

The output starts with On branch main and No commits yet. Under Untracked files: it lists todo.txt. Untracked means the file exists in the folder but Git has never been told to record it.

Step 2 · Stage a file and make the first commit

A commit is made in two moves. First you stage the changes you want, which puts them in the index, a waiting area. Then you commit what is staged. The two moves let you choose exactly what each commit contains.

Bash
git add todo.txt
git status

git status now lists todo.txt under Changes to be committed: as new file. Commit it with a message that says what the commit does:

Bash
git commit -m "Add the first to-do list"

Git prints the branch and a short hash, such as [main (root-commit) 3f2a1c9] Add the first to-do list, then a summary: 1 file changed, 2 insertions(+). root-commit appears because this commit has no parent. Your hash will differ. A hash is a name Git computes from the commit's content, and every commit has a unique one.

Write messages for the reader of the history. A first line of about fifty characters in the imperative ("Add", "Fix", "Remove") reads well in lists. Without -m, Git opens your editor for a longer message, with the subject on the first line, a blank line, then the details.

Check it worked

Bash
git status
git log

git status says nothing to commit, working tree clean. git log shows one commit with its full 40-character hash, your name and email on the Author: line, the date, and the message.

Step 3 · See what changed before you commit

Edit the file and add a second one:

Bash
cat >> todo.txt <<'TXT'
Call the dentist
TXT
cat > shopping.txt <<'TXT'
Milk
Bread
TXT
git status --short

The short status uses two columns. M means modified and not staged, and ?? means untracked. Ask for the difference with git diff:

Bash
git diff

git diff compares your working tree with the index and shows the added line with a + in front of it. It reports only files Git already tracks, so shopping.txt does not appear. Stage todo.txt, and the same change moves from "not staged" to "staged":

Bash
git add todo.txt
git diff
git diff --staged

The first git diff prints nothing now, because the working tree and the index agree. git diff --staged shows the change that the next commit will contain. Make a habit of running it before every commit: it is the last chance to notice a stray debug line or a file you did not mean to include.

Bash
git commit -m "Add the dentist call to the to-do list"

Check it worked

Bash
git status --short

The output is ?? shopping.txt. The to-do change is committed, and the shopping list is still untracked because you did not stage it. Staging by file is how you split unrelated work into separate commits.

Step 4 · Stage and commit another file

Stage the shopping list and commit it on its own. You can stage several files in one command, or a whole folder, but name files while you are learning, so you know what goes in:

Bash
git add shopping.txt
git commit -m "Add a shopping list"

git commit -a stages every change to files Git already tracks before it commits. It does not pick up new files, so a new file still needs git add first. Use it when you are sure that every modified file belongs in the commit:

Bash
echo "Eggs" >> shopping.txt
git commit -a -m "Add eggs to the shopping list"

Check it worked

git log --oneline shows one line per commit, with the short hash and the message, newest first:

Bash
git log --oneline

There are four commits. The oldest, at the bottom, is "Add the first to-do list".

Step 5 · Ignore files that do not belong in history

Some files should never be committed: build output, editor backups, dependencies, and anything with passwords in it. A .gitignore file lists patterns that Git skips. Make a scratch file and a log file, and see how Git reacts:

Bash
echo "temporary" > scratch.tmp
echo "started" > app.log
git status --short

Both appear as ??. Create a .gitignore that names them by pattern, and commit it, so everyone who shares the repository gets the same rules:

Bash
cat > .gitignore <<'TXT'
*.log
*.tmp
TXT
git status --short
git add .gitignore
git commit -m "Ignore log and temporary files"

Now only .gitignore showed up as new, and the two ignored files have gone from the status. A pattern only affects files that Git does not already track. If you committed a file by mistake and add it to .gitignore afterwards, Git keeps tracking it. Stop tracking it, and keep the copy on disk, with git rm --cached <file>. A file that held a secret and has already been committed stays in the history after that, so read keep secrets out of Git before you push anything.

Check it worked

Bash
git status --short
git check-ignore -v app.log

The first command prints nothing. git check-ignore -v says which rule matched: .gitignore:1:*.log followed by the file name.

Step 6 · Rename and delete files

Tell Git about a rename with git mv, which moves the file and stages the move in one go. git rm deletes a file and stages the deletion:

Bash
git mv todo.txt tasks.txt
git rm shopping.txt
git status --short

The status shows R todo.txt -> tasks.txt and D shopping.txt. Git does not store renames as such. It notices that the content is the same under a different name. If you had renamed the file in your file manager instead, git add of the new name and the old name would give the same result. Commit both changes:

Bash
git commit -m "Rename the to-do list and drop the shopping list"

Check it worked

Bash
git log --stat -1

git log --stat adds the files each commit touched. The latest commit lists todo.txt => tasks.txt with no lines added or removed, because the content is identical, and shopping.txt with its lines deleted.

Step 7 · Read the history

You have already used git log and git log --oneline. A few options answer most questions:

Bash
git log --oneline --graph --decorate
git log --oneline -- tasks.txt
git log --author="Your Name" --oneline
git log --since="1 day ago" --oneline
  • --graph --decorate draws the line of commits and labels the one that HEAD and main point at. With a single branch it is a straight line. It becomes useful in the next tutorial.
  • -- tasks.txt limits the list to commits that changed that path. The two dashes separate options from paths.
  • --author and --since filter by who and when. Put your own name in --author.

To see one commit in full, give git show a hash from your log. HEAD always means the latest commit, and HEAD~1 the one before it:

Bash
git show HEAD~1 --stat
git show HEAD~3:todo.txt

The first command shows the commit before the latest, with its files. The second prints a file as it was three commits ago, using the form <commit>:<path>. Nothing in your working folder changes.

To see which commit last changed each line of a file, use git blame:

Bash
git blame tasks.txt

Every line begins with the short hash of the commit that last changed it, then the author, the date and the line.

Check it worked

Bash
git log --oneline | wc -l
git log -1 --format='%s'

The first prints 6, one line per commit. The second prints the subject of the latest commit: Rename the to-do list and drop the shopping list.

Step 8 · Compare two points in history

git diff takes two commits and shows how to get from one to the other:

Bash
git diff HEAD~3 HEAD --stat
git diff HEAD~3 HEAD -- .gitignore

The first command summarises the files that differ between three commits ago and now, and shows the renamed file as todo.txt => tasks.txt. The second shows the line-by-line difference for one path, here the new .gitignore. Both only read the repository.

Troubleshooting

  • fatal: not a git repository (or any of the parent directories): .git means you ran the command outside the repository. Run cd ~/git-demo/notes and try again.
  • Author identity unknown or Please tell me who you are means user.name and user.email are not set. Set them as in the first tutorial and run the commit again.
  • nothing added to commit but untracked files present means you ran git commit without staging the new file. Run git add <file> first.
  • A file is in .gitignore but still shows up as modified, because it was committed earlier. Run git rm --cached <file> and commit.

Step 9 · Clean up

Leave the repository where it is for the next tutorial, which builds on it. If you are stopping here, delete the demo folder:

Bash
rm -rf ~/git-demo

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.