Skip to main content

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.

tip

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โ€‹

StateDescription
UntrackedFile not yet tracked by Git.
ModifiedFile changed after the last commit.
StagedFile marked to be included in next commit.
CommittedChanges saved in local repository.

Visual representation:

Working Directory โ†’ git add โ†’ Staging Area โ†’ git commit โ†’ Repository

Summaryโ€‹

CommandDescription
git initStart a new Git repository
git cloneCopy an existing repository
git statusView current file states
git addStage changes for commit
git commit -mSave snapshot with message
git logShow commit history
git diffCompare changes between versions
.gitignoreIgnore 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.