Introduction

Git is a powerful distributed version control system that has become an essential tool for modern software development. GitHub, a web-based platform built around Git, provides a centralized location to host repositories, collaborate with others, and manage projects.

This guide is designed for beginners and focuses on the 20% of Git and GitHub functionality that you’ll use 80% of the time. By mastering these core concepts, you’ll be well-equipped to handle most day-to-day version control tasks.

Getting Started

After installing Git, configure your identity (you only need to do this once):

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

Core Concepts

Repository (Repo)

A repository is a collection of files and folders, along with their complete history of changes. It’s the fundamental unit of storage in Git.

Commit

A commit is a snapshot of your repository at a specific point in time. Each commit has a unique ID and a message describing the changes made.

Branch

A branch is a parallel version of your repository. It allows you to work on new features or fixes without affecting the main codebase. The default branch is typically named main.

Clone

Cloning creates a local copy of a remote repository on your machine. This allows you to work on the project offline and sync your changes later.

Push and Pull

  • Pushing sends your local commits to a remote repository, sharing your changes with others.
  • Pulling fetches changes from a remote repository and merges them into your local branch.

Essential Git Commands

Getting a Repository

# Get an existing project
git clone <repository-url>

Daily Workflow Commands

# Check what's changed
git status

# Add files to prepare for commit
git add <file-name>
git add .  # Add all changes

# Save your changes with a message
git commit -m "Your descriptive message here"

# Send changes to GitHub
git push origin main

# Get latest changes from GitHub
git pull origin main

Working with Branches

# Create and switch to a new branch
git checkout -b <new-branch-name>

# Switch back to main branch
git checkout main

# Delete a branch when done
git branch -d <branch-name>

Viewing History

# See commit history
git log --oneline

The .gitignore File

This special file tells Git which files to ignore. Create a .gitignore file in your repository root:

# Common things to ignore
node_modules/
.env
*.log
.DS_Store

Fixing Common Mistakes

Undo Staging a File

git reset <file-name>

⚠️ Caution: git reset can be dangerous because it can permanently remove commits or changes when used incorrectly. For unstaging files, git restore --staged <file-name> is generally the recommended approach these days.

Fix Your Last Commit Message

git commit --amend -m "New commit message"

Discard Changes to a File

git checkout -- <file-name>

ℹ️ Note: While git checkout -- <file-name> works, the newer command git restore <file-name> is now recommended for discarding changes to a file, as it has a clearer purpose than the multi-functional git checkout.

Basic GitHub Workflow

This is the everyday workflow you’ll use most often:

  1. Get the repository: git clone <url>
  2. Create a branch for your work: git checkout -b feature-name
  3. Make your changes: Edit files as needed
  4. Stage changes: git add .
  5. Commit changes: git commit -m "Describe what you changed"
  6. Push to GitHub: git push origin feature-name
  7. Create a Pull Request: Go to GitHub and click “New pull request”
  8. Merge and clean up: After approval, merge the PR and delete the branch

Fork and Pull Request Workflow

When contributing to someone else’s project:

  1. Fork the repository on GitHub (creates your own copy)
  2. Clone your fork: git clone <your-fork-url>
  3. Create a branch: git checkout -b your-feature
  4. Make changes, commit, and push to your fork
  5. Create a Pull Request from your fork to the original repository

GitHub Essentials

Issues

GitHub Issues help track bugs, feature requests, and tasks. They’re like a to-do list for your project.

Pull Requests

Pull Requests let you propose changes to a repository. They enable code review and discussion before merging changes.

README Files

Every repository should have a README.md file that explains what the project does and how to use it.

Best Practices

Write Good Commit Messages

# Good
git commit -m "Fix login button alignment on mobile"

# Bad
git commit -m "fix stuff"

Commit Often

Make small, focused commits rather than large ones. It’s easier to track what changed and why.

Always Pull Before Starting Work

git pull origin main

This ensures you have the latest changes before starting your work.

Quick Reference

Here are the commands you’ll use 80% of the time:

git clone <url>           # Get a repository
git status                # Check what's changed
git add .                 # Stage all changes
git commit -m "message"   # Save changes
git push origin <branch>  # Send to GitHub
git pull origin <branch>  # Get latest changes
git checkout -b <branch>  # Create new branch
git checkout main         # Switch to main branch

Conclusion

This guide covers the fundamental commands and concepts you need to start using Git and GitHub effectively. While there’s much more to learn, mastering these basics will handle the vast majority of your version control needs.

The key is to practice these commands regularly. Start with simple personal projects, and gradually you’ll build the muscle memory for these essential Git operations. Remember: everyone makes mistakes with Git at first - it’s part of the learning process!