Git Basics: Complete Beginner's Guide
Learn Git fundamentals from scratch. Commits, branches, merging, and essential Git commands for version control.
Git Basics: Complete Beginner's Guide
Git is the most widely used version control system in the world. This comprehensive guide will teach you Git fundamentals from the ground up.
What is Git?
Git is a distributed version control system that tracks changes in files and coordinates work among multiple people. It allows you to:
- Track changes in your code over time
- Collaborate with other developers
- Revert to previous versions
- Branch and merge code safely
- Backup your work automatically
Installing Git
Windows
# Download from https://git-scm.com/download/win
# Or use Chocolatey
choco install git
# Or use winget
winget install Git.Git
macOS
# Using Homebrew
brew install git
# Or download from https://git-scm.com/download/mac
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install git
Linux (CentOS/RHEL)
sudo yum install git
# or
sudo dnf install git
First-Time Git Setup
Configure Your Identity
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "your.email@example.com"
# Check your configuration
git config --list
Configure Default Editor
# Set VS Code as default editor
git config --global core.editor "code --wait"
# Set Vim as default editor
git config --global core.editor "vim"
# Set Nano as default editor
git config --global core.editor "nano"
Creating Your First Repository
Initialize a New Repository
# Create a new directory
mkdir my-project
cd my-project
# Initialize Git repository
git init
# Check status
git status
Clone an Existing Repository
# Clone from GitHub
git clone https://github.com/username/repository.git
# Clone to specific directory
git clone https://github.com/username/repository.git my-folder
# Clone with specific branch
git clone -b branch-name https://github.com/username/repository.git
Basic Git Workflow
1. Check Status
# See current status
git status
# Short status
git status -s
2. Add Files to Staging
# Add specific file
git add filename.txt
# Add all files
git add .
# Add files by pattern
git add *.js
# Interactive add
git add -i
3. Commit Changes
# Commit with message
git commit -m "Add initial project files"
# Commit with detailed message
git commit -m "Add user authentication
- Implement login functionality
- Add password validation
- Create user session management"
# Amend last commit
git commit --amend -m "Updated commit message"
4. View History
# View commit history
git log
# One-line format
git log --oneline
# Graph format
git log --graph --oneline --all
# Show changes
git log -p
Working with Branches
Create and Switch Branches
# Create new branch
git branch feature-branch
# Switch to branch
git checkout feature-branch
# Create and switch in one command
git checkout -b feature-branch
# Modern way (Git 2.23+)
git switch -c feature-branch
List and Delete Branches
# List local branches
git branch
# List all branches (including remote)
git branch -a
# Delete local branch
git branch -d feature-branch
# Force delete branch
git branch -D feature-branch
# Delete remote branch
git push origin --delete feature-branch
Merge Branches
# Switch to main branch
git checkout main
# Merge feature branch
git merge feature-branch
# Merge with no fast-forward
git merge --no-ff feature-branch
# Squash merge
git merge --squash feature-branch
Remote Repositories
Add Remote
# Add remote repository
git remote add origin https://github.com/username/repository.git
# List remotes
git remote -v
# Change remote URL
git remote set-url origin https://github.com/username/new-repository.git
Push and Pull
# Push to remote
git push origin main
# Push new branch
git push -u origin feature-branch
# Pull changes
git pull origin main
# Fetch without merging
git fetch origin
Essential Git Commands
Viewing Changes
# Show differences
git diff
# Show staged differences
git diff --staged
# Show commit differences
git diff HEAD~1
# Show file history
git log --follow filename.txt
Undoing Changes
# Unstage file
git reset HEAD filename.txt
# Discard changes
git checkout -- filename.txt
# Reset to last commit
git reset --hard HEAD
# Reset to specific commit
git reset --hard commit-hash
Stashing
# Stash current changes
git stash
# Stash with message
git stash push -m "Work in progress"
# List stashes
git stash list
# Apply stash
git stash apply
# Apply and remove stash
git stash pop
# Drop stash
git stash drop
Best Practices
Commit Messages
# Good commit messages
git commit -m "Add user authentication feature"
git commit -m "Fix bug in login validation"
git commit -m "Update documentation for API endpoints"
# Bad commit messages
git commit -m "fix"
git commit -m "update"
git commit -m "changes"
.gitignore File
Create a .gitignore file to exclude files:
# Dependencies
node_modules/
package-lock.json
# Build outputs
dist/
build/
*.exe
# Environment files
.env
.env.local
# IDE files
.vscode/
.idea/
*.swp
# OS files
.DS_Store
Thumbs.db
Branch Naming
# Good branch names
feature/user-authentication
bugfix/login-validation
hotfix/security-patch
release/v1.2.0
# Bad branch names
new-feature
fix
test
Common Git Scenarios
Scenario 1: Undo Last Commit
# Keep changes, unstage
git reset --soft HEAD~1
# Discard changes completely
git reset --hard HEAD~1
Scenario 2: Change Last Commit
# Add more changes
git add .
git commit --amend -m "Updated commit message"
Scenario 3: Merge Conflicts
# When merge conflict occurs
git status
# Edit conflicted files
git add resolved-file.txt
git commit
Scenario 4: Rebase Instead of Merge
# Rebase feature branch onto main
git checkout feature-branch
git rebase main
# Interactive rebase
git rebase -i HEAD~3
Advanced Git Features
Aliases
# Create useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
Hooks
# Pre-commit hook example
#!/bin/sh
# Run tests before commit
npm test
Submodules
# Add submodule
git submodule add https://github.com/user/repo.git path/to/submodule
# Update submodules
git submodule update --init --recursive
Troubleshooting
Common Issues
"Your branch is ahead of origin/main"
bashgit push origin main"Your branch and origin/main have diverged"
bashgit pull --rebase origin main"Please tell me who you are"
bashgit config --global user.name "Your Name" git config --global user.email "your.email@example.com"Accidentally committed to wrong branch
bashgit checkout correct-branch git cherry-pick commit-hash git checkout wrong-branch git reset --hard HEAD~1
Git GUI Tools
Command Line Tools
- Git Bash (Windows)
- Terminal (macOS/Linux)
GUI Applications
- GitHub Desktop
- Sourcetree
- GitKraken
- VS Code Git Integration
Conclusion
Git is an essential tool for any developer. By mastering these fundamentals, you'll be able to:
- Track your code changes effectively
- Collaborate with other developers
- Maintain clean project history
- Handle complex development workflows
Remember to:
- Commit often with meaningful messages
- Use branches for feature development
- Keep your repository clean
- Learn advanced features gradually
- Practice regularly
Happy coding! 🚀