Git Cheat Sheet
A complete reference of Git commands with examples and usage instructions. Find the command you need using the search bar or browse by category.
📚 Practice what you've learned with our Git Flashcards
git init
SetupInitialize a new Git repository
Syntax:
git init [directory]Examples:
git init Initialize repository in current directorygit init my-project Initialize repository in my-project directoryNotes:
Creates a .git subdirectory with repository files
git clone
SetupClone a repository into a new directory
Syntax:
git clone [url] [directory]Examples:
git clone https://github.com/user/repo.git Clone remote repositorygit clone https://github.com/user/repo.git my-folder Clone into specific foldergit clone --depth=1 https://github.com/user/repo.git Clone only the latest revisionNotes:
Creates a copy of a repository with full history
git config
SetupGet and set repository or global options
Syntax:
git config [--global] [name] [value]Examples:
git config --global user.name "Your Name" Set global usernamegit config --global user.email "email@example.com" Set global emailgit config --list List all configuration settingsNotes:
Use --global to apply settings for all repositories for the user
git help
SetupDisplay help information about Git commands
Syntax:
git help [command]Examples:
git help Show general helpgit help commit Show help for commit commandgit help -a List all available commandsgit help -g List concept guidesNotes:
Opens help documentation in browser or terminal
git version
SetupDisplay the version of Git
Syntax:
git versionExamples:
git version Show Git version informationgit --version Alternative way to show versionNotes:
Useful for checking which Git version is installed
git add
BasicAdd file contents to the index (staging area)
Syntax:
git add [file|pattern]Examples:
git add file.txt Add a specific filegit add . Add all new and modified filesgit add -p Interactive add (choose chunks to stage)Notes:
Prepares content for the next commit
git commit
BasicRecord changes to the repository
Syntax:
git commit -m "[message]"Examples:
git commit -m "Add feature X" Commit with messagegit commit -a -m "Fix bugs" Add all tracked files and commitgit commit --amend Modify the last commitNotes:
Creates a snapshot of staged changes
git status
BasicShow the working tree status
Syntax:
git status [options]Examples:
git status Display full statusgit status -s Display short statusNotes:
Shows which files are modified, staged, or untracked
git log
BasicShow commit logs
Syntax:
git log [options]Examples:
git log Show commit historygit log --oneline Compact summary viewgit log --graph Show ASCII graph of branch and merge historygit log -p Show changes introduced by each commitNotes:
View the commit history
git rm
BasicRemove files from the working tree and index
Syntax:
git rm [file]Examples:
git rm file.txt Remove file from working tree and indexgit rm --cached file.txt Remove from index only (keep in working tree)git rm -r directory/ Remove directory recursivelygit rm '*.log' Remove all .log filesNotes:
Different from regular file deletion - stages the removal
git mv
BasicMove or rename files, directories, or symlinks
Syntax:
git mv [source] [destination]Examples:
git mv old-name.txt new-name.txt Rename a filegit mv file.txt directory/ Move file to directorygit mv old-dir/ new-dir/ Rename a directoryNotes:
Equivalent to mv + git rm + git add, but preserves history better
git branch
BranchingList, create, or delete branches
Syntax:
git branch [name] | [-d name]Examples:
git branch List local branchesgit branch -a List all branches (local and remote)git branch feature Create branch named 'feature'git branch -d feature Delete branch 'feature' (if merged)git branch -D feature Force delete branch 'feature'Notes:
Branches are lightweight pointers to commits
git checkout
BranchingSwitch branches or restore working tree files
Syntax:
git checkout [branch|commit] | [--] [file]Examples:
git checkout feature Switch to branch 'feature'git checkout -b feature Create and switch to branch 'feature'git checkout -- file.txt Discard changes to file.txtNotes:
Updates working directory to match selected branch or paths
git switch
BranchingSwitch branches (newer alternative to checkout)
Syntax:
git switch [branch]Examples:
git switch feature Switch to branch 'feature'git switch -c feature Create and switch to branch 'feature'git switch - Switch to previous branchNotes:
Modern replacement for git checkout for switching branches
git merge
BranchingJoin two or more development histories together
Syntax:
git merge [branch]Examples:
git merge feature Merge 'feature' branch into current branchgit merge --no-ff feature Create a merge commit even if fast-forward is possiblegit merge --abort Abort a merge in progressNotes:
Incorporates changes from the named branch into the current branch
git rebase
BranchingReapply commits on top of another base tip
Syntax:
git rebase [base]Examples:
git rebase main Rebase current branch onto 'main'git rebase -i HEAD~3 Interactive rebase for last 3 commitsgit rebase --abort Abort a rebase in progressNotes:
Rewrites commit history to create a linear history
git remote
RemoteManage set of tracked repositories
Syntax:
git remote [add|remove|show|rename]Examples:
git remote -v List remote connections with URLsgit remote add origin https://github.com/user/repo.git Add remote named 'origin'git remote show origin Show information about remote 'origin'git remote rename old new Rename a remoteNotes:
Defines connections to other repositories
git fetch
RemoteDownload objects and refs from another repository
Syntax:
git fetch [remote]Examples:
git fetch origin Fetch from remote 'origin'git fetch --all Fetch from all remotesgit fetch origin branch Fetch specific branch from originNotes:
Fetches branches and tags without merging
git pull
RemoteFetch from and integrate with another repository or branch
Syntax:
git pull [remote] [branch]Examples:
git pull Pull from tracked upstream branchgit pull origin main Pull from 'main' branch of 'origin'git pull --rebase Pull and rebase instead of mergeNotes:
Equivalent to git fetch followed by git merge
git push
RemoteUpdate remote refs along with associated objects
Syntax:
git push [remote] [branch]Examples:
git push origin main Push 'main' branch to remote 'origin'git push -u origin feature Push and set upstream for 'feature' branchgit push --tags Push all tags to remotegit push --force Force push (use with caution)Notes:
Publishes local changes to remote repository
git diff
InspectionShow changes between commits, commit and working tree, etc
Syntax:
git diff [options] [commit] [--] [path]Examples:
git diff Show unstaged changesgit diff --staged Show staged changesgit diff HEAD Show all changes (staged and unstaged)git diff branch1..branch2 Show differences between two branchesNotes:
Displays file differences
git show
InspectionShow various types of objects
Syntax:
git show [object]Examples:
git show Show the last commitgit show abc123 Show commit with hash abc123git show HEAD~3 Show the third-last commitgit show master:file.txt Show file.txt from master branchNotes:
Shows commit details and changes
git blame
InspectionShow what revision and author last modified each line of a file
Syntax:
git blame [file]Examples:
git blame file.txt Show who changed each line in file.txtgit blame -L 10,20 file.txt Show blame for lines 10-20git blame -w file.txt Ignore whitespace changesNotes:
Helps find who introduced changes to specific lines
git grep
InspectionSearch for patterns in tracked files
Syntax:
git grep [pattern] [commit]Examples:
git grep "function" Search for 'function' in all tracked filesgit grep -n "TODO" Search with line numbersgit grep "pattern" HEAD~5 Search in files from 5 commits agogit grep -i "error" Case-insensitive searchNotes:
Faster than regular grep as it only searches tracked files
git shortlog
InspectionSummarize git log output by author
Syntax:
git shortlog [options]Examples:
git shortlog Show commits grouped by authorgit shortlog -s Show only commit counts per authorgit shortlog -n Sort by number of commitsgit shortlog --since="1 month ago" Show contributions from last monthNotes:
Useful for generating changelogs and contribution summaries
git ls-files
InspectionShow information about files in the index and working tree
Syntax:
git ls-files [options]Examples:
git ls-files List all tracked filesgit ls-files --others List untracked filesgit ls-files --ignored List ignored filesgit ls-files --stage Show staged files with metadataNotes:
Useful for debugging what Git is tracking
git ls-tree
InspectionList the contents of a tree object
Syntax:
git ls-tree [tree-ish]Examples:
git ls-tree HEAD List files in the current commitgit ls-tree -r HEAD Recursively list all filesgit ls-tree master:directory/ List files in specific directorygit ls-tree --name-only HEAD Show only file namesNotes:
Shows the tree structure as Git sees it
git rev-parse
InspectionParse revision (or other objects) identifiers
Syntax:
git rev-parse [object]Examples:
git rev-parse HEAD Get the full SHA of HEADgit rev-parse --short HEAD Get short SHA of HEADgit rev-parse --abbrev-ref HEAD Get current branch namegit rev-parse HEAD~3 Get SHA of 3 commits agoNotes:
Useful in scripts for getting commit hashes and branch names
git describe
InspectionGive an object a human readable name based on an available ref
Syntax:
git describe [commit]Examples:
git describe Describe current commitgit describe --tags Use any tag, not just annotated tagsgit describe --always Show commit hash if no tag foundgit describe --abbrev=0 Show only the tag nameNotes:
Useful for generating version numbers from Git tags
git stash
AdvancedStash the changes in a dirty working directory away
Syntax:
git stash [push|pop|apply|list]Examples:
git stash Stash changesgit stash pop Apply and remove the latest stashgit stash apply Apply the latest stash without removing itgit stash list List all stashesgit stash drop Remove the latest stashNotes:
Temporarily stores modified, tracked files for later use
git tag
AdvancedCreate, list, delete or verify a tag object signed with GPG
Syntax:
git tag [name] [commit] | -d [name]Examples:
git tag v1.0.0 Create a lightweight taggit tag -a v1.0.0 -m "Version 1.0.0" Create an annotated taggit tag List all tagsgit tag -d v1.0.0 Delete tag v1.0.0Notes:
Tags are used to mark specific points in history, usually for releases
git reset
AdvancedReset current HEAD to the specified state
Syntax:
git reset [--soft|--mixed|--hard] [commit]Examples:
git reset HEAD~1 Undo the last commit, keep changes stagedgit reset --soft HEAD~1 Undo the last commit, keep changes stagedgit reset --mixed HEAD~1 Undo the last commit, unstage changesgit reset --hard HEAD~1 Undo the last commit, discard all changesNotes:
Changes where HEAD is pointing, with different effects on working directory and index
git revert
AdvancedCreate a new commit that undoes changes from a previous commit
Syntax:
git revert [commit]Examples:
git revert HEAD Revert the last commitgit revert abc123 Revert the commit with hash abc123git revert --no-commit HEAD Revert but do not automatically commitNotes:
Creates a new commit that undoes changes, safer than reset for public branches
git cherry-pick
AdvancedApply the changes introduced by some existing commits
Syntax:
git cherry-pick [commit]Examples:
git cherry-pick abc123 Apply commit abc123 to current branchgit cherry-pick abc123..def456 Apply a range of commitsgit cherry-pick --no-commit abc123 Apply changes without committingNotes:
Useful for moving specific commits between branches
git clean
AdvancedRemove untracked files from the working tree
Syntax:
git clean [-d] [-f]Examples:
git clean -n Show what would be removed (dry run)git clean -f Remove untracked filesgit clean -df Remove untracked files and directoriesgit clean -xf Remove untracked and ignored filesNotes:
Permanently deletes untracked files, use with caution
git bisect
AdvancedUse binary search to find the commit that introduced a bug
Syntax:
git bisect [start|bad|good|reset]Examples:
git bisect start Start bisect sessiongit bisect bad Mark current commit as badgit bisect good v1.0 Mark v1.0 as goodgit bisect reset End bisect sessionNotes:
Helps find which commit introduced a bug by binary search
git archive
AdvancedCreate an archive of files from a named tree
Syntax:
git archive [format] [tree-ish]Examples:
git archive --format=zip HEAD > archive.zip Create zip archive of current commitgit archive --format=tar.gz --prefix=project/ HEAD Create tar.gz with prefixgit archive v1.0.0 > release.tar Archive specific taggit archive HEAD:src/ > src.tar Archive specific directoryNotes:
Useful for creating release packages without .git directory
git submodule
AdvancedInitialize, update or inspect submodules
Syntax:
git submodule [command] [options]Examples:
git submodule add https://github.com/user/repo.git path/to/submodule Add a new submodulegit submodule init Initialize submodule configurationgit submodule update Update submodules to recorded commitsgit submodule status Show status of submodulesgit submodule foreach git pull origin main Run command in each submoduleNotes:
Allows including other Git repositories as subdirectories
🔧 What is Git?
Git is a distributed version control system created by Linus Torvalds in 2005. Originally developed to manage the Linux kernel source code, Git has become the world's most popular version control system, used by millions of developers and virtually every major software project. Git tracks changes in files over time, enables collaboration among multiple developers, and provides a complete history of every change made to a project.
🚀 Core Features
- ✓ Distributed: Every copy is a complete repository with full history
- ✓ Branching: Create parallel development lines effortlessly
- ✓ Merging: Intelligent merging of different code changes
- ✓ History tracking: Complete audit trail of every change
- ✓ Collaboration: Seamless team development workflows
- ✓ Speed: Lightning-fast operations even on large projects
💡 Why Use Git?
- • Never lose code: Complete history and backup of all changes
- • Experiment safely: Try new features without breaking main code
- • Collaborate effectively: Multiple people can work simultaneously
- • Track changes: See exactly who changed what and when
- • Roll back easily: Undo changes or revert to any previous state
- • Industry standard: Used by 95%+ of professional developers
🌟 Key Git Concepts
Repository (Repo)
A container for your project that stores all files, history, and branches. Can be local (on your computer) or remote (on GitHub, GitLab, etc.)
Commit
A snapshot of your project at a specific point in time. Each commit has a unique ID and message describing the changes.
Branch
An independent line of development. Create branches for features, bug fixes, or experiments without affecting the main code.
Working Directory
Your local folder where you edit files. Changes here are not tracked until you stage and commit them.
Staging Area
A holding area for changes you want to include in your next commit. Use `git add` to stage files.
Remote
A version of your repository hosted elsewhere (GitHub, GitLab). Used for collaboration and backup.
🔄 Git Workflow
1. Modify
Edit files in your working directory
nano file.txt2. Stage
Add changes to staging area
git add file.txt3. Commit
Save changes to repository
git commit -m "Update"Repeat this cycle: Modify → Stage → Commit → Push to Remote
🏢 Git in Different Environments
Individual Projects
- • Track personal project history
- • Experiment with features safely
- • Backup code to cloud services
- • Organize work with branches
Team Development
- • Collaborate without conflicts
- • Review code before merging
- • Track who made which changes
- • Manage release versions
Open Source
- • Accept contributions from anyone
- • Maintain multiple project versions
- • Handle complex branching strategies
- • Distribute development globally
🛠️ Git Hosting Platforms
Popular Git Hosts
- • GitHub: Most popular, great for open source
- • GitLab: Full DevOps platform, self-hosted options
- • Bitbucket: Atlassian integration, free private repos
- • SourceForge: Long-standing open source host
- • Codeberg: Non-profit, privacy-focused
What They Provide
- • Remote repository hosting
- • Web-based repository management
- • Pull request/merge request workflows
- • Issue tracking and project management
- • Continuous integration/deployment
- • Team collaboration tools
🚀 Getting Started with Git
1. Installation & Setup
Download Git from git-scm.com • Configure your name and email • git config --global user.name "Your Name"
2. Create Your First Repository
git init in a project folder •
Make your first commit • Connect to a remote repository
3. Learn Core Commands
Master add, commit, push, pull • Learn branching and merging • Practice with small projects first
⚖️ Git vs. Other Version Control
Git Advantages
- • Distributed - no single point of failure
- • Incredibly fast operations
- • Powerful branching and merging
- • Industry standard with huge ecosystem
- • Works offline completely
- • Free and open source
Alternatives
- • SVN (Subversion): Centralized, simpler but less flexible
- • Mercurial: Similar to Git, easier to learn
- • Perforce: Enterprise-focused, handles large files well
- • Bazaar: Distributed like Git, less popular
- • CVS: Legacy system, rarely used today
Git Mastery: Git seems complex at first, but mastering the basics transforms how you work with code. Start with simple commands, practice regularly, and gradually explore advanced features. This cheat sheet is your companion on the journey to Git expertise! 🎯
Pro Git Tips
Everyday Git Workflow
git pull- Update your local repositorygit checkout -b feature- Create a new branch- Make your changes
git add .- Stage your changesgit commit -m "Description"- Commit changesgit push -u origin feature- Push to remote
Git Best Practices
- Commit early and often
- Write meaningful commit messages
- Use branches for new features or bug fixes
- Pull before pushing to avoid conflicts
- Use
git diffto review changes before committing - Set up
.gitignoreto exclude unwanted files