Git for Beginners: How to Push & Pull Code, Track Changes, and Understand Version Control
Source: Dev.to
Git Basics for Beginners
Git is a version‑control tool that helps developers track changes in their code and collaborate safely. Instead of creating multiple versions of the same file, Git records what changed, when it changed, and who made the change. This makes it easy to work in teams and recover from mistakes.
Git works in a project folder called a repository. You work locally on your computer, then sync your changes to a remote repository such as GitHub.
Working with Repositories
Starting a New Project
git init # create a new empty repository
git remote add origin # (optional) link to a remote
Cloning an Existing Project
git clone https://github.com/username/project.git
Checking Repository Status
git status # shows staged, unstaged, and untracked files
Staging Changes
Stage a Specific File
git add path/to/file
Stage All Changes
git add .
Committing Changes
Save a Checkpoint
git commit -m "Your descriptive commit message"
Pushing Changes to Remote
First‑time Push (set upstream)
git push -u origin main
Regular Push
git push
Pulling Changes from Remote
Get the Latest Updates
git pull # fetches and merges remote changes
Tip: Always pull before starting work to avoid conflicts.
Viewing History
Commit Log
git log # shows commit history
See What Changed in Files
git diff # shows unstaged changes
git diff HEAD~1 HEAD # shows changes introduced by the last commit
Common Pitfalls
- Forgetting to pull before working, leading to merge conflicts.
- Writing unclear commit messages.
- Committing broken code.
- Being afraid to make mistakes (Git is designed to recover safely).
Core Commands to Remember
git status– view current repository state.git add– stage changes.git commit– record a snapshot.git push– send commits to the remote.git pull– fetch and integrate remote changes.
Everything else builds on these fundamentals. Happy coding!