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
- Create a feature branch
- Make changes
- Stage and commit
- Push to remote
- Create pull request
- Merge after review
- 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
- Write clear commit messages
- Commit often, in logical chunks
- Pull before pushing
- Use branches for features
- Review changes before committing
- Don't commit sensitive data