Git Full Documentation
This guide covers Git from beginner to advanced level including private repositories, SSH authentication, merging strategies, and recovery techniques.
Install Git
# Ubuntu/Debian
sudo apt update
sudo apt install git -y
# Check version
git --version
sudo apt update
sudo apt install git -y
# Check version
git --version
Initialize & Configure
# Set global username
git config --global user.name "Your Name"
# Set email
git config --global user.email "you@example.com"
# View configuration
git config --list
# Initialize repository
git init
git config --global user.name "Your Name"
# Set email
git config --global user.email "you@example.com"
# View configuration
git config --list
# Initialize repository
git init
Add & Commit
# Check status
git status
# Add specific file
git add index.php
# Add all files
git add .
# Commit changes
git commit -m "Initial commit"
# Amend last commit
git commit --amend
git status
# Add specific file
git add index.php
# Add all files
git add .
# Commit changes
git commit -m "Initial commit"
# Amend last commit
git commit --amend
Log & History
# Show commit history
git log
# Compact log
git log --oneline
# Graph view
git log --graph --oneline --all
git log
# Compact log
git log --oneline
# Graph view
git log --graph --oneline --all
Branching
# List branches
git branch
# Create new branch
git branch feature-login
# Switch branch
git checkout feature-login
# Create and switch
git checkout -b feature-payment
git branch
# Create new branch
git branch feature-login
# Switch branch
git checkout feature-login
# Create and switch
git checkout -b feature-payment
Merge
# Merge feature into main
git checkout main
git merge feature-login
# Abort merge if conflict
git merge --abort
git checkout main
git merge feature-login
# Abort merge if conflict
git merge --abort
Rebase
# Rebase feature onto main
git checkout feature-login
git rebase main
# Continue after conflict
git rebase --continue
git checkout feature-login
git rebase main
# Continue after conflict
git rebase --continue
Add Remote Repository
# Add remote
git remote add origin https://github.com/user/repo.git
# View remotes
git remote -v
git remote add origin https://github.com/user/repo.git
# View remotes
git remote -v
Push & Pull
# Push to remote
git push origin main
# Pull updates
git pull origin main
# Clone repository
git clone https://github.com/user/repo.git
git push origin main
# Pull updates
git pull origin main
# Clone repository
git clone https://github.com/user/repo.git
SSH Setup for Private Repositories
# Generate SSH key
ssh-keygen -t ed25519 -C "you@example.com"
# Add SSH remote
git remote set-url origin git@github.com:user/repo.git
# Test connection
ssh -T git@github.com
ssh-keygen -t ed25519 -C "you@example.com"
# Add SSH remote
git remote set-url origin git@github.com:user/repo.git
# Test connection
ssh -T git@github.com
Git Stash
# Save changes temporarily
git stash
# List stashes
git stash list
# Apply stash
git stash apply
# Drop stash
git stash drop
git stash
# List stashes
git stash list
# Apply stash
git stash apply
# Drop stash
git stash drop
Reset & Revert
# Soft reset (keep changes)
git reset --soft HEAD~1
# Hard reset (delete changes)
git reset --hard HEAD~1
# Revert commit safely
git revert HEAD
git reset --soft HEAD~1
# Hard reset (delete changes)
git reset --hard HEAD~1
# Revert commit safely
git revert HEAD
Cherry Pick
# Apply specific commit
git cherry-pick COMMIT_ID
git cherry-pick COMMIT_ID
Tags
# Create tag
git tag v1.0
# Push tag
git push origin v1.0
git tag v1.0
# Push tag
git push origin v1.0
Stop Git From Asking Login
# Use credential manager (HTTPS)
git config --global credential.helper manager
# OR use SSH instead (recommended)
git remote set-url origin git@github.com:user/repo.git
git config --global credential.helper manager
# OR use SSH instead (recommended)
git remote set-url origin git@github.com:user/repo.git
Git Credential Manager Setup (Linux - Recommended)
# Ensure curl is installed
curl --version
# Install curl if missing (Ubuntu/Debian)
sudo apt update && sudo apt install curl -y
# Download and install Git Credential Manager
curl -L https://aka.ms/gcm/linux-install-source.sh | sh
# Configure Git to use it
git-credential-manager configure
# Verify installation
git config --global --list
curl --version
# Install curl if missing (Ubuntu/Debian)
sudo apt update && sudo apt install curl -y
# Download and install Git Credential Manager
curl -L https://aka.ms/gcm/linux-install-source.sh | sh
# Configure Git to use it
git-credential-manager configure
# Verify installation
git config --global --list
Alternative: Store Credentials (Less Secure)
# Store credentials in plain text
git config --global credential.helper store
git config --global credential.helper store
Credential Cache Behavior
# Use in-memory credential caching
git config --global credential.credentialStore cache
# Optional: set cache timeout (in seconds)
git config --global credential.helper "cache --timeout=3600"
# Check current configuration
git config --global --list
git config --global credential.credentialStore cache
# Optional: set cache timeout (in seconds)
git config --global credential.helper "cache --timeout=3600"
# Check current configuration
git config --global --list
Note: When using
:contentReference[oaicite:0]{index=0}
with cache, credentials are stored temporarily in memory.
- ✔ No repeated login during active session
- ❌ Credentials are cleared after system restart
- ❌ Credentials are lost when session ends
Tip: Use cache for servers (e.g., EC2).
For a permanent solution without repeated logins, consider using SSH authentication instead.
git config --unset user.name
git config --unset user.email
# Remove configuration from global Git settings
git config --global --unset user.name
git config --global --unset user.email
# Remove configuration from system-wide settings
sudo git config --system --unset user.name
sudo git config --system --unset user.email
# Verify current configuration
git config --list
# Remove an entire section (example)
git config --remove-section user
Recover Deleted Work
# View reference log
git reflog
# Recover commit
git checkout COMMIT_ID
git reflog
# Recover commit
git checkout COMMIT_ID