📌Git Basics — init, add, commit, push
Your first commits — hands-on practice from zero
Git Basics — init, add, commit, push
Welcome to your first hands-on Git lesson. By the end of this tutorial, you will have created a real repository, made commits, and pushed your code to GitHub. Every step includes the exact command to run and what to expect.
Why Git Matters
Git is the version control system used by virtually every software team in the world. It tracks every change you make, lets you undo mistakes, collaborate with others, and maintain a complete history of your project. Without Git, you are flying blind.
Step 1 — Create a Project Folder
Open your terminal and create a new folder for your project:
mkdir my-first-project
cd my-first-projectYou now have an empty folder. Git does not know about it yet.
Step 2 — Initialize a Git Repository
Run the following command inside your project folder:
git initWhat happens: Git creates a hidden .git folder inside your project. This folder contains the entire history database. You never need to touch it directly.
Expected output:
Initialized empty Git repository in /path/to/my-first-project/.git/
Step 3 — Check the Status
git statusUnderstanding the output:
On branch main
No commits yet
nothing to commit (create/copy files and use "git add" to track)
This tells you three things:
- You are on the
mainbranch (the default branch). - There are no commits yet — the history is empty.
- There are no files to track — the folder is empty.
Key insight: git status is your best friend. Run it constantly. It always tells you exactly where you stand.
Step 4 — Create Your First File
echo "# My First Project" > README.mdNow run git status again:
On branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present
Untracked means Git sees the file but is not watching it yet. You must explicitly tell Git to track it.
Step 5 — The Staging Area (git add)
This is the most misunderstood concept for beginners. Git has a staging area (also called the index). Think of it as a loading dock:
Working Directory → Staging Area → Repository
(your files) (ready to ship) (permanent history)
To move a file to the staging area:
git add README.mdNow check the status:
git statusOn branch main
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: README.md
The file is now staged — it is ready to be committed. The green text (in your terminal) confirms this.
Adding Multiple Files
git add file1.txt file2.txt # Add specific files
git add . # Add everything in current directory
git add *.js # Add all JavaScript filesWarning: git add . adds everything. Be careful — you might accidentally stage files you do not want (passwords, large binaries, etc.). That is why .gitignore exists (covered below).
Step 6 — See What Changed (git diff)
Before committing, you can see exactly what changed:
git diff # Shows unstaged changes
git diff --staged # Shows staged changes (what will be committed)Example output:
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..4b5fa63
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# My First ProjectLines starting with + are additions. Lines starting with - are deletions. This is how you review your work before saving it permanently.
Step 7 — Make Your First Commit
git commit -m "Initial commit: add README"What happens: Git takes a snapshot of everything in the staging area and saves it permanently with your message, your name, a timestamp, and a unique ID (hash).
Expected output:
[main (root-commit) a1b2c3d] Initial commit: add README
1 file changed, 1 insertion(+)
create mode 100644 README.md
Commit message best practices:
- Use present tense: "Add feature" not "Added feature"
- Be specific: "Fix login timeout on slow networks" not "Fix bug"
- Keep the first line under 72 characters
- If you need more detail, leave a blank line then write a paragraph
Step 8 — View Your History (git log)
git logcommit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0 (HEAD -> main)
Author: Your Name <your@email.com>
Date: Fri Apr 25 2026 10:00:00
Initial commit: add README
Every commit has:
- A hash (the long hex string) — a unique fingerprint
- The author — who made the change
- The date — when
- The message — why
Step 9 — Modify, Stage, Commit Again
Edit your file:
echo "This project demonstrates Git basics." >> README.mdCheck what changed:
git status
git diffStage and commit:
git add README.md
git commit -m "Add project description to README"Step 10 — Compact History View
git log --onelineb2c3d4e Add project description to README
a1b2c3d Initial commit: add README
This is the compact view — one line per commit. Much easier to scan. Other useful variations:
git log --oneline --graph # Shows branch structure visually
git log --oneline -5 # Shows only last 5 commits
git log --oneline --all # Shows all branchesStep 11 — Create a .gitignore File
Some files should never be committed: passwords, API keys, build artifacts, OS files. Create a .gitignore file:
touch .gitignoreAdd patterns to it:
# Dependencies
node_modules/
# Environment variables (NEVER commit these)
.env
.env.local
# Build output
dist/
build/
# OS files
.DS_Store
Thumbs.db
# IDE files
.vscode/
.idea/
# Logs
*.log
Stage and commit:
git add .gitignore
git commit -m "Add .gitignore for common exclusions"Important: .gitignore only prevents untracked files from being added. If you already committed a file and then add it to .gitignore, it will still be tracked. You must remove it first:
git rm --cached .env # Stop tracking but keep the file locally
git commit -m "Remove .env from tracking"Step 12 — Create a GitHub Account
- Go to https://github.com
- Click Sign up and follow the steps
- Verify your email address
- You now have a place to store your code online
On GitHub, click New repository:
- Name:
my-first-project - Keep it public (or private, your choice)
- Do NOT initialize with README (you already have one)
- Click Create repository
GitHub will show you setup instructions. Follow the ones below.
Step 13 — Connect to GitHub (git remote)
git remote add origin https://github.com/YOUR-USERNAME/my-first-project.gitWhat this does: It tells your local Git that there is a remote server called origin at that URL. The name origin is a convention — it means "the main remote."
Verify it worked:
git remote -vorigin https://github.com/YOUR-USERNAME/my-first-project.git (fetch)
origin https://github.com/YOUR-USERNAME/my-first-project.git (push)
Step 14 — Push Your Code
git push -u origin mainBreakdown:
git push— send commits to the remote-u— set upstream tracking (so next time you can just typegit push)origin— the remote namemain— the branch name
After this, refresh your GitHub page. Your files and commit history are now online.
For future pushes, you only need:
git pushStep 15 — Pull Changes
If someone else pushes changes (or you edit on GitHub directly), pull them down:
git pullThis fetches the latest commits from the remote and merges them into your local branch. Always pull before you start working to avoid conflicts.
Step 16 — Clone an Existing Repository
To download someone else's project (or your own on a new computer):
git clone https://github.com/username/repository-name.gitThis creates a new folder with the project name, downloads all files and history, and automatically sets up the remote.
git clone https://github.com/username/repo.git my-folder # Custom folder nameThe Full Workflow Diagram
Here is the complete Git workflow you will use daily:
┌──────────────┐ git add ┌──────────────┐ git commit ┌──────────────┐ git push ┌──────────────┐
│ Working │ ──────────────→ │ Staging │ ───────────────→ │ Local │ ─────────────→ │ Remote │
│ Directory │ │ Area │ │ Repository │ │ Repository │
│ (your edits)│ │ (index) │ │ (.git) │ │ (GitHub) │
└──────────────┘ └──────────────┘ └──────────────┘ └──────────────┘
↑ │
└─────────────────────────────────── git pull ────────────────────────────────────────────────────┘
The daily cycle:
git pull— get latest changes- Edit your files
git add— stage changesgit diff --staged— reviewgit commit -m "message"— save snapshotgit push— share with team
Common Mistakes and How to Fix Them
Mistake 1: Forgot to add a file before committing
# You committed but forgot a file
git add forgotten-file.txt
git commit -m "Add forgotten file"Or, if you want to include it in the previous commit:
git add forgotten-file.txt
git commit --amend --no-edit # Adds to the last commit without changing the messageWarning: Only amend commits that have NOT been pushed yet.
Mistake 2: Wrong commit message
git commit --amend -m "Correct commit message here"Again, only do this before pushing.
Mistake 3: Push rejected (remote has changes you do not have)
! [rejected] main -> main (non-fast-forward)
Fix:
git pull --rebase origin main # Pull and replay your commits on top
git pushMistake 4: Accidentally staged a file
git reset HEAD secret-file.env # Unstage without deletingOr with newer Git:
git restore --staged secret-file.envMistake 5: Want to undo the last commit (keep changes)
git reset --soft HEAD~1 # Undo commit, keep files stagedCommands Cheatsheet
| Command | What It Does |
|---|---|
git init | Initialize a new repository |
git status | Show current state of working directory and staging area |
git add <file> | Stage a file for the next commit |
git add . | Stage all changed files |
git diff | Show unstaged changes |
git diff --staged | Show staged changes |
git commit -m "msg" | Create a commit with a message |
git commit --amend | Modify the last commit |
git log | Show full commit history |
git log --oneline | Show compact commit history |
git remote add origin <url> | Connect to a remote repository |
git push -u origin main | Push and set upstream tracking |
git push | Push commits to remote |
git pull | Fetch and merge remote changes |
git clone <url> | Download a repository |
git rm --cached <file> | Stop tracking a file |
git reset HEAD <file> | Unstage a file |
git restore --staged <file> | Unstage a file (modern syntax) |
git reset --soft HEAD~1 | Undo last commit, keep changes staged |
Practice Exercises
Exercise 1 — Full Cycle
- Create a new folder called
practice-git - Initialize it as a Git repository
- Create a file called
hello.txtwith the text "Hello, Git!" - Stage and commit the file
- Modify the file to add a second line
- Check the diff, then stage and commit
- View the log in oneline format
Exercise 2 — .gitignore
- In the same repository, create a file called
secrets.env - Create a
.gitignorefile that excludes*.envfiles - Run
git status— verifysecrets.envdoes not appear - Stage and commit the
.gitignore
Exercise 3 — Push to GitHub
- Create a new repository on GitHub (do not initialize with README)
- Add the remote to your local repository
- Push your commits
- Verify on GitHub that all files and history are visible
Exercise 4 — Clone and Modify
- Clone your repository to a different folder
- Make a change in the cloned copy
- Commit and push from the clone
- Go back to the original folder and pull the changes
Key Takeaways
- git init creates a repository — do this once per project
- git add moves files to the staging area — you choose what to commit
- git commit saves a permanent snapshot — always write a clear message
- git push shares your work — sends commits to the remote
- git pull stays in sync — always pull before starting work
- git status and git diff are your safety net — use them constantly
- .gitignore protects sensitive files — set it up early
- Never commit secrets, large binaries, or build artifacts
You now have all the fundamentals. Every advanced Git concept (branches, merges, rebases, pull requests) builds on exactly these commands.