Git Beginner's Guide
Table of Contents
- What is Git?
- Installation
- Basic Configuration
- Core Concepts
- Essential Commands
- Working with Repositories
- Branching and Merging
- Remote Repositories
- Common Workflows
- Best Practices
- Troubleshooting
What is Git?
Git is a distributed version control system that helps you track changes in your code over time. Think of it as a sophisticated "save" system that:
- Keeps a complete history of all changes
- Allows multiple people to work on the same project
- Lets you experiment with different features safely
- Helps you recover from mistakes
Key Benefits
- Version Control: Track every change made to your files
- Collaboration: Multiple developers can work on the same project
- Backup: Your code history is preserved
- Branching: Work on different features simultaneously
Installation
Linux (Ubuntu/Debian)
sudo apt update
sudo apt install git
Linux (CentOS/RHEL/Fedora)
# CentOS/RHEL
sudo yum install git
# Fedora
sudo dnf install git
macOS
# Using Homebrew
brew install git
# Or download from https://git-scm.com/download/mac
Windows
Download from https://git-scm.com/download/windows
Verify Installation
git --version
Basic Configuration
Before using Git, set up your identity:
# Set your name and email (required)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Set default branch name (recommended)
git config --global init.defaultBranch main
# Set default editor (optional)
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "nano" # Nano
git config --global core.editor "vim" # Vim
# View your configuration
git config --list
Core Concepts
Repository (Repo)
A folder that contains your project and its complete history.
Working Directory
The current state of your files that you can see and edit.
Staging Area (Index)
A temporary area where you prepare changes before committing them.
Commit
A snapshot of your project at a specific point in time.
Branch
An independent line of development.
Remote
A version of your repository hosted elsewhere (like GitHub).
Essential Commands
Getting Help
git help <command>
git <command> --help
Repository Operations
Initialize a New Repository
git init
Clone an Existing Repository
git clone <repository-url>
git clone https://github.com/user/repo.git
File Operations
Check Repository Status
git status
Add Files to Staging Area
git add <filename> # Add specific file
git add . # Add all files in current directory
git add *.js # Add all JavaScript files
git add -A # Add all changes (including deletions)
Commit Changes
git commit -m "Your commit message"
git commit -am "Add and commit in one step" # For tracked files only
View Commit History
git log
git log --oneline # Compact view
git log --graph # Visual representation
git log -n 5 # Show last 5 commits
View Changes
git diff # Changes in working directory
git diff --staged # Changes in staging area
git diff HEAD~1 # Compare with previous commit
Working with Repositories
Basic Workflow
- Make changes to your files
- Stage changes with
git add
- Commit changes with
git commit
- Push changes to remote repository
Example Workflow
# 1. Check current status
git status
# 2. Make changes to files (edit in your editor)
# 3. Add changes to staging area
git add .
# 4. Commit changes
git commit -m "Add new feature"
# 5. Push to remote repository
git push origin main
Undoing Changes
Unstage Files
git reset <filename> # Unstage specific file
git reset # Unstage all files
Discard Changes in Working Directory
git checkout -- <filename> # Discard changes to specific file
git checkout -- . # Discard all changes
Undo Last Commit (Keep Changes)
git reset --soft HEAD~1
Undo Last Commit (Discard Changes)
git reset --hard HEAD~1
Branching and Merging
Branch Operations
List Branches
git branch # Local branches
git branch -r # Remote branches
git branch -a # All branches
Create and Switch Branches
git branch <branch-name> # Create branch
git checkout <branch-name> # Switch to branch
git checkout -b <branch-name> # Create and switch in one command
git switch <branch-name> # Modern way to switch branches
git switch -c <branch-name> # Create and switch (modern)
Merge Branches
git checkout main # Switch to main branch
git merge <feature-branch> # Merge feature branch into main
Delete Branches
git branch -d <branch-name> # Delete merged branch
git branch -D <branch-name> # Force delete branch
Example Branching Workflow
# Create and switch to feature branch
git checkout -b feature/new-login
# Make changes and commit
git add .
git commit -m "Implement new login system"
# Switch back to main
git checkout main
# Merge feature branch
git merge feature/new-login
# Delete feature branch
git branch -d feature/new-login
Remote Repositories
Adding Remotes
git remote add origin <repository-url>
git remote add origin https://github.com/user/repo.git
Viewing Remotes
git remote -v
Pushing Changes
git push origin main # Push main branch
git push origin <branch-name> # Push specific branch
git push -u origin main # Set upstream and push
Pulling Changes
git pull origin main # Fetch and merge
git fetch origin # Fetch without merging
git merge origin/main # Merge fetched changes
Working with GitHub/GitLab
First Time Setup
# Clone repository
git clone https://github.com/user/repo.git
cd repo
# Make changes
echo "Hello World" > README.md
git add README.md
git commit -m "Add README"
# Push changes
git push origin main
Common Workflows
Feature Branch Workflow
# 1. Start from main branch
git checkout main
git pull origin main
# 2. Create feature branch
git checkout -b feature/user-authentication
# 3. Work on feature
# ... make changes ...
git add .
git commit -m "Add user login functionality"
# 4. Push feature branch
git push origin feature/user-authentication
# 5. Create pull request (on GitHub/GitLab)
# 6. After review, merge and cleanup
git checkout main
git pull origin main
git branch -d feature/user-authentication
Hotfix Workflow
# 1. Create hotfix branch from main
git checkout main
git checkout -b hotfix/critical-bug
# 2. Fix the bug
# ... make changes ...
git add .
git commit -m "Fix critical security vulnerability"
# 3. Push and merge quickly
git push origin hotfix/critical-bug
# Create pull request and merge immediately
# 4. Cleanup
git checkout main
git pull origin main
git branch -d hotfix/critical-bug
Best Practices
Commit Messages
- Use present tense: "Add feature" not "Added feature"
- Keep first line under 50 characters
- Be descriptive but concise
- Use imperative mood
Good Examples:
Add user authentication system
Fix memory leak in image processing
Update documentation for API endpoints
Bad Examples:
Fixed stuff
WIP
asdfgh
Updated files
Branching Strategy
- Use descriptive branch names:
feature/user-login
,bugfix/header-styling
- Keep branches focused on single features
- Delete merged branches
- Regularly sync with main branch
General Tips
- Commit often, push regularly
- Write meaningful commit messages
- Use
.gitignore
for files you don't want to track - Review changes before committing
- Keep commits atomic (one logical change per commit)
Troubleshooting
Common Issues and Solutions
"Permission denied" when pushing
# Check remote URL
git remote -v
# Use HTTPS instead of SSH if having issues
git remote set-url origin https://github.com/user/repo.git
Merge Conflicts
# When merge conflict occurs:
# 1. Open conflicted files and resolve conflicts manually
# 2. Remove conflict markers (<<<<<<<, =======, >>>>>>>)
# 3. Add resolved files
git add <resolved-file>
# 4. Complete the merge
git commit -m "Resolve merge conflict"
Accidentally committed wrong files
# Remove file from last commit but keep in working directory
git reset --soft HEAD~1
git reset HEAD <unwanted-file>
git commit -m "Correct commit message"
Want to see what changed in a commit
git show <commit-hash>
git show HEAD # Show last commit
git show HEAD~2 # Show commit 2 steps back
Forgot to add files to last commit
# Add forgotten files
git add <forgotten-file>
# Amend last commit
git commit --amend --no-edit
Useful Aliases
Add these to your Git configuration for shortcuts:
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'
Next Steps
Once you're comfortable with these basics:
- Learn about Git workflows (GitFlow, GitHub Flow)
- Explore advanced features (rebasing, cherry-picking, stashing)
- Set up SSH keys for easier authentication
- Learn about Git hooks for automation
- Practice with a real project on GitHub/GitLab
Helpful Resources
Remember: Git has a learning curve, but once you understand the basics, it becomes an invaluable tool for any developer. Start with simple commands and gradually work your way up to more advanced features. Practice regularly, and don't be afraid to experiment in test repositories!