Git Cheat Sheet: From Basics to Advanced

Introduction

Git is a version control system that helps you track changes in your projects. This cheat sheet covers essential commands for beginners and more advanced users.

Setting Up Git

# Set your name and email (do this first!)
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Check your settings
git config --list

Explanation: These commands tell Git who you are. It’s important for collaboration, as your name and email will be attached to your changes.

Starting a Project

# Create a new Git repository
git init

# Clone an existing repository
git clone https://github.com/username/repository.git

Explanation:

  • git init starts a new project on your computer.
  • git clone copies an existing project from the internet to your computer.

Basic Workflow

# Check the status of your project
git status

# Add changes to staging area
git add filename.txt
git add .  # Add all changes

# Commit your changes
git commit -m "Describe your changes here"

# Send changes to GitHub (or other remote repository)
git push origin main

# Get latest changes from GitHub
git pull origin main

Explanation:

  • git status shows what’s changed in your project.
  • git add prepares changes to be saved.
  • git commit saves your changes.
  • git push uploads your changes to GitHub.
  • git pull downloads the latest changes from GitHub.

Working with Branches

# Create a new branch
git branch new-feature

# Switch to a branch
git checkout new-feature

# Create and switch to a new branch (shortcut)
git checkout -b another-feature

# Merge a branch into your current branch
git merge new-feature

# Delete a branch
git branch -d new-feature

Explanation: Branches let you work on different versions of your project at the same time. They’re great for developing new features or fixing bugs without affecting the main project.

Viewing Project History

# View commit history
git log
git log --oneline  # Compact view

# View changes in a specific commit
git show commit-hash

# View changes before committing
git diff

Explanation: These commands help you see what’s changed in your project over time.

Undoing Changes

# Undo changes in your working directory
git checkout -- filename.txt

# Unstage files (undo git add)
git reset HEAD filename.txt

# Change the last commit
git commit --amend -m "New commit message"

# Reset to a previous commit (be careful!)
git reset --hard commit-hash

Explanation: Sometimes you need to undo changes. These commands help you go back to previous versions of your work.

Advanced Techniques

Stashing Changes

# Temporarily store changes
git stash save "Work in progress"

# List stashed changes
git stash list

# Apply stashed changes
git stash apply stash@{0}

# Apply and remove stashed changes
git stash pop

# Remove a stash
git stash drop stash@{0}

Explanation: Stashing lets you temporarily set aside changes. It’s useful when you need to switch tasks quickly.

Rewriting History

# Combine the last 3 commits
git rebase -i HEAD~3
# In the editor, change 'pick' to 'squash' for commits you want to combine

# Cherry-pick a specific commit
git cherry-pick commit-hash

Explanation: These commands help you clean up your commit history. Use them carefully, especially if you’ve already shared your code.

Debugging

# See who changed each line of a file
git blame filename.txt

# Find which commit introduced a bug
git bisect start
git bisect bad  # Current version is bad
git bisect good commit-hash  # This older version was good
# Git will help you test different versions
git bisect reset  # When you're done

Explanation: These tools help you find when and where bugs were introduced.

Advanced Logging

# View a graph of commits
git log --graph --oneline --all

# Search commit messages
git log --grep="bug fix"

# Search for content changes
git log -S"function_name" --patch

Explanation: These commands give you more detailed information about your project’s history.

Conclusion

This cheat sheet covers both basic and advanced Git usage. Remember, Git is powerful but can be complex. Always be careful when changing history or using advanced commands, especially on shared projects. Practice these commands on a test repository to get comfortable with them.