Git

Installation

Ubuntu/Debian

# Update package manager
sudo apt update
 
# Install Git
sudo apt install git -y
 
# Verify installation
git --version

CentOS/RHEL

# Install Git
sudo yum install git -y
 
# Verify installation
git --version

Initial Configuration

# Set global name
git config --global user.name "Your Name"
 
# Set global email
git config --global user.email "[email protected]"
 
# Set default editor
git config --global core.editor "nano"
 
# View all settings
git config --list
 
# View global settings
git config --list --global

Repository Setup

# Clone repository
git clone https://github.com/user/repo.git
 
# Clone with specific branch
git clone -b branch-name https://github.com/user/repo.git
 
# Clone with depth (faster for large repos)
git clone --depth 1 https://github.com/user/repo.git
 
# Initialize new repository
git init
 
# Add remote origin
git remote add origin https://github.com/user/repo.git
 
# Change remote URL
git remote set-url origin https://github.com/user/newrepo.git
 
# View remote
git remote -v

Basic Commands

# Check status
git status
 
# Add files
git add filename
git add .              # Add all files
 
# Commit changes
git commit -m "commit message"
git commit -am "commit message"  # Stage and commit
 
# Push to remote
git push
git push origin branch-name
 
# Pull from remote
git pull
git pull origin branch-name
 
# Fetch changes
git fetch
git fetch origin

Branch Management

# List branches
git branch                    # Local branches
git branch -a                 # All branches (local and remote)
 
# Create branch
git branch branch-name
git checkout -b branch-name   # Create and switch
 
# Switch branch
git checkout branch-name
git switch branch-name        # New syntax
 
# Delete branch
git branch -d branch-name
git branch -D branch-name     # Force delete
 
# Rename branch
git branch -m old-name new-name
 
# Track remote branch
git branch -u origin/branch-name

Merge & Rebase

# Merge branch
git merge branch-name
 
# Rebase branch
git rebase branch-name
 
# Abort merge/rebase
git merge --abort
git rebase --abort
 
# Continue after conflict resolution
git merge --continue
git rebase --continue

Commit History

# View commit log
git log
git log --oneline           # One line per commit
git log --graph --all       # Visual branch graph
git log --author="name"     # Filter by author
git log --grep="text"       # Filter by message
 
# View specific commit
git show commit-hash
 
# View changes between commits
git diff commit1 commit2

Stashing Changes

# Stash changes
git stash
 
# List stashes
git stash list
 
# Apply stash
git stash apply stash@{0}
 
# Pop stash (apply and delete)
git stash pop
 
# Delete stash
git stash drop stash@{0}

Reset & Revert

# Undo changes (working directory)
git checkout filename
git restore filename
 
# Undo staged changes
git reset filename
git restore --staged filename
 
# Undo last commit (keep changes)
git reset --soft HEAD~1
 
# Undo last commit (discard changes)
git reset --hard HEAD~1
 
# Revert commit (create new commit)
git revert commit-hash

Tags

# Create tag
git tag v1.0.0
git tag -a v1.0.0 -m "version 1.0.0"  # Annotated tag
 
# List tags
git tag
 
# Delete tag
git tag -d v1.0.0
 
# Push tags
git push origin v1.0.0        # Push single tag
git push origin --tags        # Push all tags

SSH Key Setup

# Generate SSH key
ssh-keygen -t rsa -b 4096 -C "[email protected]"
 
# View public key
cat ~/.ssh/id_rsa.pub
 
# Add to Git service (GitHub, GitLab, etc.)
# Copy content of id_rsa.pub to Settings > SSH Keys
 
# Test connection
ssh -T [email protected]

Useful Aliases

# Create aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'restore --staged'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'