Getting Started with Git
Welcome to your Git journey! This guide will help you get started with Git, from installation to your first commit.
Installation
Windows
Download Git from git-scm.com and run the installer.
macOS
Install using Homebrew:
bash
brew install gitOr download from git-scm.com.
Linux
Use your distribution's package manager:
Ubuntu/Debian:
bash
sudo apt-get install gitFedora:
bash
sudo dnf install gitInitial Configuration
After installing Git, configure your identity:
bash
# Set your name
git config --global user.name "Your Name"
# Set your email
git config --global user.email "your.email@example.com"
# Set default branch name to main
git config --global init.defaultBranch main
# Check your configuration
git config --listCreating Your First Repository
Initialize a New Repository
bash
# Create a new directory
mkdir my-project
cd my-project
# Initialize Git
git initClone an Existing Repository
bash
git clone https://github.com/username/repository.gitBasic Workflow
1. Check Status
bash
git status2. Add Files
bash
# Add specific file
git add filename.txt
# Add all files
git add .3. Commit Changes
bash
git commit -m "Your commit message"4. View History
bash
git logWorking with Remotes
Add a Remote Repository
bash
git remote add origin https://github.com/username/repository.gitPush Your Changes
bash
git push -u origin mainPull Changes from Remote
bash
git pull origin mainEssential Commands for Beginners
| Command | Description |
|---|---|
git init | Initialize a new repository |
git clone <url> | Clone a repository |
git status | Check repository status |
git add <file> | Stage files for commit |
git commit -m "message" | Commit staged changes |
git push | Push commits to remote |
git pull | Pull changes from remote |
git log | View commit history |
Common Scenarios
Making Your First Commit
bash
# Create a new file
echo "# My Project" > README.md
# Stage the file
git add README.md
# Commit the file
git commit -m "Initial commit: Add README"
# Push to remote (if set up)
git push origin mainChecking What Changed
bash
# See what files changed
git status
# See specific changes
git diffUndoing Changes
bash
# Discard changes in a file
git checkout -- filename.txt
# Unstage a file
git reset HEAD filename.txtBest Practices for Beginners
- Commit Often: Make small, focused commits
- Write Clear Messages: Describe what and why, not how
- Pull Before Push: Always pull latest changes before pushing
- Use Branches: Keep your main branch stable
- Review Before Commit: Check
git statusandgit diff
Common Pitfalls to Avoid
- ❌ Don't commit sensitive information (passwords, API keys)
- ❌ Don't make huge commits with unrelated changes
- ❌ Don't force push to shared branches
- ❌ Don't ignore merge conflicts
- ❌ Don't work directly on the main branch for features
Getting Help
Command Help
bash
# Get help for any command
git help <command>
git <command> --help
# Quick reference
git <command> -hUseful Resources
Next Steps
Now that you have the basics, explore:
Ready to dive deeper? Let's explore the powerful features of Git!