Environment Setup & Configuration
# Retrieve current configurations
git config --list
git config --global --list
# Define user identity (mandatory)
git config --global user.name "Jane Doe"
git config --global user.email "jane.doe@domain.com"
# Specify default text editor
git config --global core.editor "nano" # Nano
git config --global core.editor "code --wait" # VS Code
# Create shortcuts for efficiency
git config --global alias.sw switch
git config --global alias.rs restore
git config --global alias.cm commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
# Enable credential caching to avoid repeated password entry
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=7200'
Repository Initialization & Cloning
# Bootstrap a new project
git init # Initialize inside current folder
git init /path/to/project # Initialize in a specific directory
# Download remote repositories (frequent operations)
git clone https://git.example.com/app.git # Clone default branch
git clone https://git.example.com/app.git local_dir # Clone into a custom folder
git clone -b dev https://git.example.com/app.git # Fetch a specific branch
git clone --depth 1 https://git.example.com/app.git # Shallow clone (latest commit only)
git clone --recurse-submodules https://git.example.com/app.git # Fetch with submodules
# Inspect repository state
git status # Full overview
git status -s # Condensed output
Core Workflow Commands
Staging Modifications
git add index.html # Stage a specific file
git add . # Stage all new and modified files (excludes deletions)
git add -A # Stage all changes (includes deletions)
git add -u # Stage modifications to tracked files only
git add -p # Interactive staging mode
Saving Snapshots
git commit -m "feat: implement user authentication" # Commit staged files
git commit -a -m "chore: update dependencies" # Auto-stage tracked files and commit
git commit # Open editor for a detailed message
git commit --amend # Replace the most recent commit
git commit --amend -m "docs: update readme" # Alter the last commit message
git commit --amend --no-edit # Add staged changes to the last commit without changing message
Integrating Remote Updates
git pull # Fetch and merge (equivalent to fetch + merge)
git pull --rebase # Fetch and rebase (prevents merge commits)
git pull upstream main # Pull from a specific remote and branch
Publishing Local Commits
git push # Push to the linked remote
git push -u origin main # Push and establish tracking link
git push --force # Overwrite remote history (use with extreme caution)
git push --force-with-lease # Safer force push
git push origin feature/login # Push a specific branch
git push --all # Push all local branches
git push --tags # Push all local tags
Branch Management
# Listing branches
git branch # Show local branches
git branch -a # Show local and remote branches
git branch -r # Show remote branches exclusively
git branch -v # Display branches with latest commit
# Creating and navigating
git branch feature/ui # Create a new branch
git checkout feature/ui # Switch to an existing branch
git checkout -b feature/api # Create and switch simultaneously
git switch feature/ui # Modern alternative to checkout (Git 2.23+)
git switch -c feature/api # Modern alternative to create and switch
# Merging lines of development
git merge feature/ui # Merge target branch into current
git merge --no-ff feature/api # Force a merge commit to preserve history
git merge --abort # Cancel a conflicting merge
# Removing branches
git branch -d feature/ui # Delete a fully merged branch
git branch -D feature/api # Force delete an unmerged branch
# Renaming branches
git branch -m old-name new-name # Rename a branch
git branch -M old-name new-name # Force rename a branch
# Comparing branches
git diff main feature/ui # View divergent changes
git diff main feature/ui -- src/app.js # Compare a specific file across branches
Remote Synchronization
# Inspecting remotes
git remote # List remote handles
git remote -v # Show remote URLs
# Modifying remote connections
git remote add origin https://git.example.com/app.git # Link a remote repository
git remote remove origin # Unlink a remote
git remote rename origin upstream # Change remote handle
git remote set-url origin https://new.url/app.git # Update remote URL
# Fetching without merging
git fetch # Download all remote branch updates
git fetch upstream # Download updates from a specific remote
git fetch --prune # Remove stale remote-tracking references
# Tracking remote references
git branch --track local-branch origin/remote-branch # Set local to track remote
git branch --set-upstream-to=origin/main # Update tracking for current branch
git checkout --track origin/remote-branch # Create local branch tracking remote
Inspecting History & Differences
# Exploring commit logs
git log # Exhaustive commit history
git log --oneline # Condensed hash and message
git log --graph # Visual representation of branches
git log --graph --oneline --decorate --all # Comprehensive overview
git log -3 # Show the last three commits
git log --author="Jane" # Filter by contributor
git log --since="1 week ago" # Filter by date
git log --grep="authentication" # Search commit messages
git log -- src/app.js # History of a specific file
git log -p # Display patch diff for each commit
git log --stat # Show file change statistics
# Analyzing differences
git diff # Unstaged changes vs staging area
git diff --staged # Staged changes vs repository
git diff HEAD # Working directory vs latest commit
git diff 1a2b3c 4d5e6f # Difference between two commits
# Investigating commits
git show 1a2b3c # Metadata and diff of a commit
git show 1a2b3c:src/app.js # File contents at a specific commit
Undoing Changes & Reverting
# Discarding local modifications
git checkout -- src/app.js # Revert file in working directory (legacy)
git restore src/app.js # Modern approach (Git 2.23+)
# Unstaging files
git reset HEAD src/app.js # Remove from staging area (legacy)
git restore --staged src/app.js # Modern approach (Git 2.23+)
# Reversing commits
git reset --soft 1a2b3c # Move HEAD back, keep changes staged
git reset --mixed 1a2b3c # Move HEAD back, keep changes in working directory
git reset --hard 1a2b3c # Wipe out commits and all changes (destructive)
# Recovering lost states
git reflog # Log of where HEAD has pointed
git reset --hard 1a2b3c # Restore using a reflog reference
# Countermanding pushed commits
git revert 1a2b3c # Create a compensating commit (safe for shared history)
git revert --no-commit 1a2b3c # Stage the revert without committing
# Shelving progress
git stash # Temporarily store dirty working directory
git stash push -m "WIP: auth module" # Stash with a description
git stash list # View all stashes
git stash pop # Apply latest stash and delete it
git stash apply # Apply latest stash but retain it
git stash drop # Discard the latest stash
git stash clear # Erase all stashes
Tagging Releases
# Browsing tags
git tag # List all tags
git tag -l "v2.*" # Filter tags using a pattern
# Creating tags
git tag v2.0.0 # Lightweight tag
git tag -a v2.0.0 -m "Release 2.0" # Annotated tag (recommended)
git tag -a v1.5.0 1a2b3c # Tag a historical commit
# Distributing tags
git push origin v2.0.0 # Upload a single tag
git push --tags # Upload all local tags
# Removing tags
git tag -d v2.0.0 # Delete locally
git push origin :refs/tags/v2.0.0 # Delete remotely
# Accessing tagged states
git checkout v2.0.0 # Inspect in detached HEAD state
git checkout -b fix/v2.0.0 v2.0.0 # Create a branch from a tag
Advanced Techniques
# Reapplying commits (Rebase)
git rebase main # Rebase current branch onto main
git rebase -i HEAD~4 # Alter the last four commits interactively
git rebase --abort # Escape a rebase
git rebase --continue # Proceed after resolving conflicts
# Transplanting commits
git cherry-pick 1a2b3c # Apply a specific commit to current branch
git cherry-pick --abort # Cancel the cherry-pick
# Maintenance tasks
git gc # Optimize repository size
git fsck # Validate repository integrity
git prune # Purge unreachable objects
# Managing submodules
git submodule add https://git.example.com/lib.git # Attach a submodule
git submodule init # Initialize local config
git submodule update # Fetch submodule data
git submodule sync # Update remote URLs
# Parallel workspaces (Worktree)
git worktree add ../hotfix-dir main # Create a linked working tree
git worktree remove ../hotfix-dir # Delete a worktree
git worktree list # Show all linked worktrees
# Searching codebase
git grep "TODO" # Find string in tracked files
git grep -n "TODO" # Include line numbers
git grep --cached "TODO" # Search staged content
Troubleshooting & Recovery
# Diagnosing state
git status # Verify working tree condition
git reflog # Locate lost commits
git fsck --lost-found # Retrieve dangling objects
# Resolving merge conflicts
git status # Identify conflicting files
# Manually adjust conflict markers in files
git add src/app.js # Mark conflict as resolved
git commit # Finalize the merge
# System checks
git --version # Confirm installed version
git help reset # Access manual for a command
git config --list # Audit active settings
git config --global --edit # Modify global configuration directly
# Updating connectivity
git remote set-url origin https://new.url/app.git # Point origin to a new address
Workflow Blueprints
Standard Feature Development
# 1. Sync local environment
git clone https://git.example.com/app.git # Initial setup
git pull --rebase # Fetch latest changes
# 2. Isolate new work
git checkout -b feature/dashboard
# 3. Record progress
git add src/dashboard.js
git commit -m "feat: build dashboard layout"
# 4. Share with team
git push -u origin feature/dashboard
# 5. Integrate via Pull Request or local merge
git checkout main
git merge feature/dashboard
git push
Urgent Production Fix
# 1. Prepare the patch environment
git checkout main
git pull --rebase
git checkout -b hotfix/security-patch
# 2. Apply the correction
git add src/auth.js
git commit -m "fix: patch authentication bypass"
# 3. Deploy and tag
git checkout main
git merge hotfix/security-patch
git tag v2.0.1
git push origin main --tags