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:
mkdir -p ~/git-demo/notes
cd ~/git-demo/notes
git init
cat > todo.txt <<'TXT'
Buy milk
Write the report
TXTgit 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
git statusThe 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.
git add todo.txt
git statusgit status now lists todo.txt under Changes to be committed: as new file. Commit it with a message that says what the commit does:
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
git status
git loggit 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:
cat >> todo.txt <<'TXT'
Call the dentist
TXT
cat > shopping.txt <<'TXT'
Milk
Bread
TXT
git status --shortThe short status uses two columns. M means modified and not staged, and ?? means untracked. Ask for the difference with git diff:
git diffgit 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":
git add todo.txt
git diff
git diff --stagedThe 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.
git commit -m "Add the dentist call to the to-do list"Check it worked
git status --shortThe 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:
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:
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:
git log --onelineThere 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:
echo "temporary" > scratch.tmp
echo "started" > app.log
git status --shortBoth appear as ??. Create a .gitignore that names them by pattern, and commit it, so everyone who shares the repository gets the same rules:
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
git status --short
git check-ignore -v app.logThe 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:
git mv todo.txt tasks.txt
git rm shopping.txt
git status --shortThe 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:
git commit -m "Rename the to-do list and drop the shopping list"Check it worked
git log --stat -1git 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:
git log --oneline --graph --decorate
git log --oneline -- tasks.txt
git log --author="Your Name" --oneline
git log --since="1 day ago" --oneline--graph --decoratedraws the line of commits and labels the one thatHEADandmainpoint at. With a single branch it is a straight line. It becomes useful in the next tutorial.-- tasks.txtlimits the list to commits that changed that path. The two dashes separate options from paths.--authorand--sincefilter 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:
git show HEAD~1 --stat
git show HEAD~3:todo.txtThe 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:
git blame tasks.txtEvery line begins with the short hash of the commit that last changed it, then the author, the date and the line.
Check it worked
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:
git diff HEAD~3 HEAD --stat
git diff HEAD~3 HEAD -- .gitignoreThe 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): .gitmeans you ran the command outside the repository. Runcd ~/git-demo/notesand try again.Author identity unknownorPlease tell me who you aremeansuser.nameanduser.emailare not set. Set them as in the first tutorial and run the commit again.nothing added to commit but untracked files presentmeans you rangit commitwithout staging the new file. Rungit add <file>first.- A file is in
.gitignorebut still shows up as modified, because it was committed earlier. Rungit 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:
rm -rf ~/git-demoNext steps
- Work on separate lines of change: create, switch and merge branches in Git.
- Fix what you got wrong: undo mistakes in Git.
- Share the repository: push to GitHub over SSH and open a pull request.
Related tutorials
- How to Install and Configure GitYou will install Git 2.53 from Ubuntu 26.04's own packages, set your name and email, make main the default branch, choose an editor and a few safe defaults, use a different email for work projects, and create an SSH key to use with GitHub.
- How to Keep Passwords and API Keys Out of GitYou will keep secrets in a git-ignored .env file, commit a safe template instead, stop a commit that contains a key with a pre-commit hook, and learn what to do when a secret has already been committed.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials