Git Basics: Create, Commit & Track Changes
Welcome to Part 2 of the Git & GitHub Tutorial Series by CodeHarborHub. Now that you understand what Git is, letโs dive into practical commands that help you start tracking and managing your project history.
1. Create a New Repositoryโ
A repository (repo) is like a project folder where Git tracks your files and changes.
Initialize a new repositoryโ
git init
This creates a hidden folder
.git/inside your project directory that stores all Git data.
You only need to run this once per project.
2. Clone an Existing Repositoryโ
If you want to work on an existing project hosted on GitHub or another server:
git clone https://github.com/username/repo-name.git
This command downloads the entire project and its history.
Cloning = downloading the repo + linking it to the remote GitHub repository.
3. Check Repository Statusโ
At any time, see which files have been changed or staged:
git status
- ๐ข Green โ files ready to commit (staged)
- ๐ด Red โ files modified but not yet staged
4. Add Changes to Staging Areaโ
Before committing, you must stage the files you want to include:
git add filename
Or, to add all modified files:
git add .
Think of staging as preparing your changes for a โsnapshotโ.
5. Commit Changesโ
Once staged, you can commit your changes โ a permanent snapshot in history.
git commit -m "Add new feature or fix bug"
- Every commit has a message describing what changed.
- Use clear, meaningful commit messages.
Example:
git commit -m "Add homepage UI and update styles"
6. View Commit Historyโ
To see your commit history:
git log
This shows:
- Commit ID (unique hash)
- Author name & email
- Date & time
- Commit message
For a one-line summary:
git log --oneline
Each commit acts like a โsave pointโ in your project timeline.
7. Ignore Unwanted Filesโ
Use a .gitignore file to tell Git which files or folders to skip.
Example .gitignore:
node_modules/
.env
.DS_Store
dist/
This keeps your repository clean and free from temporary or sensitive files.
8. Remove or Rename Filesโ
To delete a tracked file:
git rm filename
To rename a file:
git mv oldname newname
After renaming or deleting, donโt forget to commit your changes.
9. See File Differencesโ
Check what has changed before committing:
git diff
After staging changes:
git diff --staged
Use this to review your modifications before finalizing them.
10. Typical Workflow Exampleโ
Hereโs a simple end-to-end Git flow:
# Step 1: Initialize Git
git init
# Step 2: Check repo status
git status
# Step 3: Add your files
git add .
# Step 4: Commit your changes
git commit -m "Initial project setup"
# Step 5: View history
git log --oneline
Done โ your local Git repository is now tracking your project!
11. Understanding Git Statesโ
| State | Description |
|---|---|
| Untracked | File not yet tracked by Git. |
| Modified | File changed after the last commit. |
| Staged | File marked to be included in next commit. |
| Committed | Changes saved in local repository. |
Visual representation:
Working Directory โ git add โ Staging Area โ git commit โ Repository
Summaryโ
| Command | Description |
|---|---|
git init | Start a new Git repository |
git clone | Copy an existing repository |
git status | View current file states |
git add | Stage changes for commit |
git commit -m | Save snapshot with message |
git log | Show commit history |
git diff | Compare changes between versions |
.gitignore | Ignore unwanted files |
Next Upโ
Now that youโve mastered the basics, itโs time to explore branching and merging โ the power that makes Git truly flexible. ๐ Next: Branching & Merging โ
Additional Resourcesโ
๐ This lesson is part of CodeHarborHubโs Git & GitHub series โ empowering you to code, commit, and collaborate effectively.