From 53c5732757ee86e498a13857fb5539aa43ad984d Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Fri, 14 Feb 2025 11:14:32 +0100 Subject: [PATCH] Add a comprehensive Git cheatsheet Provides a detailed reference for Git commands including repository setup, basic operations, branching, stashing, history review, merging, rebasing, and configuration. Useful for quick lookup and as a learning resource for users. --- .gitignore | 75 +++++++++++++++++++++ cheat-sheets/git.md | 157 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 232 insertions(+) create mode 100644 .gitignore create mode 100644 cheat-sheets/git.md diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1dea202 --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Ignore IntelliJ IDEA project-specific files +.idea/ +*.iml +*.iws + +# Ignore OS generated files +.DS_Store +.DS_Store? +Thumbs.db +ehthumbs.db +Desktop.ini + +# Ignore temporary files +*.tmp +*.swp +*.swo +*.bak +*.orig + +# Ignore hidden system files +.*.s[a-v][a-z] +.*un~ +$RECYCLE.BIN/ + +# Ignore logs and runtime files +*.log +*.pid +*.seed +*.gz + +# Ignore clutter generated by markdown previews/editors +*.markdown-tmp +.markdown-cache/ + +# Ignore common backup files +*.~* +*.save +*~ + +# Ignore unnecessary files for compressed formats (if extracting archives is needed) +*.zip +*.tar +*.tar.gz +*.rar + +# Ignore image editor temp files (if images are modified) +*.xcf~ +*.psd~ + +# Ignore binary thumbnails or preview files (if editors generate them) +*.thumbs + +# Ignore files specific to versioning history outside Git +*.orig + +# Include images, documents, and markdown explicitly (so they're not accidentally excluded) +!*.md +!*.markdown +!*.png +!*.jpg +!*.jpeg +!*.gif +!*.pdf +!*.doc +!*.docx +!*.ppt +!*.pptx +!*.xls +!*.xlsx +!*.odt + +# Ensure backup or runtime-related folders are ignored (user-configurable) +backup/ +temp/ +generated/ \ No newline at end of file diff --git a/cheat-sheets/git.md b/cheat-sheets/git.md new file mode 100644 index 0000000..d041847 --- /dev/null +++ b/cheat-sheets/git.md @@ -0,0 +1,157 @@ +# Git Cheatsheet +## Repository Setup & Remote Operations +```bash +# Clone a repository +git clone + +# Add a remote +git remote add +git remote add origin git@github.com:user/repo.git + +# Change remote URL +git remote set-url origin + +# View remotes +git remote -v +``` + +## Basic Commands +```bash +# Check status +git status + +# Stage files +git add # Stage specific file +git add . # Stage all changes +git add -p # Interactively stage changes + +# Commit +git commit -m "message" +git commit --amend # Modify last commit + +# Push/Pull +git push origin +git pull origin +git pull --rebase origin # Pull with rebase +``` + +## Branch Operations +```bash +# List branches +git branch # Local branches +git branch -r # Remote branches +git branch -a # All branches + +# Create branch +git branch +git checkout -b # Create and switch + +# Checkout remote branch (create if not exists) +git checkout -b origin/ +git checkout --track origin/ # Shorter version + +# Switch branch +git checkout + +# Delete branch +git branch -d # Local branch +git push origin --delete # Remote branch +``` + +## Working with Changes +```bash +# View changes +git diff # Unstaged changes +git diff --staged # Staged changes +git diff # Between branches + +# Discard changes +git checkout -- # Discard file changes +git restore # Modern version +git reset --hard HEAD # Discard all changes + +# Stash changes +git stash +git stash save "message" +git stash list +git stash pop # Apply and remove +git stash apply # Apply and keep +``` + +## History and Logs +```bash +# View history +git log +git log --oneline # Compact view +git log --graph # With branch graph +git blame # File history + +# Search history +git log --grep="keyword" # Search commits +git log -S"code" # Search code changes +``` + +## Merge and Rebase +```bash +# Merge +git merge +git merge --abort # Cancel merge + +# Rebase +git rebase +git rebase -i HEAD~3 # Interactive rebase last 3 commits +git rebase --abort # Cancel rebase +``` + +## Credential Caching +```bash +# Cache credentials temporarily +git config --global credential.helper cache # Default 15 min +git config --global credential.helper 'cache --timeout=3600' # Custom timeout (seconds) + +# Store credentials indefinitely +git config --global credential.helper store # Stored in plaintext +git config --global credential.helper 'manager-core' # Windows credential manager +git config --global credential.helper osxkeychain # macOS keychain + +# For GitHub specifically - Using Personal Access Token +git config --global credential.helper store # Then use token as password +``` + +## Configuration +```bash +# User setup +git config --global user.name "Your Name" +git config --global user.email "your.email@example.com" + +# Aliases +git config --global alias.co checkout +git config --global alias.br branch +git config --global alias.st status +git config --global alias.unstage 'reset HEAD --' + +# View config +git config --list +``` + +## Advanced/Useful Commands +```bash +# Cherry-pick commits +git cherry-pick + +# Clean untracked files +git clean -n # Dry run +git clean -fd # Force delete + +# Find lost commits +git reflog + +# Save work in progress +git commit -m "WIP" # Quick save +git reset HEAD~ # Undo WIP commit + +# Update fork with upstream +git remote add upstream +git fetch upstream +git rebase upstream/main +``` \ No newline at end of file