Git Basics

Setup & Configuration

git config --global user.name "Your Name"
Set your username globally
git config --global user.email "email@example.com"
Set your email globally
git config --list
View all configuration settings
git config --global init.defaultBranch main
Set default branch name to 'main'

Repository Initialization

git init
Initialize a new Git repository
git clone <url>
Clone a remote repository
git clone <url> <directory>
Clone into a specific directory

Basic Commands

git status
Show working tree status
git add <file>
Add file to staging area
git add .
Add all files to staging area
git commit -m "message"
Commit with message
git commit -am "message"
Add and commit tracked files
git log
Show commit history
git log --oneline
Show condensed commit history
git diff
Show changes in working directory
Pro Tip: Use git status frequently to understand what's happening in your repository. It's your best friend for staying oriented!
Always start a new project with git init. It turns your folder into a Git project.
Want to undo changes? git checkout -- filename brings back the last saved version.

Branching & Merging

Branch Management

git branch
List all local branches
git branch <branch-name>
Create new branch
git checkout <branch-name>
Switch to branch
git checkout -b <branch-name>
Create and switch to new branch
git switch <branch-name>
Switch to branch (newer syntax)
git switch -c <branch-name>
Create and switch to new branch
git branch -d <branch-name>
Delete branch (safe)
git branch -D <branch-name>
Force delete branch
Always create a new feature branch using git checkout -b feature-name to keep the master/main branch clean.
Use descriptive branch labels: fix-login-bug or feat-payment-api are easy to track.

Merging

git merge <branch-name>
Merge branch into current branch
git merge --no-ff <branch-name>
Merge with no fast-forward
git merge --squash <branch-name>
Squash merge (combine commits)
git rebase <branch-name>
Rebase current branch onto another
Warning: Never rebase branches that have been pushed to a shared remote repository. Rebase changes commits history which breaks pipelines for others.

Remote Repositories

Remote Management

git remote
List remote repositories
git remote -v
List remotes with URLs
git remote add origin <url>
Add remote repository
git remote remove <name>
Remove remote
git remote rename <old> <new>
Rename remote
Set a new remote destination using: git remote add origin URL.
Use git pull before starting any modifications to fetch team updates and avoid conflicts.

Synchronization

git fetch
Download changes from remote
git pull
Fetch and merge changes
git pull --rebase
Fetch and rebase changes
git push
Push changes to remote
git push -u origin <branch>
Push and set upstream
git push --force-with-lease
Safer force push

Advanced Commands

Undoing Changes

git reset --soft HEAD~1
Undo last commit, keep changes staged
git reset --mixed HEAD~1
Undo last commit, unstage changes
git reset --hard HEAD~1
Undo last commit, discard changes
git revert <commit-hash>
Create new commit that undoes changes
git checkout -- filename
Discard changes in working directory
git restore <file>
Restore file (newer syntax)

Stashing

git stash
Stash current changes
git stash push -m "message"
Stash with message
git stash list
List all stashes
git stash pop
Apply and remove latest stash
git stash apply stash@{n}
Apply specific stash
git stash drop stash@{n}
Delete specific stash

Advanced Operations

git cherry-pick <commit-hash>
Merge specific commit to current branch
git rebase -i HEAD~n
Interactive rebase for last n commits
git bisect start
Start binary search for bugs
git blame <file>
Show who changed each line
git reflog
Show reference log (recover lost commits)
git clean -fd
Remove untracked files and directories

Common Workflows

📤 Local to Remote Workflow

Starting with a local project and pushing to GitHub

1
git init
Initialize local repository
2
git add .
Stage all files
3
git commit -m "Initial commit"
Create first commit
4
git branch -M main
Rename branch to main
5
git remote add origin <repository-url>
Add remote repository
6
git push -u origin main
Push and set upstream

📥 Remote to Local Workflow

Starting with an existing GitHub repository

1
git clone <repository-url>
Clone the remote repository
2
cd <repository-name>
Navigate to project directory
3
git checkout -b feature-branch
Create and switch to new branch
4
# Make your changes
Edit files as needed
5
git add . && git commit -m "Add feature"
Stage and commit changes
6
git push -u origin feature-branch
Push branch to remote

🔄 Collaborative Workflow

Working with team members on shared repository

1
git pull origin main
Get latest changes from main
2
git checkout -b feature/new-feature
Create feature branch
3
# Work on feature
Make your changes
4
git add . && git commit -m "Implement feature"
Commit your work
5
git push -u origin feature/new-feature
Push feature branch
6
# Create Pull Request on GitHub
Open PR for code review
7
git checkout main && git pull
Switch back and update main
8
git branch -d feature/new-feature
Delete local feature branch

GitHub Specific

GitHub CLI Commands

gh repo create <name>
Create new GitHub repository
gh repo clone <user/repo>
Clone GitHub repository
gh pr create
Create pull request
gh pr list
List pull requests
gh pr checkout <number>
Checkout PR locally
gh issue create
Create new issue

SSH Setup

ssh-keygen -t ed25519 -C "email@example.com"
Generate SSH key
eval "$(ssh-agent -s)"
Start SSH agent
ssh-add ~/.ssh/id_ed25519
Add SSH key to agent
cat ~/.ssh/id_ed25519.pub
Display public key (add to GitHub)

Useful GitHub Features

git log --pretty=format:"%h %s" --graph
Pretty commit graph
git shortlog -sn
Contributor statistics
git tag -a v1.0.0 -m "Version 1.0.0"
Create annotated tag
git push origin --tags
Push tags to remote
Add a README.md description block to your remote repo—it serves as the landing page homepage.
Use GitHub Pages to publish static projects directly for free. Setup target in repository settings tab.
GitHub Best Practices:
  • Use conventional commit titles (feat:, fix:, docs:, chore:)
  • Write PR description content detailing changed components and testing
  • Open issues to track bug tickets and enhancements
  • Lock default master/main branch with protection policies

Git Best Practices

Commit Messages

  • ✅ Use present tense: "Add feature" not "Added feature"
  • ✅ Keep first line under 50 characters
  • ✅ Be descriptive and specific
  • ✅ Use conventional commits when possible

Branching

  • ✅ Use descriptive branch names
  • ✅ Keep branches small and focused
  • ✅ Delete merged branches
  • ✅ Use feature/, bugfix/, hotfix/ prefixes

Collaboration

  • ✅ Pull before pushing
  • ✅ Use pull requests for code review
  • ✅ Write clear PR descriptions
  • ✅ Test before committing

Security

  • ✅ Never commit secrets or passwords
  • ✅ Use .gitignore for sensitive files
  • ✅ Use SSH keys for authentication
  • ✅ Enable 2FA on GitHub

Emergency Commands

Use these commands with extreme caution! Incorrect usage can cause permanent local database changes or code loss.
git reflog
Find lost commits (your ultimate savior!)
git reset --hard <commit-hash>
Reset workspace to specific commit (destructive)
git checkout <commit-hash> -- <file>
Restore file state from specific past commit
git fsck --lost-found
Find orphaned or corrupted local database elements