beginner general 14 min read

Git Basics for Web Developers

Learn essential Git commands for version control in web development.

git basics git tutorial version control git commands

What is Git?

Git is a version control system that tracks changes to your files. It enables collaboration and maintains a history of your project.

Initial Setup

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

Starting a Repository

# Initialize new repository
git init

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

Basic Workflow

Check Status

git status

Stage Changes

# Stage specific files
git add filename.txt

# Stage all changes
git add .

Commit Changes

git commit -m "Add feature description"

Push to Remote

git push origin main

Pull Updates

git pull origin main

Branching

Create Branch

git branch feature-name
git checkout feature-name

# Or in one command
git checkout -b feature-name

List Branches

git branch      # Local branches
git branch -a   # All branches

Switch Branches

git checkout main
git checkout feature-name

Merge Branches

git checkout main
git merge feature-name

Delete Branch

git branch -d feature-name

Viewing History

# View commit history
git log

# Compact view
git log --oneline

# With graph
git log --oneline --graph

Undoing Changes

Unstage Files

git reset HEAD filename

Discard Changes

git checkout -- filename

Amend Last Commit

git commit --amend -m "New message"

Revert a Commit

git revert commit-hash

Remote Repositories

Add Remote

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

View Remotes

git remote -v

Fetch Updates

git fetch origin

Common Workflow

  1. Create a feature branch
  2. Make changes
  3. Stage and commit
  4. Push to remote
  5. Create pull request
  6. Merge after review
  7. Delete feature branch

.gitignore

Create a .gitignore file to exclude files:

# Dependencies
node_modules/

# Build output
dist/
build/

# Environment
.env
.env.local

# IDE
.vscode/
.idea/

# OS
.DS_Store
Thumbs.db

Best Practices

  1. Write clear commit messages
  2. Commit often, in logical chunks
  3. Pull before pushing
  4. Use branches for features
  5. Review changes before committing
  6. Don't commit sensitive data

Continue Learning