Git Quick Reference
Everything you need day‑to‑day – workflow, branching, remotes, and undoing changes.
Configuration
# Set user name and email (required for commits) git config --global user.name "Your Name" git config --global user.email "you@example.com" # Set default editor git config --global core.editor "code --wait" # VS Code git config --global core.editor "vim" # Vim git config --global core.editor "nano" # Nano # Set default branch name git config --global init.defaultBranch main # Set aliases git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status git config --global alias.lg "log --oneline --graph --decorate" # View all configuration git config --list # View configuration file location git config --global --edit
Repository Management
# Create a new repository in the current directory git init # Create a new repository with a specific name git init project-name # Clone an existing repository git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git # SSH # Clone a specific branch git clone -b branch-name https://github.com/user/repo.git # Clone with a custom folder name git clone https://github.com/user/repo.git my-folder
Basic Workflow
Status & Tracking
# Check status of working directory git status # Short status git status -s # Add file(s) to staging area git add filename git add . # add all files in current directory git add -A # add all files in entire repo git add *.js # add all .js files # Add all changes, including deletions git add --all # Interactive add (patch mode) git add -p # stage changes hunk by hunk
Commit
# Commit with a message git commit -m "Add feature X" # Commit and skip staging (add + commit) git commit -a -m "Update all tracked files" # Amend the last commit (add changes to previous commit) git commit --amend -m "New commit message" # Amend without changing message git commit --amend --no-edit
Log & History
# View commit history git log # Compact log (one line per commit) git log --oneline # Graph view git log --graph --oneline --decorate # View commits by author git log --author="Your Name" # View commits in a date range git log --since="2024-01-01" --until="2024-12-31" # View commits with a specific message pattern git log --grep="fix" # View changes in a commit git show commit-hash # View what changed in the last commit git show HEAD
Diff
# View unstaged changes git diff # View staged changes git diff --staged # View changes between commits git diff commit1..commit2 # View changes in a specific file git diff filename # View changes in a more readable format git diff --color-words
Branching
Branch Basics
# List branches git branch # local branches git branch -r # remote branches git branch -a # all branches (local + remote) # Create a branch git branch new-branch # Create and switch to a branch git checkout -b new-branch git switch -c new-branch # newer syntax # Switch to a branch git checkout branch-name git switch branch-name # newer syntax # Delete a branch git branch -d branch-name # safe (only if merged) git branch -D branch-name # force delete # Rename the current branch git branch -m new-name # Rename a specific branch git branch -m old-name new-name
Merging
# Merge a branch into the current branch git merge branch-name # Merge with no fast-forward (creates a merge commit) git merge --no-ff branch-name # Abort a merge in conflict git merge --abort # View merge conflicts git status # After resolving conflicts git add . git commit
Rebasing
# Rebase current branch onto another git rebase main # Interactive rebase (squash, reword, etc.) git rebase -i HEAD~3 # last 3 commits git rebase -i commit-hash # from a specific commit # Abort a rebase git rebase --abort # Continue a rebase after resolving conflicts git rebase --continue
Rebase vs Merge (Quick Guide)
# Merge – preserves history, creates merge commit # Use when you want to keep the full commit history # Rebase – linear history, no merge commit # Use when you want a clean, linear history # Rule of thumb: rebase feature branches onto main before merging git checkout feature git rebase main git checkout main git merge feature
Remote Repositories
Managing Remotes
# Add a remote git remote add origin https://github.com/user/repo.git # View remote URLs git remote -v # Remove a remote git remote remove origin # Rename a remote git remote rename origin upstream
Push & Pull
# Push to remote git push origin main # Push and set upstream (tracking) git push -u origin main # Push all branches git push --all # Push tags git push --tags # Force push (use with caution) git push --force origin main # Force push with lease (safer) git push --force-with-lease origin main # Pull (fetch + merge) git pull origin main # Pull with rebase (fetch + rebase) git pull --rebase origin main # Fetch changes without merging git fetch origin # Fetch all remotes git fetch --all
Tracking Branches
# Track a remote branch git branch -u origin/main git branch --set-upstream-to=origin/main # View tracking branches git branch -vv # Checkout a remote branch git checkout -b local-name origin/remote-branch
Stashing
# Stash current changes git stash git stash save "optional message" # Stash including untracked files git stash -u # List stashes git stash list # Apply the latest stash (keeps it in stash list) git stash apply # Apply a specific stash git stash apply stash@{2} # Pop the latest stash (apply + drop) git stash pop # Drop a specific stash git stash drop stash@{1} # Clear all stashes git stash clear # View the changes in a stash git stash show stash@{0} # View diff of a stash git stash show -p stash@{0}
Undoing Changes
Restore
# Restore a file from the last commit git restore filename # Restore a file from a specific commit git restore -s commit-hash filename # Unstage a file (remove from staging) git restore --staged filename # Unstage all files git restore --staged .
Reset
# Unstage changes (keep changes in working directory) git reset HEAD filename git reset # unstage everything # Undo the last commit (keep changes staged) git reset --soft HEAD~1 # Undo the last commit (keep changes unstaged) git reset HEAD~1 # Undo the last commit (discard changes completely) git reset --hard HEAD~1 # Reset to a specific commit (discard changes) git reset --hard commit-hash # Undo a reset (use git reflog to find the lost commit) git reflog git reset --hard HEAD@{n}
Revert
# Create a new commit that undoes a previous commit git revert commit-hash # Revert the last commit git revert HEAD # Revert without auto-commit (edit the message) git revert -n commit-hash
Remove
# Remove a file from Git and working directory git rm filename # Remove a file from Git only (keep in working directory) git rm --cached filename # Remove a directory git rm -r directory/
Tagging
# List tags git tag # List tags matching a pattern git tag -l "v1.*" # Create a lightweight tag git tag v1.0.0 # Create an annotated tag (recommended) git tag -a v1.0.0 -m "Release version 1.0.0" # Tag a specific commit git tag v1.0.0 commit-hash # Push a specific tag git push origin v1.0.0 # Push all tags git push --tags # Delete a local tag git tag -d v1.0.0 # Delete a remote tag git push origin --delete v1.0.0 # View tag details git show v1.0.0
.gitignore
# Common patterns to ignore # Compiled files *.class *.o *.exe # Dependencies node_modules/ vendor/ # Environment / config .env *.local # Build outputs dist/ build/ target/ # Logs *.log # OS generated files .DS_Store Thumbs.db # IDE files .idea/ .vscode/ *.swp *.swo # View ignored files git status --ignored
Common Workflow Patterns
Feature Branch Workflow
# Start a new feature git checkout main git pull origin main git checkout -b feature/new-feature # Work on the feature # ... make changes ... git add . git commit -m "Add new feature" # Push feature branch git push -u origin feature/new-feature # Merge back to main (after PR/review) git checkout main git pull origin main git merge feature/new-feature git push origin main # Delete the feature branch git branch -d feature/new-feature git push origin --delete feature/new-feature
Hotfix Workflow
# Start a hotfix git checkout main git checkout -b hotfix/urgent-fix # Fix and commit # ... make changes ... git add . git commit -m "Fix critical bug" # Merge hotfix git checkout main git merge --no-ff hotfix/urgent-fix git push origin main # Delete hotfix branch git branch -d hotfix/urgent-fix
Common Commands Reference
Essential Commands
git init– create a new repogit clone– copy a remote repogit status– view changesgit add– stage changesgit commit– save changesgit log– view historygit diff– view differencesgit branch– manage branchesgit checkout– switch branchesgit merge– merge branches
Remote Commands
git remote– manage remotesgit push– upload to remotegit pull– fetch + mergegit fetch– download changesgit clone– initial clone
Undo Commands
git restore– discard changesgit reset– undo commitsgit revert– safe undogit rm– remove filesgit stash– save work temporarily
Best Practices
- Write meaningful commit messages – subject line (50 chars), blank line, body (72 chars).
- Commit early and often – small, focused commits are easier to review and revert.
- Use feature branches – never commit directly to
mainormaster. - Pull before pushing – always
git pullbeforegit pushto avoid conflicts. - Use
--force-with-leaseinstead of--forcewhen force pushing. - Write a
.gitignore– exclude build artifacts, dependencies, and environment files. - Use tags for releases – annotate with version numbers and release notes.
- Review before committing – use
git diffandgit statusbefore staging. - Keep the history clean – use interactive rebase to squash fixup commits before merging.
- Never rebase shared branches – rebasing changes history and causes problems for others.
- Use
git stashto save uncommitted work when switching branches. - Learn
git reflog– it's your safety net for recovering lost commits.
📌 Quick Reference
Check Git version:
View Git config:
Get help:
Visualise history:
git --versionView Git config:
git config --listGet help:
git help commandVisualise history:
git log --oneline --graph --decorate --all