1. Getting Started
| Command |
Description |
Example |
git config |
Configure Git settings |
git config --global user.name "Your Name" |
git init |
Initialize new repository |
git init project-name |
git clone |
Clone existing repository |
git clone https://github.com/user/repo.git |
git help |
Get help about Git |
git help commit |
Initial Setup:
# Set your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default editor
git config --global core.editor "code --wait" # VS Code
# or
git config --global core.editor "nano"
# Enable color output
git config --global color.ui true
# Set default branch name
git config --global init.defaultBranch main
3. Branching
| Command |
Description |
Example |
git branch |
List/create branches |
git branch feature-x |
git checkout |
Switch branches |
git checkout main |
git switch |
Newer way to switch branches |
git switch feature-x |
git merge |
Merge branches |
git merge feature-x |
git rebase |
Reapply commits on top of another branch |
git rebase main |
git stash |
Temporarily save changes |
git stash push -m "Work in progress" |
Branching Strategies:
- Feature branches - One branch per feature
- Git Flow - Formalized branching model with develop/main branches
- GitHub Flow - Simpler model with just main and feature branches
- Trunk-based development - Small, frequent commits to main branch
# Create and switch to new branch
git checkout -b feature-x
# Work on branch, make commits
git add .
git commit -m "Implement feature X"
# Switch back to main
git checkout main
# Merge feature branch
git merge feature-x
# Delete feature branch
git branch -d feature-x
4. Remote Repositories
| Command |
Description |
Example |
git remote |
Manage remote repositories |
git remote -v |
git fetch |
Download objects from remote |
git fetch origin |
git pull |
Fetch and merge remote changes |
git pull origin main |
git push |
Upload local changes to remote |
git push origin feature-x |
git remote add |
Add new remote |
git remote add upstream https://github.com/original/repo.git |
Common Remote Workflow:
# Fork a repository (on GitHub/GitLab)
# Clone your fork locally
git clone https://github.com/yourname/repo.git
cd repo
# Add original repo as upstream
git remote add upstream https://github.com/original/repo.git
# Create feature branch
git checkout -b feature-x
# Make changes and commit
git add .
git commit -m "Add new feature"
# Push to your fork
git push origin feature-x
# Create pull request (on GitHub/GitLab UI)
# To sync with upstream
git fetch upstream
git merge upstream/main
5. Inspecting History
| Command |
Description |
Example |
git log |
Show commit history |
git log --oneline --graph |
git show |
Show details of a commit |
git show abc123 |
git blame |
Show who changed each line |
git blame file.txt |
git bisect |
Binary search through history |
git bisect start |
git tag |
Mark specific points in history |
git tag -a v1.0 -m "Version 1.0" |
# Show compact history with graph
git log --oneline --graph --all
# Show changes in last commit
git show HEAD
# Show changes between two commits
git diff abc123 def456
# Find when a bug was introduced
git bisect start
git bisect bad # Current version is bad
git bisect good abc123 # Commit abc123 is known good
# Git will checkout midpoint, you test and mark good/bad
git bisect reset # When done
6. Undoing Changes
| Situation |
Command |
Example |
| Unstage file |
git restore --staged |
git restore --staged file.txt |
| Discard local changes |
git restore |
git restore file.txt |
| Amend last commit |
git commit --amend |
git commit --amend -m "New message" |
| Revert a commit |
git revert |
git revert abc123 |
| Reset to previous commit |
git reset |
git reset --hard HEAD~1 |
| Recover deleted file |
git checkout |
git checkout HEAD -- file.txt |
Reset Types:
--soft - Moves HEAD but keeps changes staged
--mixed (default) - Moves HEAD and unstages changes
--hard - Discards all changes (be careful!)
# Change last commit (before pushing)
git add forgotten-file.txt
git commit --amend --no-edit
# Undo a public commit (creates new revert commit)
git revert abc123
# Completely remove last local commit (use with caution)
git reset --hard HEAD~1
# Recover file deleted in last commit
git checkout HEAD -- deleted-file.txt
7. Advanced Git
| Command |
Description |
Example |
git cherry-pick |
Apply specific commit to current branch |
git cherry-pick abc123 |
git reflog |
Show reference logs (helps recover lost commits) |
git reflog |
git filter-branch |
Rewrite history (caution!) |
git filter-branch --tree-filter 'rm -f password.txt' |
git submodule |
Manage nested repositories |
git submodule add https://github.com/lib/library.git |
git worktree |
Manage multiple working trees |
git worktree add ../hotfix hotfix-branch |
Interactive Rebase:
# Rebase last 3 commits interactively
git rebase -i HEAD~3
# In the editor that opens:
# - Change "pick" to "edit" to modify a commit
# - Change to "squash" to combine with previous commit
# - Change to "reword" to change commit message
# - Delete line to remove commit
# After saving, follow prompts to complete rebase
8. Git Hooks
Git hooks are scripts that run automatically before or after Git commands. They live in the .git/hooks directory.
| Hook |
Trigger |
Common Use |
pre-commit |
Before commit is created |
Run linters, tests |
commit-msg |
After commit message is created |
Validate message format |
pre-push |
Before push to remote |
Run full test suite |
post-merge |
After merge completes |
Install dependencies |
# Example pre-commit hook to run ESLint
#!/bin/sh
# Run ESLint on staged JS files
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep ".js$")
if [[ "$STAGED_FILES" = "" ]]; then
exit 0
fi
echo "Running ESLint on staged files..."
eslint $STAGED_FILES
if [[ $? != 0 ]]; then
echo "ESLint found errors. Commit aborted."
exit 1
fi
exit 0
9. Git Aliases
Create shortcuts for common Git commands in your ~/.gitconfig:
[alias]
co = checkout
br = branch
ci = commit
st = status
last = log -1 HEAD
lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
undo = reset HEAD~1
amend = commit --amend --no-edit
unstage = restore --staged
discard = restore
graph = log --all --graph --decorate --oneline
Useful Aliases:
git config --global alias.s "status -sb" - Short status
git config --global alias.lg "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit" - Pretty log
git config --global alias.undo "reset HEAD~1" - Undo last commit
My simple library
..of useful code