The Ultimate Git Cheatsheet: Master Version Control in 2025

7 min read
Updated:
DevTechTools Team

The Ultimate Git Cheatsheet: Master Version Control in 2025

Git is the backbone of modern software development, yet many developers still struggle with its more advanced features. Whether you're a beginner just starting out or an experienced developer looking to refresh your memory, this comprehensive Git cheatsheet will help you navigate version control with confidence.

What You'll Learn

This cheatsheet covers everything from basic Git commands to advanced workflows, including branching strategies, conflict resolution, and collaborative development techniques. By the end of this guide, you'll have a solid reference for all your Git needs.

Getting Started with Git

Initial Setup

bash
# Configure your identity
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Check your configuration
git config --list

# Initialize a new repository
git init

# Clone an existing repository
git clone https://github.com/username/repository.git

Basic Repository Operations

bash
# Check repository status
git status

# Add files to staging area
git add filename.txt          # Add specific file
git add .                     # Add all files
git add *.js                  # Add all JavaScript files

# Commit changes
git commit -m "Your commit message"
git commit -am "Add and commit in one step"

# View commit history
git log
git log --oneline            # Compact view
git log --graph              # Visual representation

Working with Branches

Branches are one of Git's most powerful features, allowing you to work on different features simultaneously without affecting the main codebase.

Branch Management

bash
# List all branches
git branch                   # Local branches
git branch -r               # Remote branches
git branch -a               # All branches

# Create and switch to a new branch
git checkout -b feature-branch
git switch -c feature-branch  # Modern alternative

# Switch between branches
git checkout main
git switch main              # Modern alternative

# Delete a branch
git branch -d feature-branch  # Safe delete
git branch -D feature-branch  # Force delete

# Rename current branch
git branch -m new-branch-name

Merging and Rebasing

bash
# Merge a branch into current branch
git merge feature-branch

# Rebase current branch onto another
git rebase main

# Interactive rebase (last 3 commits)
git rebase -i HEAD~3

# Abort a merge or rebase
git merge --abort
git rebase --abort

Remote Repository Operations

Working with remote repositories is essential for collaboration and backup.

Remote Management

bash
# List remote repositories
git remote -v

# Add a remote repository
git remote add origin https://github.com/username/repo.git

# Push changes to remote
git push origin main
git push -u origin main      # Set upstream branch
git push --all              # Push all branches

# Pull changes from remote
git pull origin main
git pull --rebase           # Pull with rebase instead of merge

# Fetch changes without merging
git fetch origin
git fetch --all             # Fetch from all remotes

Undoing Changes

Mistakes happen, and Git provides several ways to undo changes safely.

Undoing Local Changes

bash
# Discard changes in working directory
git checkout -- filename.txt
git restore filename.txt     # Modern alternative

# Unstage files
git reset HEAD filename.txt
git restore --staged filename.txt  # Modern alternative

# Reset to previous commit
git reset --soft HEAD~1     # Keep changes staged
git reset --mixed HEAD~1    # Keep changes unstaged (default)
git reset --hard HEAD~1     # Discard all changes

# Revert a commit (creates new commit)
git revert commit-hash

Advanced Undo Operations

bash
# Interactive reset
git reset -i HEAD~3

# Cherry-pick a commit
git cherry-pick commit-hash

# Create a new branch from a previous commit
git checkout -b new-branch commit-hash

# Temporarily save changes
git stash
git stash pop               # Apply and remove from stash
git stash list              # List all stashes
git stash apply stash@{0}   # Apply specific stash

Viewing and Comparing Changes

Understanding what changed and when is crucial for effective version control.

Inspection Commands

bash
# Show changes in working directory
git diff

# Show staged changes
git diff --staged
git diff --cached           # Alternative

# Compare branches
git diff main..feature-branch

# Show commit details
git show commit-hash

# Search commit messages
git log --grep="bug fix"

# Search code changes
git log -S "function name"  # Pickaxe search

Advanced Log Options

bash
# Compact log with graph
git log --oneline --graph --decorate --all

# Show files changed in each commit
git log --stat

# Show actual changes
git log -p

# Filter by date
git log --since="2023-01-01" --until="2023-12-31"

# Filter by author
git log --author="John Doe"

Collaborative Workflows

Working with Pull Requests

bash
# Create a feature branch
git checkout -b feature/new-feature

# Push branch to remote
git push -u origin feature/new-feature

# After PR is merged, clean up
git checkout main
git pull origin main
git branch -d feature/new-feature
git push origin --delete feature/new-feature

Handling Merge Conflicts

bash
# When conflicts occur during merge
git status                  # See conflicted files
# Edit files to resolve conflicts
git add conflicted-file.txt # Mark as resolved
git commit                  # Complete the merge

# Use merge tools
git mergetool

# Abort merge if needed
git merge --abort

Advanced Git Techniques

Git Hooks

bash
# Navigate to hooks directory
cd .git/hooks

# Common hooks (remove .sample extension to activate):
# pre-commit       - Run before commit
# post-commit      - Run after commit
# pre-push         - Run before push
# post-receive     - Run on remote after receive

Submodules

bash
# Add a submodule
git submodule add https://github.com/user/repo.git path/to/submodule

# Initialize submodules after cloning
git submodule init
git submodule update

# Or in one command
git submodule update --init --recursive

# Update submodules
git submodule update --remote

Git Aliases

Save time with custom Git aliases:

bash
# Set up useful aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual '!gitk'

# Advanced aliases
git config --global alias.lg 'log --oneline --graph --decorate --all'
git config --global alias.contributors 'shortlog -sn'

Git Best Practices

Commit Message Guidelines

  • Use present tense ("Add feature" not "Added feature")
  • Keep first line under 50 characters
  • Capitalize the first letter
  • Use body to explain what and why, not how

Branch Naming Conventions

text
feature/user-authentication
bugfix/login-error
hotfix/security-patch
release/v2.1.0

Workflow Tips

  • Commit often: Small, focused commits are easier to understand and revert
  • Use branches: Keep main/master stable
  • Write meaningful commit messages: Your future self will thank you
  • Review before pushing: Use git diff --staged before committing
  • Keep history clean: Use interactive rebase to squash commits when appropriate
  • Troubleshooting Common Issues

    "Detached HEAD" State

    bash
    # Create a branch from detached HEAD
    git checkout -b new-branch-name
    
    # Or discard changes and return to main
    git checkout main

    Accidentally Committed to Wrong Branch

    bash
    # Move last commit to new branch
    git branch new-branch
    git reset --hard HEAD~1
    git checkout new-branch

    Large File Issues

    bash
    # Remove large file from history
    git filter-branch --tree-filter 'rm -rf path/to/large/file' HEAD
    
    # Or use BFG Repo-Cleaner (recommended)
    java -jar bfg.jar --delete-files large-file.zip repo.git

    Conclusion

    Git is a powerful tool that becomes more valuable as you master its features. This cheatsheet covers the most common and useful Git commands you'll encounter in your development workflow. Keep this reference handy, and don't be afraid to experiment with Git's features in a safe environment.

    Remember: Git's greatest strength is its ability to track and manage changes safely. When in doubt, create a branch, commit often, and use Git's extensive help system with git help .

    Happy coding, and may your merges be conflict-free!

    Found this article helpful?

    Share it with others who might benefit from it.

    More Articles