"Git isn't hard. Remembering the commands is."
If you've ever found yourself Googling:
- How do I create a new branch?
-
What's the difference between *
git fetch** andgit pull?* - How do I undo my last commit without breaking everything?
- How do I get back a file I accidentally deleted?
You're definitely not alone.
I used to jump between Stack Overflow, documentation, and terminal tabs every time I needed Git. After a while, I realized something important:
You don't need to memorize Git—you need a practical workflow and a few commands you use every day.
So in this post, I'm sharing a clean Git cheat sheet with the most useful commands for real-world development. Whether you're working on a side project, collaborating with a team, or shipping features to production, this will help you move faster and with more confidence.
Why Git Matters
Git is a distributed version control system that helps developers:
- Track code changes
- Collaborate with teammates
- Restore previous versions
- Experiment safely on branches
- Recover from mistakes
- Ship code with confidence
Whether you're a student, freelancer, or software engineer, Git is one of the most important tools in your workflow.
1. Setting Up Git (One-Time Setup)
Before creating repositories, configure your identity.
git config --global user.name "Your GitHub UserName"
git config --global user.email "your@email.com"
git config --global color.ui auto
This tells Git who is making each commit and enables colorful terminal output for better readability.
You can also check your current configuration anytime:
git config --list
If you want to see only one setting:
git config user.name
2. Starting a Repository
There are two common ways to begin.
Initialize a new project
git init
Clone an existing repository
git clone <repository-url>
Use git init when starting from scratch.
Use git clone when contributing to an existing project.
If you want to see the remote URL of a cloned repo:
git remote -v
3. The Daily Git Workflow
This is the cycle you'll repeat every day.
Check what's changed
git status
This is usually the first command I run before doing anything else.
Stage your changes
git add filename
or stage everything:
git add .
If you want to stage only parts of a file interactively:
git add -p
That one is especially useful when you want to split a big file into smaller, cleaner commits.
Review your changes
git diff
See staged changes:
git diff --staged
If you want a compact history view while reviewing work:
git log --oneline --graph --decorate --all
Save your work
git commit -m "Add login validation"
Think of a commit as taking a snapshot of your project.
A few helpful commit tips:
- Keep messages short and clear
- Use present tense
- Make one commit per logical change
Example:
git commit -m "Fix navbar alignment on mobile"
If you forgot to add a file after committing, you can amend the last commit:
git commit --amend
4. Working with Branches
Branches let you experiment without touching the main project.
Create a branch:
git branch feature/login
Switch branches:
git checkout feature/login
A more modern way to switch branches:
git switch feature/login
Create and switch in one step:
git switch -c feature/login
View branches:
git branch
View all branches, including remote ones:
git branch -a
Rename a branch:
git branch -m new-branch-name
Delete a branch after merging:
git branch -d feature/login
Merge your work:
git merge feature/login
This keeps new features isolated until they're ready.
If you want to bring in just one commit from another branch, use:
git cherry-pick <commit-sha>
That command is incredibly useful when you need a specific fix without merging an entire branch.
5. Syncing with GitHub
After committing locally, you'll eventually want to push your code online.
Add a remote:
git remote add origin <GitHub-repo-url>
Upload commits:
git push origin main
If this is your first push on a new branch:
git push -u origin feature/login/main/branch
Download updates and merge them:
git pull
Fetch without merging:
git fetch origin
A simple way to remember:
- Fetch = Download
- Pull = Download + Merge
- Push = Upload
If you want to inspect the remote branches after fetching:
git branch -r
6. Inspecting History
Need to know who changed what?
git log
A cleaner log view:
git log --oneline
Follow changes to a file:
git log --follow filename
Inspect a commit:
git show SHA
Compare branches:
git diff branchA...branchB
See which files changed in a commit:
git show --stat SHA
These commands are lifesavers when debugging or reviewing code.
7. Save Work for Later with Stash
Imagine you're halfway through a feature when your manager says:
"Can you quickly fix this production bug?"
Instead of committing unfinished work:
git stash
You can also save with a message:
git stash push -m "WIP: login form"
Return later:
git stash pop
Apply stash without removing it:
git stash apply
View saved work:
git stash list
Inspect a stash:
git stash show -p
Delete a stash:
git stash drop
Clear all stashes:
git stash clear
Think of stash as Git's temporary locker.
8. Moving, Restoring, or Deleting Files
Rename a file:
git mv old.js new.js
Delete a file:
git rm file.js
If you want to restore a file you changed:
git restore filename
Restore a file from staging:
git restore --staged filename
These commands are especially helpful when you accidentally edit or remove something you didn't mean to touch.
9. Undoing Changes Safely
Sometimes you need to fix mistakes.
Undo the last commit but keep your changes staged:
git reset --soft HEAD~1
Undo the last commit and keep changes in your working directory:
git reset --mixed HEAD~1
Completely remove the last commit and changes:
git reset --hard HEAD~1
⚠️ Be careful with --hard. It can permanently remove uncommitted work.
If you want to undo a commit safely on a shared branch, use:
git revert <commit-sha>
Unlike reset, revert creates a new commit that reverses the changes. This is usually the safer option for team projects.
10. Rewriting History (Use Carefully)
Sometimes you need to clean up your branch before merging.
git rebase main
Interactive rebase is great for cleaning up commits:
git rebase -i HEAD~3
This lets you squash, reorder, or edit recent commits.
⚠️ These commands are powerful. Avoid using them on shared branches unless you fully understand the consequences.
If something goes wrong, Git often keeps a record of where you were:
git reflog
This command can save you when you think you've lost work.
11. Ignore Files You Don't Want to Commit
Create a .gitignore file.
Example:
node_modules/
.env
logs/
*.log
dist/
Git will ignore matching files, keeping your repository clean.
You can also ignore files globally on your machine if needed, but for most projects, a local .gitignore is enough.
12. Cleaning Up Untracked Files
Sometimes your project gets cluttered with generated or temporary files.
Preview what would be removed:
git clean -n
Remove untracked files:
git clean -f
Remove untracked files and directories:
git clean -fd
⚠️ Use this carefully. Once deleted, those files are gone unless they were tracked or stashed.
13. Tags and Releases
When you're ready to mark a release, tags are very useful.
Create a tag:
git tag v1.0.0
Create an annotated tag:
git tag -a v1.0.0 -m "First stable release"
List tags:
git tag
Push tags to GitHub:
git push origin v1.0.0
Or push all tags:
git push origin --tags
Tags are perfect for versioning releases and deployments.
My Favorite Git Workflow
Edit Code
↓
git status
↓
git add .
↓
git commit -m "Meaningful message"
↓
git pull
↓
Resolve conflicts (if any)
↓
git push origin main
Simple.
Repeat.
Ship.
Bonus: A Few Commands I Use All the Time
Here are some extra Git commands that are worth remembering:
git remote -v
git branch -a
git switch -c feature/new-ui
git add -p
git commit --amend
git revert <commit-sha>
git reflog
git stash push -m "WIP"
git clean -fd
git tag v1.0.0
If you learn just these, you'll already be ahead of many developers who only know the basics.
Final Thoughts
Git can feel overwhelming at first because there are so many commands.
The good news?
You only need about 15–20 commands to be productive every day.
Once these become muscle memory, Git transforms from something intimidating into one of the most powerful tools in your development workflow.
The real secret is not memorizing everything—it’s understanding the workflow:
- check status
- stage changes
- commit often
- branch for features
- pull before pushing
- stash when interrupted
- revert safely when needed
That’s the rhythm of working with Git.
Quick Question 👇
What's the Git command you always forget?
Mine used to be:
git stash pop
Drop your answer in the comments—let's build a community cheat sheet together!
⭐ If this article helped you, consider leaving a ❤️ and sharing it with another developer who's learning Git.
Top comments (0)