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.

38 commands found
Filter by category:

git init

Setup

Initialize a new Git repository

Syntax:

git init [directory]

Examples:

git init Initialize repository in current directory
git init my-project Initialize repository in my-project directory

Notes:

Creates a .git subdirectory with repository files

git clone

Setup

Clone a repository into a new directory

Syntax:

git clone [url] [directory]

Examples:

git clone https://github.com/user/repo.git Clone remote repository
git clone https://github.com/user/repo.git my-folder Clone into specific folder
git clone --depth=1 https://github.com/user/repo.git Clone only the latest revision

Notes:

Creates a copy of a repository with full history

git config

Setup

Get and set repository or global options

Syntax:

git config [--global] [name] [value]

Examples:

git config --global user.name "Your Name" Set global username
git config --global user.email "email@example.com" Set global email
git config --list List all configuration settings

Notes:

Use --global to apply settings for all repositories for the user

git help

Setup

Display help information about Git commands

Syntax:

git help [command]

Examples:

git help Show general help
git help commit Show help for commit command
git help -a List all available commands
git help -g List concept guides

Notes:

Opens help documentation in browser or terminal

git version

Setup

Display the version of Git

Syntax:

git version

Examples:

git version Show Git version information
git --version Alternative way to show version

Notes:

Useful for checking which Git version is installed

git add

Basic

Add file contents to the index (staging area)

Syntax:

git add [file|pattern]

Examples:

git add file.txt Add a specific file
git add . Add all new and modified files
git add -p Interactive add (choose chunks to stage)

Notes:

Prepares content for the next commit

git commit

Basic

Record changes to the repository

Syntax:

git commit -m "[message]"

Examples:

git commit -m "Add feature X" Commit with message
git commit -a -m "Fix bugs" Add all tracked files and commit
git commit --amend Modify the last commit

Notes:

Creates a snapshot of staged changes

git status

Basic

Show the working tree status

Syntax:

git status [options]

Examples:

git status Display full status
git status -s Display short status

Notes:

Shows which files are modified, staged, or untracked

git log

Basic

Show commit logs

Syntax:

git log [options]

Examples:

git log Show commit history
git log --oneline Compact summary view
git log --graph Show ASCII graph of branch and merge history
git log -p Show changes introduced by each commit

Notes:

View the commit history

git rm

Basic

Remove files from the working tree and index

Syntax:

git rm [file]

Examples:

git rm file.txt Remove file from working tree and index
git rm --cached file.txt Remove from index only (keep in working tree)
git rm -r directory/ Remove directory recursively
git rm '*.log' Remove all .log files

Notes:

Different from regular file deletion - stages the removal

git mv

Basic

Move or rename files, directories, or symlinks

Syntax:

git mv [source] [destination]

Examples:

git mv old-name.txt new-name.txt Rename a file
git mv file.txt directory/ Move file to directory
git mv old-dir/ new-dir/ Rename a directory

Notes:

Equivalent to mv + git rm + git add, but preserves history better

git branch

Branching

List, create, or delete branches

Syntax:

git branch [name] | [-d name]

Examples:

git branch List local branches
git 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

Branching

Switch 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.txt

Notes:

Updates working directory to match selected branch or paths

git switch

Branching

Switch 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 branch

Notes:

Modern replacement for git checkout for switching branches

git merge

Branching

Join two or more development histories together

Syntax:

git merge [branch]

Examples:

git merge feature Merge 'feature' branch into current branch
git merge --no-ff feature Create a merge commit even if fast-forward is possible
git merge --abort Abort a merge in progress

Notes:

Incorporates changes from the named branch into the current branch

git rebase

Branching

Reapply 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 commits
git rebase --abort Abort a rebase in progress

Notes:

Rewrites commit history to create a linear history

git remote

Remote

Manage set of tracked repositories

Syntax:

git remote [add|remove|show|rename]

Examples:

git remote -v List remote connections with URLs
git 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 remote

Notes:

Defines connections to other repositories

git fetch

Remote

Download objects and refs from another repository

Syntax:

git fetch [remote]

Examples:

git fetch origin Fetch from remote 'origin'
git fetch --all Fetch from all remotes
git fetch origin branch Fetch specific branch from origin

Notes:

Fetches branches and tags without merging

git pull

Remote

Fetch from and integrate with another repository or branch

Syntax:

git pull [remote] [branch]

Examples:

git pull Pull from tracked upstream branch
git pull origin main Pull from 'main' branch of 'origin'
git pull --rebase Pull and rebase instead of merge

Notes:

Equivalent to git fetch followed by git merge

git push

Remote

Update 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' branch
git push --tags Push all tags to remote
git push --force Force push (use with caution)

Notes:

Publishes local changes to remote repository

git diff

Inspection

Show changes between commits, commit and working tree, etc

Syntax:

git diff [options] [commit] [--] [path]

Examples:

git diff Show unstaged changes
git diff --staged Show staged changes
git diff HEAD Show all changes (staged and unstaged)
git diff branch1..branch2 Show differences between two branches

Notes:

Displays file differences

git show

Inspection

Show various types of objects

Syntax:

git show [object]

Examples:

git show Show the last commit
git show abc123 Show commit with hash abc123
git show HEAD~3 Show the third-last commit
git show master:file.txt Show file.txt from master branch

Notes:

Shows commit details and changes

git blame

Inspection

Show 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.txt
git blame -L 10,20 file.txt Show blame for lines 10-20
git blame -w file.txt Ignore whitespace changes

