Sub topic 9
Making Your First Commit
This chapter assumes you have Git installed and git --version works. It also assumes you have set user.name, user.email, and init.defaultBranch with git config --global. You should be comfortable running shell commands and moving between folders.
By the end, you will have created a repository, staged a file, committed it, and read the history. Along the way you will see how Git's three areas map to real commands.
The three areas in practice
Git tracks your files in three places:
- The working tree holds the files you see and edit in your project folder.
- The staging area (also called the index) holds the changes you have chosen to include in your next commit.
- The repository holds the committed snapshots, stored in a hidden
.gitfolder.
A commit is a snapshot of everything in the staging area at the moment you commit. Git does not store a list of edits. It records what the project looked like, and reuses unchanged data from earlier snapshots to save space.
Create a repository
A repository is a folder that Git is tracking. The command git init turns a folder into one by creating the .git directory inside it.
mkdir demo && cd demo
git initGit prints a message that it initialized an empty repository. Because you set init.defaultBranch to main, the first branch is named main. A branch is a movable label that points to a commit.
Do not delete the .git folder. It holds the full history. If you remove it, the folder is no longer a repository.
Check the status
Create a file and ask Git what it sees.
echo "Hello, Git" > hello.txt
git statusThe output lists hello.txt under "Untracked files". An untracked file is one that exists in the working tree but that Git has never been told to record. Git also reports "No commits yet", which confirms the history is empty.
Run git status often. It tells you which area each change is in and usually suggests the next command.
Stage the file
Staging selects what goes into the next commit.
git add hello.txt
git statusNow hello.txt appears under "Changes to be committed" as a new file. It is in the staging area but not yet in the repository.
Staging exists so you can build a commit deliberately. If you edit five files but only two belong to one logical change, you stage those two and commit them together. The other three stay in the working tree for a later commit.
Commit
git commit -m "Add hello.txt"The -m option supplies the commit message on the command line. Without it, Git opens your configured text editor and waits for you to write one.
Git records the snapshot with your name and email, the current date, and the message. It prints a short summary, including a short form of the commit's ID. That ID is a hash, a long string generated from the commit's contents.
Read the history
git log --onelineYou will see one line: an abbreviated hash followed by your message. The hash on your machine will differ from anyone else's, because it depends on your name, the timestamp, and the contents.
Run git status again. It now reports "nothing to commit, working tree clean", meaning all three areas agree.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `Author identity unknown` or `Please tell me who you are` | `user.name` and `user.email` are not set | Run the two `git config --global` commands |
| `fatal: not a git repository` | You are outside a repository folder | `cd` into the project or run `git init` |
| `nothing added to commit` | Nothing was staged | Run `git add <file>` first |
| `pathspec 'file' did not match any files` | Typo in the filename, or wrong folder | Check the name with `ls` or `dir` |
| Editor opens and seems stuck | You left out `-m` | Write a message, save, and close the editor |
Edit and commit again
A second commit shows the cycle clearly. Change the file and look at the status.
echo "A second line" >> hello.txt
git status
git add hello.txt
git commit -m "Add a second line to hello.txt"
git log --onelineBefore git add, the file appears under "Changes not staged for commit". It is now tracked, so Git sees it as modified rather than untracked. The log now lists two commits, newest first.
Exercise
You edit a.txt and b.txt, then run git add a.txt and git commit -m "Update a". Which file's changes are in the new commit, and where do the changes to the other file remain?
Show answer
Only the changes to a.txt are in the commit, because only that file was staged. The changes to b.txt remain in the working tree as unstaged modifications, and git status will still list them.
Exercise
What command shows the history as one line per commit, and what does git status print when everything has been committed?
Show answer
git log --oneline shows one line per commit. git status prints "nothing to commit, working tree clean".