Git Commands for Beginners: The 12 You Actually Need
Learn the Git commands you actually use every day: init, add, commit, push, pull, branch, merge, stash and how to undo mistakes safely.
By Velloreth
Git Commands for Beginners: The 12 You Actually Need
Git is a system for tracking changes to files over time. It lets you save snapshots of your project, go back to earlier versions, and work on different versions at the same time without overwriting each other’s work.
If you are new to Git, you only need a handful of commands to be productive. This guide covers the ones you will actually use every day.
The Three Areas in Git
Git has three areas:
- Working directory: The files you see and edit on your computer.
- Staging area: Changes you have marked as ready to include in the next snapshot.
- Repository: The committed snapshots and history.
The typical workflow is: change files in the working directory, add the changes you want to the staging area, then commit them to the repository.
Setup
Before you commit anything, tell Git who you are:
git config --global user.name "Your Name"
git config --global user.email "your_email@example.com"
This information is attached to each commit you make.
The Daily Loop
These are the commands you will use every day.
Check Status
git status
Shows which files have changed, which are staged, and what branch you are on. Run this often.
Stage Changes
git add <file>
Adds a file to the staging area, marking it to be included in the next commit. Use git add . to stage all changed files.
Commit Changes
git commit -m "Describe what changed"
Saves the staged changes as a snapshot in the repository with a message describing what changed.
Push Changes
git push
Uploads your committed changes to a remote repository (such as GitHub).
Pull Changes
git pull
Downloads and merges changes from the remote repository into your local copy.
Branches
Branches let you work on separate lines of development without affecting the main code.
Create and Switch to a Branch
git branch feature-name
git checkout feature-name
Or in one command:
git checkout -b feature-name
In newer versions of Git, you can also use git switch branch-name.
Switch Branches
git checkout branch-name
In newer versions of Git, you can also use git switch branch-name.
Merge a Branch
git merge feature-name
Merges the specified branch into your current branch.
Stash
If you have uncommitted changes and need to switch branches, stash your changes:
git stash
Later, restore them:
git stash pop
Undo Safely
Discard Changes in Working Directory
git restore <file>
Discards changes to a file in the working directory, reverting it to the last committed version.
Undo a Commit Safely
git revert <commit-id>
Creates a new commit that undoes the changes from a previous commit. This is safe because it does not rewrite history.
Avoid git reset --hard unless you are certain you want to discard changes permanently. It deletes uncommitted work and cannot be undone easily.
Good Commit Messages
Write commit messages that describe what changed and why:
Fix login redirect after signup
Not:
fix stuff
.gitignore Basics
A .gitignore file tells Git which files to ignore (for example, build output, secrets, and temporary files):
node_modules/
.env
*.log
Questions and Answers
Q: What does Git do?
A: Git tracks changes to files over time by taking snapshots of your project at each commit, letting you see what changed and revert to earlier states.
Q: What are the three areas in Git?
A: The working directory (your files), the staging area (changes prepared for the next commit), and the repository (committed snapshots).
Q: What is the daily Git loop?
A: git status to check status, git add to stage changes, git commit to save them, git push to upload them, and git pull to download changes from others.
Q: How do I undo a mistake safely?
A: Use git restore to discard changes in the working directory, or git revert to create a new commit that undoes a previous commit. Avoid git reset --hard unless you understand it deletes work.
Conclusion
Git is a tool for tracking changes to files over time. The daily loop is git status, git add, git commit, git push, and git pull. Branches let you work on separate lines of development, and git revert lets you undo changes safely without destroying history.
Start with these commands and add more as you need them. Most day-to-day work uses only a handful of commands.
This article is informational and does not cover advanced Git topics such as rebasing, cherry-picking, or complex merge strategies. Check the official Git documentation for more advanced topics.