Git Cheat Sheet
EsthyTech1. Git Setup
git --version # Check installed Git version
git config --global user.name "Your Name" # Set your Git username
git config --global user.email "you@example.com" # Set your Git email
git config --global core.editor "code --wait" # Set default editor
git config --list # Show current Git configuration
2. Starting a Repository
git init # Initialize a new local repository
git clone <repo-url> # Clone an existing remote repository
3. Status & Logs
git status # Check working tree status
git log # Show full commit history
git log --oneline # Show compact commit history
git log --graph --oneline --all # Show history in graph format
git show <commit-id> # Show details of a specific commit
4. Staging & Files
git add <file> # Stage a specific file
git add . # Stage changes in current directory
git add -A # Stage all changes including deletions
git rm <file> # Remove file from Git and working directory
git rm --cached <file> # Stop tracking file but keep it locally
git mv old new # Rename or move a file
5. Commit
git commit -m "message" # Commit staged changes with a message
git commit -am "message" # Stage tracked files and commit
git commit --amend # Modify the most recent commit
git commit --amend -m "new message" # Change the last commit message
6. Branching
git branch # List local branches
git branch -a # List local and remote branches
git branch <name> # Create a new branch
git checkout <branch> # Switch to a branch
git checkout -b <branch> # Create and switch to a new branch
git switch <branch> # Switch to a branch
git switch -c <branch> # Create and switch to a new branch
git branch -d <branch> # Delete a merged branch
git branch -D <branch> # Force delete a branch
7. Merge & Rebase
git merge <branch> # Merge a branch into the current branch
git rebase <branch> # Reapply commits on top of another branch
git rebase -i HEAD~n # Interactively rebase the last n commits
git merge --abort # Cancel an in-progress merge
git rebase --abort # Cancel an in-progress rebase
git rebase --continue # Continue rebase after resolving conflicts
8. Remote Repositories
git remote -v # Show configured remotes
git remote add origin <url> # Add a remote repository
git remote remove origin # Remove a remote repository
git fetch # Download remote changes without merging
git pull # Fetch and merge remote changes
git push # Push local commits to remote
git push -u origin <branch> # Push branch and set upstream
git push origin --delete <branch> # Delete a remote branch
9. Viewing Differences
git diff # Show unstaged changes
git diff --staged # Show staged changes
git diff branch1..branch2 # Compare two branches
git blame <file> # Show who changed each line in a file
10. Undo Changes
git restore <file> # Discard changes in a file
git restore --staged <file> # Unstage a staged file
git reset <file> # Unstage a file
git reset --soft HEAD~1 # Undo last commit but keep changes staged
git reset --mixed HEAD~1 # Undo last commit and unstage changes
git reset --hard HEAD~1 # Undo last commit and discard changes
git checkout -- <file> # Restore file to last committed version
git revert <commit-id> # Safely reverse a commit with a new commit
11. Stashing
git stash # Save uncommitted changes temporarily
git stash list # List saved stashes
git stash apply # Reapply latest stash
git stash pop # Reapply and remove latest stash
git stash drop # Delete latest stash
git stash clear # Delete all stashes
12. Tags
git tag # List tags
git tag <tag-name> # Create a lightweight tag
git tag -a <tag-name> -m "message" # Create an annotated tag
git push origin <tag-name> # Push a specific tag
git push origin --tags # Push all tags
13. Inspecting & Searching
git grep "text" # Search text in tracked files
git ls-files # List tracked files
git reflog # Show HEAD history
git shortlog # Summarize commits by author
14. Cleaning
git clean -n # Preview untracked files to remove
git clean -f # Remove untracked files
git clean -fd # Remove untracked files and directories
15. Common Everyday Workflow
git status # Check current status
git add . # Stage current changes
git commit -m "your message" # Commit changes
git push # Push changes to remote
16. Dangerous Commands
git reset --hard HEAD~1 # Delete last commit and local changes
git clean -fd # Permanently remove untracked files and folders
git push --force # Overwrite remote branch history
git branch -D <branch> # Force delete a branch
