
I’ve aimed to create this Git commands cheatsheet as a comprehensive one-stop reference, and I’m confident that you’ll find it to be the most complete and concise Git commands reference you’ve ever encountered. Bookmark this page right now because you’re gonna need it – trust me on this!
Git is the backbone of modern software development, and knowing these commands will make you 10x more productive. I’ve compiled this massive list of Git commands that I use daily and organized them so you can find exactly what you need when you need it.
Table of contents
Setting Up Git
Every Git journey starts with a proper setup. These commands will help you get started:
# Initialize a new Git repository in current directory
git init
# Set your username globally (for all repositories)
git config --global user.name "Your Name"
# Set your email globally (for all repositories)
git config --global user.email "[email protected]"
# Set username for just the current repository
git config user.name "Your Name"
# Set email for just the current repository
git config user.email "[email protected]"
# Check your current Git configuration
git config --list
BashNever worry about setting up Git again! These commands cover everything you need for a perfect setup.
Basic Git Commands
These are the commands you’ll use constantly throughout your development workflow:
# Add a specific file to staging area
git add filename.txt
# Add all modified and new files to staging area
git add .
# Add only files that are already tracked (modified files, not new ones)
git add -u
# Commit staged changes with a message
git commit -m "Your descriptive commit message"
# Combine add and commit for tracked files
git commit -am "Update files and commit in one step"
# Check the status of your working directory
git status
# View the abbreviated status output
git status -s
# See the commit history
git log
# See a more condensed commit history
git log --oneline
# Show commit history with graphical representation of branches
git log --graph --oneline --decorate
BashI use these commands literally hundreds of times a day. They’re the building blocks of everything you’ll do with Git.
Working with Branches
Branching is Git’s superpower! Here’s how to wield it like a pro:
# List all local branches
git branch
# List all branches (local and remote)
git branch -a
# Create a new branch
git branch branch-name
# Create and switch to a new branch
git checkout -b branch-name
# Switch to an existing branch
git checkout branch-name
# Switch to previous branch
git checkout -
# Rename current branch
git branch -m new-branch-name
# Delete a branch (if fully merged)
git branch -d branch-name
# Force delete a branch (even if not merged)
git branch -D branch-name
# Merge another branch into your current branch
git merge branch-name
# Rebase your current branch onto another branch
git rebase branch-name
BashBranching is critical to any successful Git workflow. Master these commands and you’ll be a Git wizard in no time!
Tip💡: Make sure you understand git merge vs rebase well to pick the right one that works best for you.
Remote Repository Operations
Working with remote repositories is essential for team collaboration. Here are the commands you need:
# Clone a remote repository to your local machine
git clone username@host:/path/to/repository
# View remote repositories
git remote -v
# Add a remote repository
git remote add origin <remote-repo-url>
# Remove a remote repository
git remote remove origin
# Fetch changes from remote (doesn't merge)
git fetch origin
# Pull changes from remote (fetches and merges)
git pull origin branch-name
# Pull changes with rebase instead of merge
git pull --rebase origin branch-name
# Push changes to remote repository
git push origin branch-name
# Push and set upstream for branch
git push -u origin branch-name
# Push all branches to remote
git push --all origin
BashThese commands connect your local work to the wider-world—absolutely critical for team collaboration.
Inspecting & Comparing
Need to see what’s changed? These commands have got you covered:
# View changes between working directory and staging area
git diff
# View changes between staging area and last commit
git diff --staged
# View changes between two commits
git diff commit1..commit2
# Show changes in a specific file
git diff -- filename
# Show changes with statistical summary
git diff --stat
# Show the contents of a file at a specific commit
git show commit:filename
# Show commit details
git show commit-hash
BashThese inspection commands help you understand exactly what’s happening in your repository at all times.
Tip 💡: Want to check diff online without having to set up a repo? Try our diff checker tool!
Undoing Changes
Everyone makes mistakes! These commands will save your bacon:
# Discard changes in working directory for a specific file
git checkout -- filename
# Restore a file to version from a specific commit
git checkout commit-hash -- filename
# Unstage a file but keep changes
git restore --staged filename
# Create a new commit that undoes changes from a previous commit
git revert commit-hash
# Reset current HEAD to specified state (--soft keeps changes, --hard discards)
git reset --soft HEAD~1 # Undo last commit, keep changes staged
git reset --mixed HEAD~1 # Undo last commit, keep changes unstaged (default)
git reset --hard HEAD~1 # Undo last commit, discard changes completely
# Temporarily save changes without committing
git stash
# List stashed changes
git stash list
# Apply most recent stash and remove it
git stash pop
# Apply most recent stash but keep it in stash list
git stash apply
BashThese commands are lifesavers when you need to recover from mistakes or explore different approaches.
Advanced Git Operations
Ready to level up?
While you may need these the least from this entire git commands cheatsheet reference, when you do, these advanced commands will make you a Git power user:
# Interactive rebase for editing commit history
git rebase -i HEAD~3 # Rebase last 3 commits
# Cherry-pick a commit from another branch
git cherry-pick commit-hash
# Create and apply a patch
git format-patch -1 commit-hash
git apply patch-file
# Clean untracked files and directories
git clean -fd
# Search commit history for a string
git log -S "search string"
# Find who modified each line in a file
git blame filename
# Archive a repository
git archive --format=zip HEAD > project.zip
# Count lines of code in repository
git ls-files | xargs wc -l
BashOnce you master these advanced operations, there’s no Git challenge you can’t handle!
Git Configuration Tricks
Customize Git to work exactly how you want:
# Set default branch name for new repositories
git config --global init.defaultBranch main
# Set up git aliases for common commands
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
# Set up automatic correction of mistyped commands
git config --global help.autocorrect 1
# Configure Git to handle line endings
git config --global core.autocrlf input # For macOS/Linux
git config --global core.autocrlf true # For Windows
# Set up a global gitignore file
git config --global core.excludesfile ~/.gitignore_global
BashThese configuration tricks will save you time and make Git feel like it was custom-built for you.
Git Workflow Tips
Follow these workflow tips to maximize productivity:
- Commit often – Small, focused commits are easier to understand and revert if needed
- Write meaningful commit messages – Future you will thank present you
- Use branches for features/fixes – Keep your work organized and isolated
- Pull before you push – Avoid unnecessary merge conflicts
- Use .gitignore properly – Don’t track unnecessary files
- Review changes before committing – Use
git diff
to verify what you’re about to commit - Leverage Git hooks – Automate checks and processes
Pro Tip 💡: Check out our comprehensive GIT Best Practices
Troubleshooting Common Issues
Run into problems? Here are solutions for some common Git headaches:
Merge Conflicts
# Abort a merge in progress
git merge --abort
# Use a GUI tool to help resolve conflicts
git mergetool
BashAuthentication Issues
# Cache credentials for 15 minutes
git config --global credential.helper cache
# Store credentials permanently
git config --global credential.helper store
BashLarge Files Problems
# Find large files in your repository
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print $1}')"
# Use Git LFS for large files
git lfs install
git lfs track "*.psd"
BashThere you have it – the ultimate Git commands cheat sheet! Keep this bookmarked and you’ll never struggle with Git again. Here’s the official documentation link in case you’re interested in diving even deeper.
Have a Git question that’s not covered here? Drop a comment below!
Remember: Git is powerful and also forgiving – even if you mess up, there’s usually a way to recover your work!
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
[…] between Git branches, you have exactly two options: merge and rebase. The choice between these git commands, whether to use git merge vs rebase, fundamentally shapes your Git workflow and the history of your […]