๐ง 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 ofcheckout
(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.