# Git Cheatsheet ## Repository Setup & Remote Operations ```bash # Clone a repository git clone # Clone a repository with branch checkout git clone -b branch_name # 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 ```