Git Tutorial for Beginners: The Commands You Actually Need
General • Tools & Setup • 8 min read
Learn the Git commands that 90% of developers use daily. init, add, commit, push, pull, branch, and merge explained simply.
Git Tutorial for Beginners: The Commands You Actually Need
Git is one of the most essential tools for developers, yet it can seem intimidating at first. This tutorial covers the core commands you’ll use daily to manage your code effectively.
What is Git and Why Does It Matter?
Git is a version control system that tracks changes to your code over time. Think of it as a time machine for your project — you can see every change ever made, revert to previous versions, and collaborate with others without overwriting each other’s work.
Getting Started
First, make sure Git is installed on your computer. Then open a terminal and navigate to your project folder.
Initialize a Repository
To start tracking a project with Git, you need to initialize a repository:
git init
This creates a hidden .git folder that stores all the version history for your project.
Saving Changes
The core workflow in Git involves staging changes and then committing them.
Stage Changes
Before saving, you tell Git which changes to include:
git add filename.txt # Stage a specific file
git add . # Stage all changes
Commit Changes
Now save those staged changes with a descriptive message:
git commit -m "Add login functionality"
Each commit is a snapshot of your project at a specific point in time.
Checking Your Status
Use git status to see what’s changed and what’s staged:
git status
This shows you which files are modified, staged, or untracked.
Viewing History
To see your commit history:
git log --oneline
This displays a condensed list of commits with their messages.
Working with Remote Repositories
Git becomes powerful when you connect to remote repositories like GitHub.
Push Your Code
Send your local commits to a remote repository:
git push origin main
Pull Updates
Get the latest changes from the remote repository:
git pull origin main
Branching and Merging
Branches let you work on features independently without affecting the main codebase.
Create a Branch
git branch feature-login
Switch to a Branch
git checkout feature-login
Merge Branches
After finishing your feature, merge it back to main:
git checkout main
git merge feature-login
A Common Workflow
Here’s a typical day-to-day workflow:
git pull origin main
git checkout -b new-feature
# Make your changes
git add .
git commit -m "Add new feature"
git push origin new-feature
# Create a pull request on GitHub
Note:
Remember that Git and GitHub are different things. Git is the version control system that runs on your computer, while GitHub is a website that hosts Git repositories online. You can use Git without GitHub, but GitHub makes collaboration much easier.Common Pitfalls
Avoid these common mistakes:
- Committing too often without meaningful messages
- Not pulling before starting new work
- Trying to push without committing first
- Not using branches for new features
Practice these commands regularly, and they’ll become second nature. Git has a learning curve, but mastering these basics will make you a more effective developer.