Essential Git commands for version control, branching, merging, and collaboration workflows
Basic Commands
Setup & Config
Initial Git setup
# Set username
git config --global user.name "Your Name"
# Set email
git config --global user.email "your@email.com"
# Check config
git config --list
# Initialize repository
git init
# Clone repository
git clone https://github.com/user/repo.git Staging & Committing
Basic Git workflow
# Check status
git status
# Stage specific file
git add filename.txt
# Stage all changes
git add .
# Commit with message
git commit -m "Add new feature"
# Stage and commit
git commit -am "Fix bug"
# Amend last commit
git commit --amend -m "Updated message" Branching
Branch Operations
Manage Git branches
# List branches
git branch
# Create new branch
git branch feature-name
# Switch to branch
git checkout feature-name
# Create and switch (shorthand)
git checkout -b feature-name
# Delete branch
git branch -d feature-name
# Force delete unmerged branch
git branch -D feature-name Merging
Merge branches
# Merge branch into current
git merge feature-name
# Merge with no fast-forward
git merge --no-ff feature-name
# Abort merge
git merge --abort
# Check merge conflicts
git diff Remote Operations
Remote Commands
Work with remote repositories
# Add remote
git remote add origin https://github.com/user/repo.git
# List remotes
git remote -v
# Push to remote
git push origin main
# Push and set upstream
git push -u origin main
# Pull from remote
git pull origin main
# Fetch without merging
git fetch origin History & Logs
View History
View commit history
# View commit history
git log
# Compact log
git log --oneline
# Graph view
git log --graph --oneline --all
# Show specific file history
git log -- filename.txt
# Show changes in commit
git show commit-hash Undoing Changes
Undo and reset operations
# Discard changes in working directory
git checkout -- filename.txt
# Unstage file
git reset HEAD filename.txt
# Undo last commit (keep changes)
git reset --soft HEAD~1
# Undo last commit (discard changes)
git reset --hard HEAD~1
# Revert commit (create new commit)
git revert commit-hash Stashing
Stash Commands
Temporarily save changes
# Stash changes
git stash
# Stash with message
git stash save "Work in progress"
# List stashes
git stash list
# Apply latest stash
git stash apply
# Apply and remove stash
git stash pop
# Drop stash
git stash drop stash@{0}
# Clear all stashes
git stash clear Find this helpful?
Check out more cheatsheets and learning resources, or get in touch for personalized training.