Git Basics: A Beginner's Guide
github.com/kristofer june 2025, v2
What is Git?
Git is a version control system that helps you track changes in your code over time. Think of it as a time machine for your project that lets you save different versions and collaborate with others.
Essential Concepts
Before diving into Git commands, it’s crucial to understand the key concepts that form Git’s foundation. These concepts work together to create a powerful system for managing code changes and collaboration.
- Repository (Repo): Your project’s folder that Git tracks
- Commit: A snapshot of your changes at a specific point in time
- Branch: An independent line of development
- Remote: A copy of your repository hosted somewhere else (like GitHub)
Basic Git Commands
Git commands follow a consistent pattern and are designed to work together as part of a workflow. Most developers use the same core set of commands daily, so mastering these fundamentals will cover the majority of your version control needs. Git commands are typed in the terminal/command prompt and follow the format git <command> <options>
.
Getting Started
When starting with an existing project, you’ll typically clone a repository rather than creating one from scratch. Cloning downloads the entire project history and sets up your local working environment automatically.
1# Clone an existing repository
2git clone <repository-url>
Daily Workflow
This is the core cycle you’ll use every day when working with Git. The workflow follows a logical sequence: check what’s changed, add your changes to staging, commit them with a message, get updates from others, and share your changes. Understanding this flow is essential because it represents how Git manages the transition from your local work to collaborative development.
1# Check status of your changes
2git status
3
4# Add files to staging area
5git add <filename> # Add specific file
6git add . # Add all files
7
8# Commit your changes
9git commit -m "Your message describing the changes"
10
11# Pull updates from remote repository
12git pull origin main
13
14# Push your changes to remote repository
15git push origin main
Working with Branches
Branches are one of Git’s most powerful features, allowing you to work on different features or experiments without affecting the main codebase. Think of branches as parallel universes for your code - you can switch between them, merge changes, and keep different lines of development separate. This is especially important when multiple people are working on the same project or when you want to try something experimental without risking your stable code.
1# Create and switch to a new branch
2git checkout -b <branch-name>
3
4# Switch between branches
5git checkout <branch-name>
6
7# List all branches
8git branch
Best Practices
- Commit Often: Make small, focused commits that are easy to understand
- Write Clear Commit Messages: Begin with a verb and explain what the change does
- Pull Before You Push: Always get the latest changes before pushing your own
- Use Branches: Create separate branches for new features or bug fixes
Common Scenarios
Even experienced developers make mistakes or need to undo changes. Git provides several ways to fix problems, each appropriate for different situations. The key is understanding what each command does and when it’s safe to use.
Undoing Changes
Undoing changes in Git depends on where those changes are: in your working directory (not yet staged), in the staging area (added but not committed), or already committed. Each situation requires different commands, and some operations are irreversible, so it’s important to understand the implications before using them.
1# Undo changes in working directory
2git checkout -- <filename>
3
4# Undo last commit (keeping changes)
5git reset --soft HEAD~1
6
7# Discard all local changes
8git reset --hard HEAD
Resolving Merge Conflicts
Merge conflicts occur when Git can’t automatically combine changes from different sources. This happens when the same lines of code have been modified in different ways. While conflicts might seem intimidating at first, they’re a normal part of collaborative development and following a systematic approach makes them manageable.
- Pull the latest changes
- Git will mark conflicting files
- Open the files and resolve conflicts manually
- Add and commit the resolved files
Getting Help
- Use
git --help
orgit <command> --help
for documentation - Visit Git documentation for detailed guides
- Check GitHub’s Git guides for tutorials
Next Steps
- Learn about
.gitignore
files to exclude files from version control - Explore Git branching strategies like Git Flow
- Practice with interactive tutorials on platforms like GitHub Learning Lab
- Why do you sedom do a
git init
?
Remember: Git has a learning curve, but mastering these basics will make your development workflow much smoother. Start with these fundamentals and gradually explore more advanced features as needed.