Compare commits

...

2 Commits

Author SHA1 Message Date
Felipe Cardoso
bf88b9b7b5 Merge remote-tracking branch 'origin/main' 2025-02-14 14:58:13 +01:00
Felipe Cardoso
53c5732757 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.
2025-02-14 14:58:08 +01:00

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
```