Notes:

Helps find who introduced changes to specific lines

git grep

Inspection

Search for patterns in tracked files

Syntax:

git grep [pattern] [commit]

Examples:

git grep "function" Search for 'function' in all tracked files
git grep -n "TODO" Search with line numbers
git grep "pattern" HEAD~5 Search in files from 5 commits ago
git grep -i "error" Case-insensitive search

Notes:

Faster than regular grep as it only searches tracked files

git shortlog

Inspection

Summarize git log output by author

Syntax:

git shortlog [options]

Examples:

git shortlog Show commits grouped by author
git shortlog -s Show only commit counts per author
git shortlog -n Sort by number of commits
git shortlog --since="1 month ago" Show contributions from last month

Notes:

Useful for generating changelogs and contribution summaries

git ls-files

Inspection

Show information about files in the index and working tree

Syntax:

git ls-files [options]

Examples:

git ls-files List all tracked files
git ls-files --others List untracked files
git ls-files --ignored List ignored files
git ls-files --stage Show staged files with metadata

Notes:

Useful for debugging what Git is tracking

git ls-tree

Inspection

List the contents of a tree object

Syntax:

git ls-tree [tree-ish]

Examples:

git ls-tree HEAD List files in the current commit
git ls-tree -r HEAD Recursively list all files
git ls-tree master:directory/ List files in specific directory
git ls-tree --name-only HEAD Show only file names

Notes:

Shows the tree structure as Git sees it

git rev-parse

Inspection

Parse revision (or other objects) identifiers

Syntax:

git rev-parse [object]

Examples:

git rev-parse HEAD Get the full SHA of HEAD
git rev-parse --short HEAD Get short SHA of HEAD
git rev-parse --abbrev-ref HEAD Get current branch name
git rev-parse HEAD~3 Get SHA of 3 commits ago

Notes:

Useful in scripts for getting commit hashes and branch names

git describe

Inspection

Give an object a human readable name based on an available ref

Syntax:

git describe [commit]

Examples:

git describe Describe current commit
git describe --tags Use any tag, not just annotated tags
git describe --always Show commit hash if no tag found
git describe --abbrev=0 Show only the tag name

Notes:

Useful for generating version numbers from Git tags

git stash

Advanced

Stash the changes in a dirty working directory away

Syntax:

git stash [push|pop|apply|list]

Examples:

git stash Stash changes
git stash pop Apply and remove the latest stash
git stash apply Apply the latest stash without removing it
git stash list List all stashes
git stash drop Remove the latest stash

Notes:

Temporarily stores modified, tracked files for later use

git tag

Advanced

Create, 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 tag
git tag -a v1.0.0 -m "Version 1.0.0" Create an annotated tag
git tag List all tags
git tag -d v1.0.0 Delete tag v1.0.0

Notes:

Tags are used to mark specific points in history, usually for releases

git reset

Advanced

Reset current HEAD to the specified state

Syntax:

git reset [--soft|--mixed|--hard] [commit]

Examples:

git reset HEAD~1 Undo the last commit, keep changes staged
git reset --soft HEAD~1 Undo the last commit, keep changes staged
git reset --mixed HEAD~1 Undo the last commit, unstage changes
git reset --hard HEAD~1 Undo the last commit, discard all changes

Notes:

Changes where HEAD is pointing, with different effects on working directory and index

git revert

Advanced

Create a new commit that undoes changes from a previous commit

Syntax:

git revert [commit]

Examples:

git revert HEAD Revert the last commit
git revert abc123 Revert the commit with hash abc123
git revert --no-commit HEAD Revert but do not automatically commit

Notes:

Creates a new commit that undoes changes, safer than reset for public branches

git cherry-pick

Advanced

Apply the changes introduced by some existing commits

Syntax:

git cherry-pick [commit]

Examples:

git cherry-pick abc123 Apply commit abc123 to current branch
git cherry-pick abc123..def456 Apply a range of commits
git cherry-pick --no-commit abc123 Apply changes without committing

Notes:

Useful for moving specific commits between branches

git clean

Advanced

Remove 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 files
git clean -df Remove untracked files and directories
git clean -xf Remove untracked and ignored files

Notes:

Permanently deletes untracked files, use with caution

git bisect

Advanced

Use binary search to find the commit that introduced a bug

Syntax:

git bisect [start|bad|good|reset]

Examples:

git bisect start Start bisect session
git bisect bad Mark current commit as bad
git bisect good v1.0 Mark v1.0 as good
git bisect reset End bisect session

Notes:

Helps find which commit introduced a bug by binary search

git archive

Advanced

Create 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 commit
git archive --format=tar.gz --prefix=project/ HEAD Create tar.gz with prefix
git archive v1.0.0 > release.tar Archive specific tag
git archive HEAD:src/ > src.tar Archive specific directory

Notes:

Useful for creating release packages without .git directory

git submodule

Advanced

Initialize, 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 submodule
git submodule init Initialize submodule configuration
git submodule update Update submodules to recorded commits
git submodule status Show status of submodules
git submodule foreach git pull origin main Run command in each submodule

Notes:

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.txt

2. Stage

Add changes to staging area

git add file.txt

3. 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

  1. git pull - Update your local repository
  2. git checkout -b feature - Create a new branch
  3. Make your changes
  4. git add . - Stage your changes
  5. git commit -m "Description" - Commit changes
  6. git 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 diff to review changes before committing
  • Set up .gitignore to exclude unwanted files