๐ง 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 initInitialize a new Git repository.
git clone https://github.com/user/repo.gitClone an existing repository.
๐พ Basic Workflow
git statusCheck the current status.
git add .Stage all changes.
git commit -m "Your message"Commit staged changes.
git pushPush commits to remote.
git pullFetch and merge changes from remote.
๐ฟ Branching
git branchList all branches.
git checkout -b new-featureCreate and switch to a new branch.
git merge mainMerge another branch into your current branch.
git branch -d old-featureDelete a branch (locally).
๐งน Undo & Reset
git restore file.txtRestore a file to the last committed state.
git reset --soft HEAD~1Undo last commit but keep changes staged.
git reset --hard HEAD~1Undo last commit and discard all changes.
๐ Stashing
git stashSave uncommitted changes for later.
git stash popReapply the latest stash.
๐ Logs & Diffs
git logView commit history.
git diffSee changes between commits, branches, or working directory.
๐ฆ Remote Control
git remote -vView connected remotes.
git remote add origin https://github.com/user/repo.gitAdd a remote.
๐ค Interactive Operations
git add -pInteractively choose portions of files to stage.
git rebase -i HEAD~3Interactive 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.0Create a lightweight tag.
git tag -a v1.0.0 -m "Version 1.0.0"Create an annotated tag with a message.
git push origin --tagsPush all tags to remote.
git tag -d v1.0.0Delete 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-featureGit Flow workflow for feature development.
# GitHub Flow
git checkout -b feature-branch
# Work, commit, push to remote
# Create pull request via GitHubSimpler GitHub Flow for continuous delivery.
# Trunk-Based Development
git checkout main
git pull
# Make small changes directly or via short-lived branchesTrunk-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-commitCreate a pre-commit hook to run linting.
# Use Husky for team-shared hooks
npm install husky --save-dev
npx husky initSetup 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/wantClone only specific parts of a large repository.
# Git LFS setup
git lfs install
git lfs track "*.psd"
git add .gitattributesTrack 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 trueConfigure 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 HEADExamine the contents of the HEAD commit object.
# View the commit graph
git log --graph --oneline --allVisually display the commit history as a graph.
# List all objects in the repository
git rev-list --objects --allList all objects in the Git database.
๐ฅ Collaborative Git
# Update your branch with upstream changes
git fetch origin
git rebase origin/mainKeep 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 commitUse 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.txtSee who last modified each line of a file and in which commit.
git rebase -i HEAD~3Interactive rebase for the last 3 commits.
git cherry-pick <commit-hash>Apply changes from a specific commit.
๐ท๏ธ Tags
git tag v1.0.0Create a lightweight tag.
git tag -a v1.0.0 -m "Version 1.0.0"Create an annotated tag.
git push origin --tagsPush all tags to remote.
๐ Troubleshooting
git blame file.txtSee 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 reflogView the history of HEAD movements.
๐ณ Advanced Branch Operations
git rebase mainRebase current branch onto main.
git branch --mergedList branches merged into current branch.
git push origin --delete branch-nameDelete a remote branch.
๐ฆ Submodules
git submodule add <repository> <path>Add a submodule to your project.
git submodule update --init --recursiveInitialize and update all submodules.
๐งน Cleanup & Maintenance
git gcClean up unnecessary files and optimize local repository.
git pruneRemove unreachable objects.
git clean -fdRemove untracked files and directories.
โ๏ธ Conflict Resolution
git merge --abortAbort a merge in case of conflicts.
git checkout --ours file.txtKeep our version of a file during merge conflict.
git checkout --theirs file.txtKeep 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 checkoutCreate command aliases.
git config --global pull.rebase trueConfigure pull to use rebase by default.
โ Bonus Tips
- Use .gitignoreto exclude files from version control.
- Use git log --onelinefor a cleaner commit view.
- Use git switchinstead 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 filenameThen 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.
