This section covers the most fundamental lifecycle of a Git repository: creating a repo, making changes, saving them to history, and checking your status.
Before you can add or commit files, you need a repository. This command initializes a brand new, empty Git repository in your current folder. It creates a hidden .git directory that will track all your changes.
# Create a new folder for your project
mkdir my-project
cd my-project
# Turn this folder into a Git repository
git initAfter this, you can start creating files.
Before using the commands, it's crucial to understand the three "areas" Git uses to manage your files:
- Working Directory: The actual folder and files on your computer that you can see and edit.
- Staging Area (or "Index"): A "waiting room" or "drafting" area. This is where you place changes you want to include in your next "snapshot" (commit).
- Repository (.git directory): The permanent history. This is where Git stores all your "snapshots" (commits).
The commands git add and git commit move your changes between these areas.
Let's walk through the full cycle after you have run git init.
First, create a new file in your repository folder.
# You can use a text editor like nano
nano content.txt
# (Inside nano, type "Hello World", then Ctrl+O to save, Ctrl+X to exit)
# Or, for a quick example, use 'echo'
echo "This is my first file" > content.txtNow, ask Git what it thinks about this new file.
$ git status
#On branch main
#No commits yet
#Untracked files:
# (use "git add <file>..." to include in what will be committed)
# content.txt
#nothing added to commit but untracked files present (use "git add" to track)What does "Untracked files" mean? This means Git sees a new file in your Working Directory, but it is not tracking its history. It's not in the Staging Area and it's not in your Repository history. Git is ignoring it until you explicitly tell it to track the file.
Now we tell Git, "I want to include this file in my next snapshot." We use git add to move the file from the Working Directory to the Staging Area.
# Stage a specific file
git add content.txtWhat's the difference?
git add <path>(e.g.,git add content.txt): Stages only the specified file or directory. This is precise and safe.git add .: Stages all new and modified files in your current directory and all subdirectories. This is a common and powerful shortcut, but use it with care to avoid adding files you don't want (like password files or build artifacts).
Let's check git status again. Notice the message is different.
$ git status
#On branch main
#No commits yet
#Changes to be committed:
# (use "git rm --cached <file>..." to unstage)
# new file: content.txtWhat does "Changes to be committed" mean? This confirms that
content.txtis now in the Staging Area. It is "staged" and ready to be permanently saved. Thegit rm --cachedhint tells you how to "unstage" it (move it out of the staging area) if you made a mistake.
A commit is a snapshot of your Staging Area at a specific point in time. It's how Git saves history. Each commit is a permanent record that builds on the last one, creating a chain of changes.
We use git commit and the -m flag to provide a short, descriptive message.
git commit -m "A: content added"
- What is a commit? It's a permanent save point in your project's history.
- What does
-mmean? It stands for "message". Every commit must have a message. This message explains why you made this change. Good, clear messages are crucial.
Finally, let's check our status one last time.
$ git status
#On branch main
#nothing to commit, working tree cleanWhat does "working tree clean" mean? This is the perfect state. It means your Working Directory, Staging Area, and the Repository's last commit are all in sync. You have no pending or unstaged changes.
Now that you have a commit, you can view your project's history.
The default git log command is very detailed.
$ git log
#commit 3a3cbb8b64f3d17983693e50b731003b5736184a (HEAD -> main)
#Author: Your Name <your@email.com>
#Date: Fri Nov 14 17:40:00 2025 +0100
# A: content addedA much more useful view is git log --oneline.
$ git log --oneline
#3a3cbb8 (HEAD -> main) A: content addedWhat does this mean?
3a3cbb8: This is the commit hash, a unique 7-character ID for that snapshot.(HEAD -> main):HEADis a pointer showing what commit you are currently on.mainis the name of the branch you are on.A: content added: This is the commit message you wrote.
Here is a very useful log command and a breakdown of its parameters:
git --no-pager log -n 10 --oneline --parents --graphgit --no-pager log: The--no-pagerpart tells Git to print the log directly to your terminal, instead of opening an interactive window (called a "pager").-n 10: (short for--max-count=10) Shows only the last 10 commits.--oneline: Shows the compact, one-line view (hash + message).--parents: Shows the parent commit(s) for each commit.--graph: Draws a small ASCII art graph showing how the branches connect (very useful in complex projects).
This entire workflow boils down to three essential commands you will use constantly (plus git init once at the start):
git status- Question it answers: "Where am I? What's the state of my project?"
git add [file]orgit add .- Action it performs: "Prepare this change to be saved." (Moves changes from Working Directory to Staging Area).
git commit -m "Your message"- Action it performs: "Save this snapshot permanently." (Moves changes from Staging Area to the Repository History).