Git Cheatsheet

Basics

Use this Git reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Git for Software Engineering Practice

Use this Git cheatsheet when you are saving algorithm practice, DSA roadmap notes, portfolio projects, or coding interview problem solutions. The commands below cover the daily version-control loop: initialize a repository, inspect changes, commit clear checkpoints, and push work to a remote.

Initializing a Repository

git init                        # Initialize a new repo in the current directory
git init <directory>            # Initialize a new repo in <directory>
git init --bare <directory>     # Create a bare repo (no working tree; for servers)
git clone <url>                 # Clone a remote repo into a new directory
git clone <url> <directory>     # Clone into a specific directory name
git clone --depth 1 <url>       # Shallow clone (only latest commit)
git clone --branch <name> <url> # Clone and check out a specific branch
git clone --recurse-submodules <url>  # Clone and initialize all submodules

Repository Status

git status                      # Show working tree status (staged, unstaged, untracked)
git status -s                   # Short format (XY filename; X=staged, Y=unstaged)
git status -u                   # Show untracked files (default); -uno to hide them
git status --ignored            # Also show ignored files

Short-format status codes: | Code | Meaning | |------|---------| | M | Modified | | A | Added (staged) | | D | Deleted | | R | Renamed | | C | Copied | | U | Updated but unmerged | | ? | Untracked | | ! | Ignored |

Getting Help

git help <command>              # Open man page for a command
git <command> --help            # Same as above
git <command> -h                # Short, inline help summary
git help -a                     # List all available commands
git help -g                     # List concept guides

Global Workflow Overview

# Typical daily flow
git status                      # Check what's changed
git diff                        # Review unstaged changes
git add <file>                  # Stage changes
git commit -m "message"         # Commit staged changes
git push origin <branch>        # Push to remote

.gitignore

# .gitignore patterns
*.log                           # Ignore all .log files
build/                          # Ignore entire build/ directory
!important.log                  # Un-ignore specific file (overrides *.log)
/config.env                     # Ignore only at repo root
**/temp                         # Ignore any directory named temp at any depth
doc/*.txt                       # Ignore .txt in doc/ but not doc/sub/*.txt
git check-ignore -v <file>      # Show which .gitignore rule is ignoring a file
git rm --cached <file>          # Stop tracking a file already committed

Global ignore file:

git config --global core.excludesfile ~/.gitignore_global

Inspecting the Repository

git show                        # Show latest commit and its diff
git show <commit>               # Show a specific commit
git show <commit>:<file>        # Show a file as it was at a commit
git ls-files                    # List tracked files
git ls-files --others --exclude-standard  # List untracked files
git count-objects -vH           # Show repo size on disk
git rev-parse HEAD              # Print the current commit SHA
git rev-parse --abbrev-ref HEAD # Print the current branch name

Plumbing / Object Types

Git stores four object types in .git/objects/:

TypeDescription
blobFile content (no filename)
treeDirectory listing (filenames → blobs/trees)
commitSnapshot metadata (tree, parent, author, message)
tagAnnotated tag object
git cat-file -t <sha>           # Show object type
git cat-file -p <sha>           # Pretty-print object content