Documentation/git/fundamentals

Git Fundamentals

Git is a distributed version control system that tracks changes in your codebase and enables collaboration.

Installation

Linux

sudo apt-get install git  # Ubuntu/Debian
sudo yum install git      # CentOS/RHEL

macOS

brew install git

Initial Configuration

# Set your identity
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

# View configuration
git config --list

Basic Workflow

Initialize a Repository

# Create new repository
git init my-project
cd my-project

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

Stage and Commit

# Check status
git status

# Stage files
git add filename
git add .              # Stage all changes

# Commit changes
git commit -m "Your commit message"

# View commit history
git log

Branching

# List branches
git branch

# Create new branch
git branch feature-branch

# Switch branch
git checkout feature-branch

# Create and switch to new branch
git checkout -b new-feature

Remote Repositories

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

# View remotes
git remote -v

# Push to remote
git push origin main

# Pull from remote
git pull origin main

# Fetch updates
git fetch origin

Advanced Features

Merging

# Merge branch into current branch
git merge feature-branch

# Merge with commit message
git merge --no-ff feature-branch

Rebasing

# Rebase current branch
git rebase main

# Interactive rebase
git rebase -i HEAD~3

Stashing

# Stash changes
git stash

# List stashes
git stash list

# Apply stash
git stash apply

Best Practices

  • Write clear, descriptive commit messages
  • Commit frequently with logical chunks
  • Use branches for new features
  • Review code before merging
  • Keep repository clean and organized