Git Command Reference
Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency. This guide covers the essential Git commands for daily development work.
Getting Started
Installation
Download and install Git from the official website.
Basic Configuration
Set Your Identity
Configure your username and email address:
# Set your Git username globally
git config --global user.name "Your Name"
# Set your Git email address globally
git config --global user.email "youremail@example.com"
Check Your Configuration
# View all configuration settings
git config --list
# View specific configuration
git config user.name
git config user.email
Additional Useful Configuration
# Set default editor
git config --global core.editor "code --wait" # For VS Code
git config --global core.editor "vim" # For Vim
# Set default branch name
git config --global init.defaultBranch main
# Enable colored output
git config --global color.ui auto
# Set up line ending handling
git config --global core.autocrlf input # On macOS/Linux
git config --global core.autocrlf true # On Windows
Repository Setup
Initialize a New Repository
# Initialize a new Git repository in current directory
git init
# Initialize with a specific branch name
git init --initial-branch=main
git init -b main
Clone an Existing Repository
# Clone a repository
git clone <repository_URL>
# Clone into a specific directory
git clone <repository_URL> <directory_name>
# Clone a specific branch
git clone -b <branch_name> <repository_URL>
# Shallow clone (only recent history)
git clone --depth 1 <repository_URL>
Working with Changes
Check Repository Status
# Show the status of working directory and staging area
git status
# Show status in short format
git status -s
git status --short
Viewing Changes
# Show unstaged changes
git diff
# Show staged changes
git diff --staged
git diff --cached
# Show changes between commits
git diff <commit1> <commit2>
# Show changes for a specific file
git diff <file_name>
Staging Changes
# Stage a specific file
git add <file_name>
# Stage all changes in current directory
git add .
# Stage all changes in repository
git add -A
git add --all
# Stage only modified files (not new files)
git add -u
git add --update
# Interactive staging
git add -i
git add --interactive
# Stage parts of a file
git add -p <file_name>
git add --patch <file_name>
Unstaging Changes
# Unstage a file (keep changes in working directory)
git restore --staged <file_name>
git reset HEAD <file_name> # Older syntax
# Unstage all files
git restore --staged .
git reset HEAD . # Older syntax
Discarding Changes
# Discard changes in working directory
git restore <file_name>
git checkout -- <file_name> # Older syntax
# Discard all changes in working directory
git restore .
git checkout -- . # Older syntax
# Remove untracked files
git clean -f
# Remove untracked files and directories
git clean -fd
# Preview what would be removed
git clean -n
Committing Changes
Basic Commits
# Commit staged changes with a message
git commit -m "Your commit message"
# Commit all tracked files (skip staging)
git commit -a -m "Your commit message"
git commit -am "Your commit message"
# Commit with a multi-line message
git commit # Opens editor for message
Advanced Commit Options
# Modify the last commit (message or files)
git commit --amend
# Modify last commit without changing message
git commit --amend --no-edit
# Commit with detailed message
git commit -m "Short description" -m "Longer detailed description"
# Create an empty commit
git commit --allow-empty -m "Empty commit message"
Remote Repositories
Managing Remotes
# Add a remote repository
git remote add origin <repository_URL>
# View remote repositories
git remote -v
# Change remote URL
git remote set-url origin <new_repository_URL>
# Remove a remote
git remote remove origin
Syncing with Remotes
# Fetch changes from remote (don't merge)
git fetch
# Fetch from specific remote
git fetch origin
# Pull changes (fetch + merge)
git pull
# Pull from specific remote and branch
git pull origin main
# Pull with rebase instead of merge
git pull --rebase
# Push changes to remote
git push
# Push to specific remote and branch
git push origin main
# Push and set upstream branch
git push -u origin main
git push --set-upstream origin main
# Force push (use with caution!)
git push --force
git push -f
Branch Management
Creating and Switching Branches
# List all local branches
git branch
# List all branches (local and remote)
git branch -a
# Create a new branch
git branch <branch_name>
# Switch to a branch
git checkout <branch_name>
git switch <branch_name> # Newer syntax
# Create and switch to new branch
git checkout -b <branch_name>
git switch -c <branch_name> # Newer syntax
# Create branch from specific commit
git checkout -b <branch_name> <commit_hash>
Branch Operations
# Rename current branch
git branch -m <new_branch_name>
# Rename a specific branch
git branch -m <old_name> <new_name>
# Delete a local branch
git branch -d <branch_name>
# Force delete a local branch
git branch -D <branch_name>
# Delete a remote branch
git push origin --delete <branch_name>
# Track a remote branch
git branch --set-upstream-to=origin/<branch_name>
Merging and Rebasing
Merging
# Merge a branch into current branch
git merge <branch_name>
# Merge without fast-forward
git merge --no-ff <branch_name>
# Abort a merge in progress
git merge --abort
# Create merge commit even for fast-forward
git merge --no-ff <branch_name>
Rebasing
# Rebase current branch onto another branch
git rebase <branch_name>
# Interactive rebase
git rebase -i <commit_hash>
git rebase -i HEAD~3 # Last 3 commits
# Abort rebase
git rebase --abort
# Continue rebase after resolving conflicts
git rebase --continue
# Skip current commit during rebase
git rebase --skip
Viewing History
Log Commands
# View commit history
git log
# One line per commit
git log --oneline
# Show branch graph
git log --graph --oneline --all
# Show commits with file changes
git log --stat
# Show commits with actual changes
git log -p
git log --patch
# Limit number of commits shown
git log -5
git log -n 5
# Show commits since a date
git log --since="2023-01-01"
git log --after="2023-01-01"
# Show commits by author
git log --author="Your Name"
# Search commit messages
git log --grep="bug fix"
Show Specific Commits
# Show details of a specific commit
git show <commit_hash>
# Show files changed in a commit
git show --name-only <commit_hash>
# Show file content at specific commit
git show <commit_hash>:<file_path>
Undoing Changes
Reset Commands
# Unstage files (keep changes)
git reset
# Reset to specific commit (keep changes)
git reset <commit_hash>
# Reset and unstage all changes
git reset --mixed <commit_hash>
# Reset and discard all changes
git reset --hard <commit_hash>
# Reset but keep changes staged
git reset --soft <commit_hash>
Revert Commands
# Create new commit that undoes a previous commit
git revert <commit_hash>
# Revert without creating commit
git revert --no-commit <commit_hash>
# Revert a merge commit
git revert -m 1 <merge_commit_hash>
Stashing
Basic Stashing
# Stash current changes
git stash
# Stash with a message
git stash push -m "Work in progress"
# List all stashes
git stash list
# Apply most recent stash
git stash apply
# Apply and remove most recent stash
git stash pop
# Apply a specific stash
git stash apply stash@{2}
# Drop a stash
git stash drop stash@{1}
# Clear all stashes
git stash clear
Advanced Stashing
# Stash including untracked files
git stash -u
git stash --include-untracked
# Stash only specific files
git stash push <file1> <file2>
# Create branch from stash
git stash branch <branch_name> stash@{1}
Tags
Creating Tags
# Create lightweight tag
git tag <tag_name>
# Create annotated tag
git tag -a <tag_name> -m "Tag message"
# Tag a specific commit
git tag <tag_name> <commit_hash>
# List all tags
git tag
# Show tag information
git show <tag_name>
Managing Tags
# Push tags to remote
git push origin <tag_name>
git push origin --tags
# Delete local tag
git tag -d <tag_name>
# Delete remote tag
git push origin --delete <tag_name>
Useful Aliases
Add these to your Git configuration for faster workflows:
# Set up useful aliases
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'
git config --global alias.lg "log --oneline --graph --all"
Troubleshooting
Common Issues
Resolve Merge Conflicts
# After resolving conflicts in files
git add <resolved_file>
git commit
# Or abort the merge
git merge --abort
Fix Commit Messages
# Fix last commit message
git commit --amend -m "New commit message"
# Fix older commit messages (interactive rebase)
git rebase -i HEAD~3
Recover Deleted Commits
# Find lost commits
git reflog
# Recover a specific commit
git checkout <commit_hash>
git cherry-pick <commit_hash>
Useful Investigation Commands
# Find when a line was changed
git blame <file_name>
# Search for text in commit history
git log -S "search_term"
# Find commits that touch a specific file
git log --follow <file_name>
# See what changed between branches
git diff main..feature-branch
Best Practices
Commit Message Guidelines
- Use imperative mood ("Add feature" not "Added feature")
- Keep first line under 50 characters
- Add detailed description after blank line if needed
- Reference issue numbers when applicable
Workflow Tips
- Pull before push: Always pull latest changes before pushing
- Use branches: Create feature branches for new work
- Commit often: Make small, logical commits
- Review before commit: Check
git diff --staged
before committing - Use
.gitignore
: Ignore files that shouldn't be tracked
Example .gitignore
# Dependencies
node_modules/
*.log
# Build outputs
dist/
build/
# IDE files
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# Environment files
.env
.env.local