Want to Learn Git and GitHub? A Step-by-Step Guide to Version Control
Source: Dev.to
Introduction
If you’re like many beginners, you’ve probably heard terms like “pushing to main” or “merging branches” and wondered what they mean. This guide walks you through the fundamentals of Git and GitHub, showing how to install them, configure them, and use them to manage your code effectively.
What is Git?
Git is a version control system—a smart history book for your code. Every time you make changes, Git records them, allowing you to:
- Track modifications – see what changed, when, and by whom.
- Revert – go back to an earlier state if something goes wrong.
What is GitHub?
GitHub is a web‑based hosting service for Git repositories (think of it as the cloud for your code). It lets you:
- Store – keep a backup of your projects.
- Share – make your code accessible to others.
- Collaborate – provide a central hub for teamwork without overwriting each other’s work.
- Showcase – build a portfolio of your projects.
Installing Git Bash (Windows)
-
Download Git Bash from the official site.
-
Run the installer and follow the on‑screen instructions. The default options work for most users.
- Choosing the default editor: keep the default (Vim) or select another editor you prefer (VS Code, Notepad++, etc.).
- Adjusting the name of the initial branch: choose “Let Git decide” (it now defaults to
main).
-
Verify the installation: open Git Bash and run
git --versionYou should see the Git version number.
Configuring Git
Before using Git, set your identity:
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
Verify the settings:
git config --global --list
Setting Up a GitHub Account and SSH Key
-
Create a GitHub account.
-
Generate an SSH key (so you won’t be prompted for a password each time):
ssh-keygen -t ed25519 -C "your@email.com"Press Enter through the prompts to accept defaults.
-
Start the SSH agent:
eval "$(ssh-agent -s)" -
Add the SSH key to the agent:
ssh-add ~/.ssh/id_ed25519 -
Copy the public key:
cat ~/.ssh/id_ed25519.pubCopy the displayed string.
-
Add the key to GitHub:
- Go to Settings → SSH and GPG keys → New SSH key.
- Paste the copied key and save.
Initializing a Local Repository
Assume you have a folder named my-cool-app.
cd path/to/my-cool-app
git init
Create a simple file, e.g., readme.txt, with the content “Hello world”.
Add and Commit
git add .
git commit -m "My first save point"
Creating a Remote Repository on GitHub
- On GitHub, click New repository and give it a name.
- Copy the SSH URL that looks like
git@github.com:yourname/your-repo.git.
Link the Remote
git remote add origin git@github.com:yourname/your-repo.git
Push to GitHub
git push -u origin main
Refresh the GitHub page—you should see your code online.
Pulling Changes
If you work on another computer or a collaborator updates the repository, bring those changes to your local copy:
git pull origin main
The “Big Three” Commands
| Action | Command | Purpose |
|---|---|---|
| Add | git add <file> | Stage changes you want to record |
| Commit | git commit -m "message" | Save a snapshot of staged changes |
| Push | git push | Send commits to the remote repository |
Don’t worry if you forget commands at first—searching for them is part of the learning process. Keep practicing, and it will soon feel natural.
Happy coding! 🥳