Beginner's Guide: Git & GitHub Workflow
Git is like a "time machine" for your projects. It saves snapshots (called commits) of your work, so you can go back to earlier versions or track changes. GitHub is an online platform where you can store these snapshots, share them, and collaborate with others.
This guide will walk you through the basics of using Git and GitHub step by step.
Key Concepts
Here are the essential terms you need to know:
- Repository (Repo): A project folder tracked by Git. It contains your files and a hidden
.git
folder where Git stores the history. - Commit: A saved snapshot of your project. You decide when to take a snapshot and add a message describing the changes.
- Branch: A separate line of work. Think of the
main
branch as the trunk of a tree. You can create branches (like limbs) to work on features without affecting the trunk. - Stage (Add): Preparing specific changes for your next snapshot (commit). Think of it as putting items in a box before taking a photo.
- Push: Uploading your local snapshots (commits) to GitHub.
- Pull: Downloading updates from GitHub to your computer.
- Remote: A connection to a repo hosted online (e.g., GitHub). The default name is usually
origin
.
Setting Up Git & GitHub
- Install Git: Download and install Git from git-scm.com.
- Configure Git: Set your name and email (used in commits).
git config --global user.name "Your Name" git config --global user.email "your.email@example.com"
- Set Up SSH Key: This allows secure, password-free communication with GitHub.
- Generate an SSH key:
ssh-keygen -t ed25519 -C "your.email@example.com"
- Copy the public key:
cat ~/.ssh/id_ed25519.pub
- Add it to GitHub: Go to Settings > SSH and GPG keys > New SSH key, paste the key, and save.
- Generate an SSH key:
Basic Git Workflow
Follow these steps to make changes to a project and upload them to GitHub:
-
Navigate to Your Project:
cd ~/Projects/git/my-cool-project
-
Check the Status:
git status
Example Output (if no changes):
On branch main nothing to commit, working tree clean
-
Make Changes: Edit a file (e.g.,
README.md
) in your editor and save it. -
Check Status Again:
git status
Example Output (after editing):
Changes not staged for commit: modified: README.md
-
Stage Your Changes:
git add README.md
To stage all changes:
git add .
-
Commit Your Changes:
git commit -m "Update README with project details"
-
Push to GitHub:
git push
-
Pull Updates (if needed):
git pull
Working with Branches
Branches let you work on new features without affecting the main project.
-
Create & Switch to a New Branch:
git checkout -b new-feature
-
Work & Commit: Make changes, stage them (
git add .
), and commit (git commit -m "Add feature"
). -
Switch Back to Main:
git checkout main
-
Merge Your Branch:
git merge new-feature
-
Push Changes:
git push
-
Delete the Branch (Optional):
git branch -d new-feature git push origin --delete new-feature
Tips for Success
- Use
git status
often to see what’s happening. - Write clear commit messages to explain your changes.
- Always pull updates (
git pull
) before starting new work to avoid conflicts.
Git is a powerful tool, and this guide covers the basics to get you started. Happy coding!