This update includes an example of how to clone a specific branch using `git clone`. It enhances the cheat sheet by providing more practical git use cases.
3.8 KiB
3.8 KiB
Git Cheatsheet
Repository Setup & Remote Operations
# Clone a repository
git clone <repository-url>
# Clone a repository with branch checkout
git clone -b branch_name <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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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