Advertisement

Learn Git Basics: Version Control for Beginners

Ever made changes to code, then wished you could go back? Or worked on a project with others and had file conflicts? Git solves these problems. It's version control - a way to track changes and collaborate.

This course teaches Git basics. By the end, you'll be able to track changes, create backups, and work with others. It's essential for any developer.

What Is Git?

Git is a version control system. It tracks changes to files over time. Think of it as a time machine for your code:

  • See what changed and when
  • Go back to previous versions
  • Work on different features simultaneously
  • Collaborate without conflicts

Git vs GitHub: Git is the tool. GitHub is a website that hosts Git repositories (like cloud storage for code).

Installing Git

Windows:

  • Download from git-scm.com
  • Run installer, use default options
  • Git Bash will be installed (command line tool)

Mac:

  • Install Xcode Command Line Tools: xcode-select --install
  • Or download from git-scm.com

Linux:

  • sudo apt-get install git (Ubuntu/Debian)
  • sudo yum install git (CentOS/RHEL)

Verify installation: git --version

First-Time Setup

Configure your name and email (one time):

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

This identifies you in commits. Use the same email as your GitHub account if you have one.

Creating a Repository

A repository (repo) is a folder that Git tracks. Two ways to create one:

1. Initialize existing folder:

cd my-project
git init

2. Clone existing repository:

git clone https://github.com/username/repo-name.git

git init creates a hidden .git folder that tracks everything.

Basic Workflow

Git has three main areas:

  • Working Directory: Your files (what you're editing)
  • Staging Area: Files ready to be committed
  • Repository: Committed changes (saved versions)

The process:

  1. Make changes to files
  2. Stage changes (git add)
  3. Commit changes (git commit)

Checking Status

See what's changed:

git status

Shows:

  • Untracked files (new files Git doesn't know about)
  • Modified files (changed but not staged)
  • Staged files (ready to commit)

Staging Changes

Add files to staging area:

# Stage specific file
git add filename.txt

# Stage all files in current directory
git add .

# Stage all files in repository
git add -A

Staging lets you choose what to commit. You can stage some files and leave others unstaged.

Committing Changes

Save a snapshot of your changes:

git commit -m "Your commit message"

Good commit messages:

  • Clear and descriptive
  • Explain what changed and why
  • Examples: "Add login form", "Fix button styling", "Update README"

Bad commit messages:

  • "asdf", "fix", "update", "changes"
  • Too vague - doesn't explain what changed
Advertisement

Viewing History

See all commits:

git log

Shows commit hash, author, date, and message. Press q to quit.

One-line view:

git log --oneline

See what changed in a commit:

git show

Undoing Changes

Unstage a file (keep changes):

git reset filename.txt

Discard changes in working directory:

git checkout -- filename.txt

Or (newer syntax):

git restore filename.txt

Change last commit message:

git commit --amend -m "New message"

Warning: Be careful with undo commands. Make sure you really want to discard changes.

Branches

Branches let you work on different features without affecting main code:

# Create new branch
git branch feature-name

# Switch to branch
git checkout feature-name

# Or create and switch in one command
git checkout -b feature-name

# List all branches
git branch

# Switch back to main
git checkout main

# Delete branch
git branch -d feature-name

Why use branches?

  • Work on features without breaking main code
  • Experiment safely
  • Collaborate without conflicts

Main branch is usually called main or master.

Merging Branches

Combine branch changes into main:

# Switch to main branch
git checkout main

# Merge feature branch
git merge feature-name

If there are conflicts, Git will tell you. You'll need to resolve them manually.

Remote Repositories

Store your code online (GitHub, GitLab, etc.):

Add remote:

git remote add origin https://github.com/username/repo.git

Push to remote:

git push -u origin main

Pull from remote:

git pull

Clone remote repository:

git clone https://github.com/username/repo.git

Common Workflow

Typical daily workflow:

  1. git status - Check what changed
  2. git add . - Stage all changes
  3. git commit -m "Description" - Commit changes
  4. git push - Upload to remote

When working with others:

  1. git pull - Get latest changes
  2. Make your changes
  3. git add .
  4. git commit -m "Description"
  5. git push

.gitignore

Tell Git to ignore certain files (passwords, build files, etc.):

Create .gitignore file:

# Ignore specific file
secret.txt

# Ignore all files with extension
*.log

# Ignore folder
node_modules/

# Common ignores
.DS_Store
.env
*.exe

Git won't track files listed in .gitignore.

Common Mistakes

  • Committing too often or too rarely: Commit logical units of work, not every single change
  • Bad commit messages: Write clear messages that explain what changed
  • Not pulling before pushing: Always pull first when working with others
  • Committing sensitive data: Never commit passwords, API keys, or secrets
  • Forgetting to stage files: Use git status to check what's staged
  • Working on main branch: Create feature branches for new work

Pro Tip: Commit often with clear messages. It's like saving your game - you can always go back. Use branches for new features. Pull before pushing when working with others. And never commit secrets - use .gitignore for sensitive files.

Next Steps

Once you're comfortable with basics:

  • Learn about pull requests (GitHub workflow)
  • Learn about rebasing
  • Learn about Git hooks
  • Practice with GitHub
  • Learn about resolving merge conflicts

Common Questions

Do I need GitHub to use Git?

No. Git works locally on your computer. GitHub is just a place to store your repositories online and collaborate. You can use Git without GitHub, but GitHub makes collaboration easier.

What's the difference between Git and GitHub?

Git is the version control tool that runs on your computer. GitHub is a website that hosts Git repositories online. Think of Git as the engine and GitHub as the garage where you park your car.

How often should I commit?

Commit logical units of work. Not every single change, but not huge batches either. A good rule: commit when you've completed a small feature or fixed a bug. Aim for multiple commits per day when actively working.

Start Using Git

Create a test project, initialize Git, and start committing. Make some changes, commit them, check the history. Create a branch, make changes, merge it back. The more you use Git, the more natural it becomes. It's an essential tool for any developer.

Advertisement