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.
This commit is contained in:
2025-02-14 11:14:32 +01:00
parent 80e7a39e8a
commit 53c5732757
2 changed files with 232 additions and 0 deletions

75
.gitignore vendored Normal file
View File

@@ -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/

157
cheat-sheets/git.md Normal file
View File

@@ -0,0 +1,157 @@
# Git Cheatsheet
## Repository Setup & Remote Operations
```bash
# Clone a repository
git clone <repository-url>
# Add a remote
git remote add <name> <url>
git remote add origin git@github.com:user/repo.git
# Change remote URL
git remote set-url origin <new-url>
# View remotes
git remote -v
```
## Basic Commands
```bash
# Check status
git status
# Stage files
git add <file> # 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 <branch>
git pull origin <branch>
git pull --rebase origin <branch> # 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 <branch-name>
git checkout -b <branch-name> # Create and switch
# Checkout remote branch (create if not exists)
git checkout -b <local-branch> origin/<remote-branch>
git checkout --track origin/<remote-branch> # Shorter version
# Switch branch
git checkout <branch-name>
# Delete branch
git branch -d <branch-name> # Local branch
git push origin --delete <branch-name> # Remote branch
```
## Working with Changes
```bash
# View changes
git diff # Unstaged changes
git diff --staged # Staged changes
git diff <branch1> <branch2> # Between branches
# Discard changes
git checkout -- <file> # Discard file changes
git restore <file> # 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> # File history
# Search history
git log --grep="keyword" # Search commits
git log -S"code" # Search code changes
```
## Merge and Rebase
```bash
# Merge
git merge <branch>
git merge --abort # Cancel merge
# Rebase
git rebase <branch>
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 <commit-hash>
# 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 <original-repo-url>
git fetch upstream
git rebase upstream/main
```