Useful Git Commands¶
Git command cheatsheet for daily use in the course.
Basic Commands¶
Initial Configuration¶
# Configure name
git config --global user.name "Tu Nombre"
# Configure email
git config --global user.email "tu@email.com"
# View configuration
git config --list
# View specific configuration
git config user.name
Clone and Update¶
# Clone your fork
git clone https://github.com/TU_USUARIO/ejercicios-bigdata.git
# Enter the folder
cd ejercicios-bigdata
# Add upstream (professor's repo)
git remote add upstream https://github.com/TodoEconometria/ejercicios-bigdata.git
# View configured remotes
git remote -v
Daily Work¶
Status and Changes¶
# View current status
git status
# View unsaved changes
git diff
# View changes in a specific file
git diff archivo.py
# View commit history
git log
# View abbreviated history
git log --oneline
# View changes of a specific commit
git show abc123d
Save Changes¶
# Add specific file
git add archivo.py
# Add all modified files
git add .
# Add only Python files
git add *.py
# Make a commit
git commit -m "Descriptive message"
# Commit already tracked files (skip add)
git commit -am "Descriptive message"
# Amend last commit (before push)
git commit --amend -m "New message"
Branches¶
Create and Switch¶
# View local branches
git branch
# View all branches (including remote)
git branch -a
# Create new branch
git branch garcia-ejercicio-01
# Switch to a branch
git checkout garcia-ejercicio-01
# Create and switch in one command
git checkout -b garcia-ejercicio-01
# Switch to main
git checkout main
Merge and Delete¶
# Merge a branch into the current one
git merge branch-name
# Delete local branch
git branch -d garcia-ejercicio-01
# Force delete (if it has unmerged changes)
git branch -D garcia-ejercicio-01
# Delete remote branch
git push origin --delete garcia-ejercicio-01
Synchronization¶
Download Changes¶
# Download changes from professor (upstream)
git fetch upstream
# Download and merge from your fork (origin)
git pull origin main
# View differences with upstream
git log HEAD..upstream/main
# View commits you don't have
git log HEAD..upstream/main --oneline
Full Fork Sync¶
# Complete workflow to sync
git checkout main
git fetch upstream
git merge upstream/main
git push origin main
# Or in a single line
git checkout main && git fetch upstream && git merge upstream/main && git push origin main
Upload Changes¶
# Push current branch to origin
git push origin branch-name
# Push main
git push origin main
# Push and set upstream (first time)
git push -u origin branch-name
# After that you only need
git push
Troubleshooting¶
Undo Changes¶
# Discard changes in a file (before add)
git checkout -- archivo.py
# Discard all unsaved changes
git checkout -- .
# Remove file from staging (after add, before commit)
git reset HEAD archivo.py
# Undo last commit (keeps changes)
git reset --soft HEAD~1
# Undo last commit (discards changes)
git reset --hard HEAD~1
# Go back to a specific commit
git reset --hard abc123d
Be careful with --hard
git reset --hard permanently deletes changes. Only use it if you are sure.
Stash (Temporarily Save)¶
# Temporarily save changes
git stash
# Save with a message
git stash save "WIP: working on exercise 03"
# View stash list
git stash list
# Apply last stash
git stash apply
# Apply and remove last stash
git stash pop
# Apply specific stash
git stash apply stash@{1}
# Remove stash
git stash drop stash@{0}
# Remove all stashes
git stash clear
Conflicts¶
# View files with conflicts
git status
# After resolving manually
git add resolved-file.py
git commit -m "Resolve conflict in file"
# Abort merge with conflicts
git merge --abort
# View merge tool
git mergetool
Information and Search¶
Inspect History¶
# View detailed history
git log --graph --decorate --all
# View who modified each line of a file
git blame archivo.py
# Search in history
git log --grep="keyword"
# View files modified in each commit
git log --stat
# View changes from a specific author
git log --author="Your Name"
Search Code¶
# Search in tracked files
git grep "keyword"
# Search in Python files
git grep "keyword" -- "*.py"
# Search showing line number
git grep -n "keyword"
Shortcuts and Aliases¶
Configure Aliases¶
# Create alias for status
git config --global alias.st status
# Create alias for checkout
git config --global alias.co checkout
# Create alias for commit
git config --global alias.ci commit
# Create alias for branch
git config --global alias.br branch
# Alias for pretty log
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"
Use Aliases¶
# Instead of: git status
git st
# Instead of: git checkout main
git co main
# Instead of: git commit -m "message"
git ci -m "message"
# View pretty log
git lg
Course Workflow¶
Start a New Exercise¶
# 1. Update main
git checkout main
git pull origin main
git fetch upstream
git merge upstream/main
# 2. Create branch for exercise
git checkout -b garcia-ejercicio-01
# 3. Work...
# ... edit files ...
# 4. Save work
git add .
git commit -m "Implement SQLite data loading"
# 5. Push to GitHub
git push -u origin garcia-ejercicio-01
Update Exercise Branch¶
# If the professor added changes while you were working
git checkout main
git fetch upstream
git merge upstream/main
git checkout garcia-ejercicio-01
git merge main
git push origin garcia-ejercicio-01
Apply Professor's Feedback¶
# 1. Make sure you are on your branch
git checkout garcia-ejercicio-01
# 2. Make corrections
# ... edit files ...
# 3. Save and push
git add .
git commit -m "Apply feedback: optimize queries"
git push origin garcia-ejercicio-01
# The PR updates automatically
Advanced Commands¶
Cherry Pick¶
# Apply a specific commit to the current branch
git cherry-pick abc123d
# Apply without automatic commit
git cherry-pick -n abc123d
Rebase¶
# Rebase current branch with main
git rebase main
# Interactive rebase (last 3 commits)
git rebase -i HEAD~3
# Continue rebase after resolving conflicts
git rebase --continue
# Abort rebase
git rebase --abort
Be careful with Rebase
Do not rebase commits that you have already pushed to GitHub (after push).
Reflog¶
# View history of all actions
git reflog
# Recover a "lost" commit
git reflog
git checkout abc123d
git checkout -b recovered-branch
Tricks and Tips¶
Useful Configuration¶
# Colorize output
git config --global color.ui auto
# Default editor (VSCode)
git config --global core.editor "code --wait"
# Temporarily save credentials
git config --global credential.helper cache
# Permanently save credentials (Windows)
git config --global credential.helper wincred
# Ignore file permission changes
git config core.fileMode false
.gitignore¶
Create a .gitignore file in the project root:
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
env/
venv/
.venv/
# PyCharm
.idea/
# VSCode
.vscode/
# Jupyter
.ipynb_checkpoints/
# Large data files
*.csv
*.db
*.parquet
datos/grandes/
# System
.DS_Store
Thumbs.db
Common Errors and Solutions¶
"fatal: not a git repository"¶
# Solution: Navigate to the project folder
cd path/to/ejercicios-bigdata
# Verify you are in the correct folder
git status
"Your branch is behind 'origin/main'"¶
"CONFLICT (content): Merge conflict"¶
# Solution:
# 1. Open the file with the conflict
# 2. Look for the markers <<<<<<< ======= >>>>>>>
# 3. Manually edit, choose which code to keep
# 4. Remove the markers
# 5. Save and commit
git add resolved-file.py
git commit -m "Resolve conflict"
"Permission denied (publickey)"¶
# Solution: Use HTTPS instead of SSH
git remote set-url origin https://github.com/TU_USUARIO/ejercicios-bigdata.git
"error: failed to push some refs"¶
# Cause: Your local branch is behind the remote
# Solution: Pull first
git pull origin your-branch
# Then push
git push origin your-branch
Emergency Commands¶
Recover Lost Work¶
# View all changes
git reflog
# Go back to a previous state
git reset --hard abc123d
# Recover a deleted file
git checkout HEAD -- archivo.py
Clean Repository¶
# Remove untracked files (dry run)
git clean -n
# Remove untracked files (execute)
git clean -f
# Remove untracked files and folders
git clean -fd
# Include files ignored in .gitignore
git clean -fdx
Additional Resources¶
Built-in Help¶
# General help
git help
# Help for a specific command
git help commit
git commit --help
# Short help version
git commit -h
Useful Links¶
---
Next Steps¶
Now that you know the essential commands:
- Fork and Clone - Initial project setup
- Sync Fork - Keep your fork up to date
- Submission Guide - How to submit exercises