The Essential Git Cheat Sheet

The Essential Git Cheat Sheet

~ 5 min read

๐Ÿง  Git Cheat Sheet: The Commands Youโ€™ll Use Daily

Whether youโ€™re a beginner or just need a refresher, this Git cheat sheet covers the commands youโ€™ll use most often.

๐Ÿ“ Setup

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Set your identity globally for all repositories.


๐Ÿงฑ Create & Clone

git init

Initialize a new Git repository.

git clone https://github.com/user/repo.git

Clone an existing repository.


๐Ÿ’พ Basic Workflow

git status

Check the current status.

git add .

Stage all changes.

git commit -m "Your message"

Commit staged changes.

git push

Push commits to remote.

git pull

Fetch and merge changes from remote.


๐ŸŒฟ Branching

git branch

List all branches.

git checkout -b new-feature

Create and switch to a new branch.

git merge main

Merge another branch into your current branch.

git branch -d old-feature

Delete a branch (locally).


๐Ÿงน Undo & Reset

git restore file.txt

Restore a file to the last committed state.

git reset --soft HEAD~1

Undo last commit but keep changes staged.

git reset --hard HEAD~1

Undo last commit and discard all changes.


๐Ÿ“Œ Stashing

git stash

Save uncommitted changes for later.

git stash pop

Reapply the latest stash.


๐Ÿ” Logs & Diffs

git log

View commit history.

git diff

See changes between commits, branches, or working directory.


๐Ÿ“ฆ Remote Control

git remote -v

View connected remotes.

git remote add origin https://github.com/user/repo.git

Add a remote.


๐Ÿค Interactive Operations

git add -p

Interactively choose portions of files to stage.

git rebase -i HEAD~3

Interactive rebase for the last 3 commits, allowing reordering, squashing, or editing.

git cherry-pick <commit-hash>

Apply changes from a specific commit to your current branch.


๐Ÿท๏ธ Tags

git tag v1.0.0

Create a lightweight tag.

git tag -a v1.0.0 -m "Version 1.0.0"

Create an annotated tag with a message.

git push origin --tags

Push all tags to remote.

git tag -d v1.0.0

Delete a local tag.


๐ŸŒŠ Modern Git Workflows

# Git Flow - Feature Branch
git checkout develop
git checkout -b feature/new-feature
# Work on feature, then...
git checkout develop
git merge feature/new-feature

Git Flow workflow for feature development.

# GitHub Flow
git checkout -b feature-branch
# Work, commit, push to remote
# Create pull request via GitHub

Simpler GitHub Flow for continuous delivery.

# Trunk-Based Development
git checkout main
git pull
# Make small changes directly or via short-lived branches

Trunk-based development for CI/CD environments.


๐Ÿช Git Hooks

# Create a pre-commit hook
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/sh
# Run linting before commit
npm run lint
EOF
chmod +x .git/hooks/pre-commit

Create a pre-commit hook to run linting.

# Use Husky for team-shared hooks
npm install husky --save-dev
npx husky init

Setup Husky to share Git hooks with your team.


๐Ÿ—„๏ธ Working with Large Repositories

# Sparse checkout
git clone --filter=blob:none --sparse https://github.com/user/large-repo.git
cd large-repo
git sparse-checkout set path/you/want

Clone only specific parts of a large repository.

# Git LFS setup
git lfs install
git lfs track "*.psd"
git add .gitattributes

Track large files with Git LFS.


๐Ÿ” Security Best Practices

# Setup GPG signing
git config --global user.signingkey YOUR_GPG_KEY_ID
git config --global commit.gpgsign true

Configure Git to sign all commits with your GPG key.

# Create a signed commit
git commit -S -m "Signed commit message"

Create a GPG-signed commit.

# Create a signed tag
git tag -s v1.0.0 -m "Signed tag"

Create a GPG-signed tag.


๐Ÿ”ง Git Internals

# View Git objects
git cat-file -p HEAD

Examine the contents of the HEAD commit object.

# View the commit graph
git log --graph --oneline --all

Visually display the commit history as a graph.

# List all objects in the repository
git rev-list --objects --all

List all objects in the Git database.


๐Ÿ‘ฅ Collaborative Git

# Update your branch with upstream changes
git fetch origin
git rebase origin/main

Keep your branch updated with changes from the main branch.

# Create a pull request (GitHub CLI)
gh pr create --title "Feature title" --body "Description"

Create a pull request using GitHub CLI.

# Review a pull request (GitHub CLI)
gh pr checkout <PR-number>

Check out a pull request locally for review.


๐Ÿ” Diagnostics and Troubleshooting

# Find which commit introduced a bug
git bisect start
git bisect bad  # Current version is bad
git bisect good <commit-hash>  # This older version was good
# Git will help you find the first bad commit

Use binary search to find which commit introduced a bug.

# Recover deleted commits (within ~30 days)
git reflog
git checkout <reflog-entry>

Recover deleted commits using reflog.

# Debug issues with a file
git blame file.txt

See who last modified each line of a file and in which commit.

git rebase -i HEAD~3

Interactive rebase for the last 3 commits.

git cherry-pick <commit-hash>

Apply changes from a specific commit.


๐Ÿท๏ธ Tags

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 push origin --tags

Push all tags to remote.


๐Ÿ” Troubleshooting

git blame file.txt

See who changed which lines and when.

git bisect start
git bisect bad
git bisect good <commit-hash>

Binary search for the commit that introduced a bug.

git reflog

View the history of HEAD movements.


๐ŸŒณ Advanced Branch Operations

git rebase main

Rebase current branch onto main.

git branch --merged

List branches merged into current branch.

git push origin --delete branch-name

Delete a remote branch.


๐Ÿ“ฆ Submodules

git submodule add <repository> <path>

Add a submodule to your project.

git submodule update --init --recursive

Initialize and update all submodules.


๐Ÿงน Cleanup & Maintenance

git gc

Clean up unnecessary files and optimize local repository.

git prune

Remove unreachable objects.

git clean -fd

Remove untracked files and directories.


โš”๏ธ Conflict Resolution

git merge --abort

Abort a merge in case of conflicts.

git checkout --ours file.txt

Keep our version of a file during merge conflict.

git checkout --theirs file.txt

Keep their version of a file during merge conflict.


โš™๏ธ Useful Configurations

git config --global core.editor "code --wait"

Set VS Code as default editor.

git config --global alias.co checkout

Create command aliases.

git config --global pull.rebase true

Configure pull to use rebase by default.


โœ… Bonus Tips

  • Use .gitignore to exclude files from version control.
  • Use git log --oneline for a cleaner commit view.
  • Use git switch instead of checkout (modern syntax).
  • Untrack files that should have been ignored:
    If youโ€™ve added files that should have been in .gitignore, but you donโ€™t want to delete them locally:
git rm --cached filename

Then commit the change:

git commit -m "Remove tracked file that should be ignored"

This keeps the file locally, but removes it from the repository. Add it to .gitignore to prevent future tracking.

all posts →