# Git Quick Reference Quick reference guide for common Git operations. ## File Changes and Status ### View Changed Files ```bash # Show working directory status git status # Show short status git status -s # List changed files only (unstaged) git diff --name-only # List changed files only (staged) git diff --cached --name-only # or git diff --staged --name-only # Show file change statistics git diff --stat git diff --cached --stat ``` ### View Detailed Changes ```bash # View unstaged changes git diff # View staged changes git diff --cached # or git diff --staged # View specific file changes git diff git diff --cached # View changes between commits git diff .. git diff HEAD~1..HEAD # View changes between branches git diff main..feature-branch ``` ## Staging and Committing ### Add Files to Staging ```bash # Add specific file git add # Add all files in directory git add . # Add all files in repository git add -A # Add files interactively git add -p # Add only modified files (not new files) git add -u ``` ### Check Staging Area ```bash # List files in staging area git diff --cached --name-only # Show detailed staged changes git diff --cached ``` ### Commit Changes ```bash # Simple commit git commit -m "feat: add user authentication" # Multi-line commit (macOS/Linux) git commit -m "$(cat <<'EOF' feat: add user authentication - Add OAuth2 support - Implement JWT tokens - Add login/logout endpoints EOF )" # Multi-line commit (Windows) git commit -m "feat: add user authentication" \ -m "" \ -m "- Add OAuth2 support" \ -m "- Implement JWT tokens" \ -m "- Add login/logout endpoints" # Commit with automatic staging git commit -am "fix: resolve issue" # Amend last commit (before push only!) git commit --amend -m "new message" ``` ## Tag Management ### Create Tags ```bash # Create annotated tag git tag -a "1.2.0" -m "feat: add new feature" # Create lightweight tag git tag "1.2.0" # Create tag with multi-line message git tag -a "1.2.1" \ -m "fix: resolve connection issue" \ -m "" \ -m "- Increase timeout to 30s" \ -m "- Add retry mechanism" # Create tag for specific commit git tag -a "1.2.0" -m "message" # Monorepo tag git tag -a "ios-1.2.0" -m "feat(ios): add feature" ``` ### List Tags ```bash # List all tags git tag # List tags with pattern git tag -l "v1.*" # List recent tags (sorted) git tag --list | sort -V | tail -5 # Show tag details git show ``` ### Push Tags ```bash # Push single tag git push origin # Push all tags git push --tags # or git push origin --tags # Push commit and tag together git push origin main && git push origin 1.2.0 ``` ### Delete Tags ```bash # Delete local tag git tag -d # Delete remote tag git push origin --delete # or git push origin :refs/tags/ # Delete multiple tags git tag -d tag1 tag2 tag3 ``` ## Branch Operations ### View Branches ```bash # Show current branch git branch --show-current # List local branches git branch # List all branches (local + remote) git branch -a # List remote branches only git branch -r # Show branch with last commit git branch -v ``` ### Create and Switch Branches ```bash # Create new branch git branch # Create and switch to new branch (old way) git checkout -b # Create and switch to new branch (modern) git switch -c # Switch to existing branch (old way) git checkout # Switch to existing branch (modern) git switch # Switch to previous branch git switch - ``` ### Delete Branches ```bash # Delete local branch (safe) git branch -d # Delete local branch (force) git branch -D # Delete remote branch git push origin --delete # or git push origin : ``` ## Pushing and Pulling ### Push Changes ```bash # Push current branch git push # Push to specific remote and branch git push origin main # Push current branch to remote git push origin $(git branch --show-current) # Push with upstream tracking git push -u origin # Push all branches git push --all # Push all tags git push --tags # Force push (dangerous!) git push --force # Better: force push with lease git push --force-with-lease ``` ### Pull Changes ```bash # Pull from tracked remote git pull # Pull from specific remote and branch git pull origin main # Pull with rebase git pull --rebase # Pull and prune deleted remote branches git pull --prune ``` ### Fetch Changes ```bash # Fetch from all remotes git fetch # Fetch from specific remote git fetch origin # Fetch and prune deleted remote branches git fetch --prune # Fetch all branches and tags git fetch --all --tags ``` ## History and Logs ### View Commit History ```bash # View recent commits git log # View compact history git log --oneline # View recent 10 commits git log --oneline -10 # View history with graph git log --graph --oneline --all # View history with stats git log --stat # View history with patches git log -p ``` ### Search History ```bash # Search commits by message git log --grep="feature" # Search by author git log --author="John" # Search by date git log --since="2024-01-01" git log --after="2 weeks ago" git log --before="yesterday" # Search by file git log -- # Search code changes git log -S "function_name" ``` ### View Commit Details ```bash # Show specific commit git show # Show specific tag git show # Show HEAD commit git show HEAD # Show previous commit git show HEAD~1 git show HEAD^ ``` ## Undoing Changes ### Discard Changes ```bash # Discard unstaged changes in file git checkout -- # or (modern) git restore # Discard all unstaged changes git checkout -- . # or (modern) git restore . # Unstage file (keep changes) git reset HEAD # or (modern) git restore --staged # Unstage all files git reset HEAD # or (modern) git restore --staged . ``` ### Reset Commits ```bash # Undo last commit, keep changes staged git reset --soft HEAD~1 # Undo last commit, keep changes unstaged git reset HEAD~1 # or git reset --mixed HEAD~1 # Undo last commit, discard changes (dangerous!) git reset --hard HEAD~1 # Reset to specific commit git reset --hard ``` ### Revert Commits ```bash # Create new commit that undoes a commit git revert # Revert without committing git revert -n # Revert multiple commits git revert .. ``` ## Stash Operations ### Save Changes ```bash # Stash current changes git stash # Stash with message git stash save "work in progress" # Stash including untracked files git stash -u # Stash including untracked and ignored files git stash -a ``` ### Apply Stash ```bash # Apply most recent stash git stash apply # Apply and remove from stash list git stash pop # Apply specific stash git stash apply stash@{2} ``` ### Manage Stash ```bash # List all stashes git stash list # Show stash changes git stash show git stash show -p # Drop specific stash git stash drop stash@{1} # Clear all stashes git stash clear ``` ## Remote Operations ### View Remotes ```bash # List remotes git remote # List remotes with URLs git remote -v # Show remote details git remote show origin ``` ### Manage Remotes ```bash # Add remote git remote add # Remove remote git remote remove # Rename remote git remote rename # Change remote URL git remote set-url ``` ## Configuration ### View Configuration ```bash # View all config git config --list # View global config git config --global --list # View local config git config --local --list # View specific config git config user.name git config user.email ``` ### Set Configuration ```bash # Set user name git config --global user.name "Your Name" # Set user email git config --global user.email "your.email@example.com" # Set default branch name git config --global init.defaultBranch main # Set default editor git config --global core.editor "code --wait" # Set credential helper git config --global credential.helper store ``` ## Workflow Examples ### Standard Commit and Tag Workflow ```bash # 1. Check status git status git diff --cached --name-only # 2. Stage changes git add . # 3. Commit git commit -m "feat: add user authentication" # 4. Create tag git tag -a "1.2.0" -m "feat: add user authentication" # 5. Push commit and tag git push origin main git push origin 1.2.0 ``` ### Complete Staging to Push Workflow ```bash # Check what files changed git status # View changes git diff # Stage specific files git add src/auth.js src/api.js # Verify staging git diff --cached --name-only # Commit with message git commit -m "feat: implement OAuth2 authentication" # Push to remote git push origin main ``` ### Push All Tags Workflow ```bash # List local tags git tag # View recent tags git tag --list | sort -V | tail -5 # Push all tags to remote git push --tags # Verify tags on remote git ls-remote --tags origin ``` ### Quick Status Check ```bash # Full status git status # Changed files only git diff --name-only git diff --cached --name-only # Recent commits and tags git log --oneline -5 git tag --list | sort -V | tail -5 # Current branch git branch --show-current ``` ## Tips and Tricks ### Aliases Add to `~/.gitconfig`: ```ini [alias] st = status co = checkout br = branch ci = commit unstage = restore --staged last = log -1 HEAD lg = log --graph --oneline --all tags = tag -l --sort=-v:refname ``` Usage: ```bash git st git co main git lg ``` ### Useful One-Liners ```bash # Delete all merged branches git branch --merged | grep -v "\*" | xargs -n 1 git branch -d # View file in specific commit git show : # Count commits by author git shortlog -sn # Find when a line was changed git blame # Show what changed in each commit for a file git log -p # List files in a commit git diff-tree --no-commit-id --name-only -r ```