Labs ICT
⭐ Pro Login

Learn Git

5 min read | Git Tutorial
⭐

Want the full learning experience?

Get structured courses, certificates, projects, and instructor support with LabsICT Pro.

Explore Pro Courses

Git is the most important tool in software development. You can think of Git as a superpower that gives you the ability to create multiple versions of your code, easily collaborate with others, and rollback changes with a simple command. I remember the first time I used Git—everything changed.

What is Git and Why It Matters

Git is a distributed version control system that lets you track changes in your code over time. Unlike centralized version control like SVN, where you depend on one main server, Git works across multiple computers—so you can code anywhere and still track your progress. It tracks every file change, including deletions and renames, so you'll never lose a version of your code.

$ git init

The git init command creates a new Git repository in your current directory. This creates a hidden .git folder that contains all the metadata Git needs to track your project. From this moment, Git starts watching your files.

Configuring Git

Before you start using Git, set up your identity. This information is attached to every commit you make, so others know who made each change:

git config --global user.name "Your Name"
git config --global user.email "your@email.com"

The --global flag applies this setting to all repositories on your machine. If you want different identities for different projects, omit --global and run the commands inside each project folder. You can also set your default branch name:

git config --global init.defaultBranch main

Tracking Files with Git

Git uses a three-area system to manage your files: the working directory (your actual files), the staging area (a preview of what you'll save), and the repository (the permanent history). Here's how you move files through this pipeline:

# Check which files Git knows about
git status

# Add a specific file to the staging area
git add index.html

# Add all changed files at once
git add .

# Remove a file from staging (keeps the file on disk)
git reset HEAD filename

Always run git status before committing. It shows you which files are staged, which are modified but not staged, and which are untracked. This habit prevents accidental commits of the wrong files.

Making Commits

A commit is a snapshot of your staged changes at a specific point in time. Write clear, descriptive commit messages that explain what changed and why:

# Commit with a message
git commit -m "Add user authentication system"

# Commit all tracked changes (skip staging)
git commit -am "Fix login form validation"

Good commit messages follow a pattern: a short summary under 50 characters, followed by a blank line, then a detailed explanation if needed. For example:

git commit -m "Add password reset functionality

- Add forgot password page with email input
- Create token generation and email sending logic
- Add password reset form with validation
- Implement backend route for password update"

Avoid vague messages like "fix bug" or "update code". Future you (and your teammates) will thank you for clear commit messages when reviewing history months later.

Viewing History

Git keeps a complete history of every change made to your project. You can browse this history to understand how your code evolved:

# View commit history
git log

# Compact one-line format
git log --oneline

# See changes in each commit
git log --stat

# View the actual code changes (diff)
git log -p

# See history of a specific file
git log -- filename.js

The git diff command shows you exactly what changed between versions. Use it before committing to review your own changes, or compare branches to see what's different:

# Unstaged changes
git diff

# Staged changes
git diff --staged

# Compare two branches
git diff main..feature-branch

Working with Branches

Branches let you work on multiple features or fixes simultaneously without affecting the main codebase. Each branch is an independent line of development:

# Create a new branch
git branch feature-login

# Switch to that branch
git checkout feature-login

# Create and switch in one command
git checkout -b feature-signup

# List all branches
git branch -a

When you're done with a branch, merge it back into main or delete it:

# Switch to the branch you want to merge into
git checkout main

# Merge the feature branch
git merge feature-login

# Delete the merged branch
git branch -d feature-login

If there are conflicting changes in the same file on both branches, Git will pause and ask you to resolve the conflict manually. Open the file, look for the conflict markers (<<<<<<<), choose the correct version, and then commit the resolution.

Working with Remote Repositories

A remote repository is a version of your project hosted on a server like GitHub, GitLab, or Bitbucket. It allows you to share code with others and back up your work:

# Clone an existing repository
git clone https://github.com/user/repo.git

# Add a remote
git remote add origin https://github.com/user/repo.git

# Push your changes to the remote
git push origin main

# Pull the latest changes from remote
git pull origin main

The git fetch command downloads changes from the remote without merging them, letting you review before integrating. git pull is essentially git fetch followed by git merge.

🧪 Quick Quiz

What is a Git repository?