Introduction
This guide covers essential Git commands and workflows you’ll use daily as a Rails developer, plus some helpful advanced commands for specific situations.
Daily Git Commands
Starting Your Day
# Update your main branch
git checkout main
git pull origin main
# Create a new branch for your work
git checkout -b feature/add-user-authentication
# Check branch status
git status
Working with Changes
# Stage specific files
git add app/models/user.rb
git add app/controllers/users_controller.rb
# Stage all changes
git add .
# Stage parts of files
git add -p
# See what's staged
git diff --staged
# See unstaged changes
git diff
# Commit changes
git commit -m "Add User model and controller"
# Amend last commit (before pushing)
git commit --amend
Pushing and Pulling
# Push your branch
git push origin feature/add-user-authentication
# Pull latest changes from main
git pull origin main
# Pull with rebase
git pull --rebase origin main
Common Scenarios
Fixing Mistakes
# Undo staged changes
git reset HEAD app/models/user.rb
# Discard local changes
git checkout -- app/models/user.rb
# Undo last commit (keeping changes staged)
git reset --soft HEAD^
# Undo last commit (discarding changes)
git reset --hard HEAD^
# Remove untracked files
git clean -fd
Working with Branches
# List all branches
git branch
# Switch to existing branch
git checkout other-branch
# Delete local branch
git branch -d feature/old-feature
# Delete remote branch
git push origin --delete feature/old-feature
# Rename current branch
git branch -m new-branch-name
Stashing Changes
# Basic stash
git stash
# Stash with message
git stash save "WIP: User authentication"
# List stashes
git stash list
# Apply most recent stash
git stash pop
# Apply specific stash
git stash apply stash@{2}
# Drop specific stash
git stash drop stash@{0}
# Clear all stashes
git stash clear
Rails-Specific Scenarios
Handling Database Changes
# Stash everything including untracked files
git stash -u
# Pull latest changes
git pull origin main
# Apply stash and run migrations
git stash pop && rails db:migrate
Managing Credentials
# Check if credentials are tracked
git check-ignore config/credentials.yml.enc
git check-ignore config/master.key
# Remove tracked credentials (if accidentally committed)
git rm --cached config/master.key
Advanced Git Commands
Searching and History
# Search commit messages
git log --grep="authentication"
# Search for code changes
git log -S"def authenticate_user"
# Show file history
git log --follow -p app/models/user.rb
# Show who changed each line
git blame app/models/user.rb
# Find which commit introduced a bug
git bisect start
git bisect bad # Current commit is broken
git bisect good v1.0.0 # This version worked
# Git will help you find the problematic commit
Cleaning and Maintenance
# Remove old branches that were merged
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
# Clean up remote-tracking branches
git remote prune origin
# Compress repository
git gc
# Verify repository integrity
git fsck
Advanced Rebase Operations
# Interactive rebase last 3 commits
git rebase -i HEAD~3
# Squash commits during rebase
# In the interactive rebase editor:
pick f7f3f6d First commit
squash 310154e Second commit
squash a5f4a0d Third commit
# Fixup commit (like squash but discard commit message)
git commit --fixup f7f3f6d
git rebase -i --autosquash HEAD~3
Git Configuration Tips
Essential Configuration
# Set your identity
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# Set default branch name
git config --global init.defaultBranch main
# Set default editor
git config --global core.editor "code --wait"
# Enable color output
git config --global color.ui true
Helpful Aliases
# Add these to your ~/.gitconfig
[alias]
# Quick status
st = status -sb
# Better log
lg = log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit
# Undo last commit
undo = reset HEAD~1 --mixed
# Show changes
changes = diff --name-status
# Amend without editing message
amend = commit --amend --no-edit
Best Practices
Commit Messages
-
Use present tense ("Add feature" not "Added feature")
-
First line should be 50 characters or less
-
Follow with a blank line
-
Provide detailed explanation if needed
Example:
Add user authentication system
- Implement Devise for user management
- Add login/signup forms
- Configure user roles
- Add authentication tests
Branch Management
-
Keep branches focused and short-lived
-
Use descriptive branch names
-
Delete branches after merging
-
Regularly rebase feature branches on main
Pull Request Workflow
-
Create feature branch from main
-
Make small, focused commits
-
Keep branch up to date with main
-
Push branch and create PR
-
Address review comments
-
Merge or rebase when approved
Troubleshooting Common Issues
Merge Conflicts
# When conflict occurs
git status # See conflicted files
git diff # See conflicts in detail
# After resolving conflicts
git add .
git commit # or git rebase --continue
# Abort merge if needed
git merge --abort
Reset Gone Wrong
# Find lost commits
git reflog
# Recover lost commit
git reset --hard HEAD@{1}
Additional Resources
-
Oh Shit, Git! - Fixing Git Mistakes
Tip
|
Keep this guide handy for quick reference. Most developers, even experienced ones, regularly refer to Git documentation and guides. |
Warning
|
Always be careful with destructive commands like git reset --hard and git clean -fd . They cannot be undone!
|