Git and GitHub: A Practical Guide
A practical guide to the Git commands I use daily and how Git works behind the scenes
Getting Started with Git and GitHub
Today, I wanted to refresh my mind on using Git and the Git commands I use regularly.
This article isn't an advanced Git guide. Instead, it's a practical reference for myself and anyone getting started with Git. I'll be explaining what Git is, how to set it up, and the commands you'll most likely use every day.
Like I always say, don't just memorize commands—understand why they exist. Once you understand the concepts behind Git, the commands become much easier to remember.
What is Git?
Git is a Version Control System (VCS).
Git helps you keep track of changes made to your project over time. If something goes wrong, you can always return to an earlier version of your project instead of starting from scratch.
Think of Git as a timeline of your project.
Every time you save your work using a commit, Git creates a snapshot that you can always return to later.
Git helps you:
- Track changes over time.
- Restore previous versions of your project.
- Experiment with new features safely.
- Collaborate with other developers.
What is GitHub?
GitHub is the cloud platform where Git repositories are hosted.
Git works on your computer, while GitHub stores your repositories online so they can be backed up and shared with other developers.
Installing Git
Download Git from the official Git website and install it on your computer.
Once installation is complete, open your terminal and configure Git globally.
Use the same email address connected to your GitHub account.
git config --global user.name "your username"
git config --global user.email "your-email@example.com"
You only need to do this once.
To verify your configuration:
git config --list
Once this is successful, you're ready to start using Git in your projects.
Understanding the Git Workflow
Before learning the commands, it's important to understand how Git stores your work.
flowchart LR
A[Working Directory]
-->B[Staging Area]
-->C[Local Repository]
-->D[GitHub]
Everything starts in your Working Directory, where you write your code.
When you're satisfied with your changes, you move them into the Staging Area using git add.
Next, you create a permanent snapshot using git commit.
Finally, you upload that snapshot to GitHub using git push.
Once you understand these four stages, Git becomes much easier to learn.
Common Git Commands
git init
This command initializes Git in your project for the first time.
It basically turns a normal folder into one that Git can track.
Git also creates a hidden .git folder where it stores all of its metadata, branches, commits, and configuration.
git init
[!NOTE] You only run
git initonce per project.
git status
This command tells you the current status of your project.
It shows:
- Modified files.
- Untracked files.
- Files waiting to be committed.
git status
You'll probably use this command more than any other Git command because it tells you exactly what's happening before making a commit.
git add
Moves files into the staging area.
To add one file:
git add filename
To add every modified file:
git add .
The . simply means everything inside the current folder.
git commit
This command saves everything currently in the staging area.
git commit -m "Describe what changed"
One thing I found interesting about Git is what actually happens when you commit.
When you commit, Git changes your working directory to match the snapshot of the branch you're currently on.
When you later switch branches, Git doesn't magically know which files belong where—it simply restores your project to the snapshot pointed to by that branch.
That's why some files appear and disappear when switching branches.
They're only part of the snapshots where they were committed.
git push
Uploads your local commits to GitHub.
git push
If you're pushing a project for the first time:
- Create a repository on GitHub.
- Copy its repository URL.
- Connect your local project to it.
git remote add origin repository-url
This command tells Git where your remote repository is located.
Next, push your branch.
git push -u origin main
or
git push -u origin master
The -u flag tells Git to remember the remote branch.
After that, future pushes only require:
git push
Summary
| Command | Purpose |
|----------|---------|
| git init | Initializes Git in a project. |
| git status | Shows the current state of your project. |
| git add | Moves files into the staging area. |
| git commit | Saves a snapshot locally. |
| git push | Uploads commits to GitHub. |
What's Next?
Now that you understand the basic Git workflow, the next step is learning how Git helps teams collaborate using branches.
In the next section, we'll cover:
- Creating branches
- Understanding
HEAD - Switching branches
- Fetching changes
- Pulling updates
- Merging branches
- Viewing commit history
- Cloning repositories
Comments (0)
No comments yet. Be the first to share your thoughts.