GIT COMMANDS CHEATSHEET
1. SETUP & CONFIGURATION
- Set your username:
git config --global user.name "name"
- Set your email:
git config --global user.email "email"
- Initialize a repository:
git init
- Create a .gitignore file:
touch .gitignore
- Generate SSH Keys:
ssh-keygen -t ed25519 -C "email"
2. BASIC SNAPSHOTTING
- Create new file:
touch filename
- Stage a specific file:
git add filename
- Stage ALL changed files:
git add -A
- Commit staged files:
git commit -m "message"
- Commit tracked files directly:
git commit -a -m "message"
- Check status:
git status
- Short status check:
git status -s
3. INSPECTION & COMPARISON
- View commit history:
git log
- View last 'n' commits with changes:
git log -p -n
- Compare Working Tree vs Staging:
git diff
- Compare Staging vs Last Commit:
git diff --staged
- List files in directory:
ls
4. UNDOING & DELETING
- Undo changes in file (Working Tree):
git checkout filename
- Undo changes in ALL files:
git checkout -f
- Remove from Staging & Disk:
git rm filename
- Remove from Staging only (Keep file):
git rm --cached filename
5. BRANCHING & MERGING
- List all branches:
git branch
- Create a new branch:
git branch branch_name
- Switch to a branch:
git checkout branch_name
- Create & Switch immediately:
git checkout -b branch_name
- Rename current branch to 'main':
git branch -M main
- Merge branch into current branch:
git merge branch_name
6. REMOTE & GITHUB
- Connect local repo to GitHub:
git remote add origin "URL"
- Check remote connection:
git remote -v
- First push (set upstream):
git push -u origin main
- Push subsequent changes:
git push
- Clone a repository:
git clone "URL"
- Pull latest changes:
git pull origin main
← Back to Home