“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 "[email protected]"
git config --global color.ui auto
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
If you want to see only one setting:
git config user.name
Enter fullscreen mode Exit fullscreen mode
2. Starting a Repository
There are two common ways to begin.
Initialize a new project
git init
Enter fullscreen mode Exit fullscreen mode
Clone an existing repository
git clone <repository-url>
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
3. The Daily Git Workflow
This is the cycle you’ll repeat every day.
Check what’s changed
git status
Enter fullscreen mode Exit fullscreen mode
This is usually the first command I run before doing anything else.
Stage your changes
git add filename
Enter fullscreen mode Exit fullscreen mode
or stage everything:
git add .
Enter fullscreen mode Exit fullscreen mode
If you want to stage only parts of a file interactively:
git add -p
Enter fullscreen mode Exit fullscreen mode
That one is especially useful when you want to split a big file into smaller, cleaner commits.
Review your changes
git diff
Enter fullscreen mode Exit fullscreen mode
See staged changes:
git diff --staged
Enter fullscreen mode Exit fullscreen mode
If you want a compact history view while reviewing work:
git log --oneline --graph --decorate --all
Enter fullscreen mode Exit fullscreen mode
Save your work
git commit -m "Add login validation"
Enter fullscreen mode Exit fullscreen mode
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"
Enter fullscreen mode Exit fullscreen mode
If you forgot to add a file after committing, you can amend the last commit:
git commit --amend
Enter fullscreen mode Exit fullscreen mode
4. Working with Branches
Branches let you experiment without touching the main project.
Create a branch:
git branch feature/login
Enter fullscreen mode Exit fullscreen mode
Switch branches:
git checkout feature/login
Enter fullscreen mode Exit fullscreen mode
A more modern way to switch branches:
git switch feature/login
Enter fullscreen mode Exit fullscreen mode
Create and switch in one step:
git switch -c feature/login
Enter fullscreen mode Exit fullscreen mode
View branches:
git branch
Enter fullscreen mode Exit fullscreen mode
View all branches, including remote ones:
git branch -a
Enter fullscreen mode Exit fullscreen mode
Rename a branch:
git branch -m new-branch-name
Enter fullscreen mode Exit fullscreen mode
Delete a branch after merging:
git branch -d feature/login
Enter fullscreen mode Exit fullscreen mode
Merge your work:
git merge feature/login
Enter fullscreen mode Exit fullscreen mode
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>
Enter fullscreen mode Exit fullscreen mode
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>
Enter fullscreen mode Exit fullscreen mode
Upload commits:
git push origin main
Enter fullscreen mode Exit fullscreen mode
If this is your first push on a new branch:
git push -u origin feature/login/main/branch
Enter fullscreen mode Exit fullscreen mode
Download updates and merge them:
git pull
Enter fullscreen mode Exit fullscreen mode
Fetch without merging:
git fetch origin
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
6. Inspecting History
Need to know who changed what?
git log
Enter fullscreen mode Exit fullscreen mode
A cleaner log view:
git log --oneline
Enter fullscreen mode Exit fullscreen mode
Follow changes to a file:
git log --follow filename
Enter fullscreen mode Exit fullscreen mode
Inspect a commit:
git show SHA
Enter fullscreen mode Exit fullscreen mode
Compare branches:
git diff branchA...branchB
Enter fullscreen mode Exit fullscreen mode
See which files changed in a commit:
git show --stat SHA
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
You can also save with a message:
git stash push -m "WIP: login form"
Enter fullscreen mode Exit fullscreen mode
Return later:
git stash pop
Enter fullscreen mode Exit fullscreen mode
Apply stash without removing it:
git stash apply
Enter fullscreen mode Exit fullscreen mode
View saved work:
git stash list
Enter fullscreen mode Exit fullscreen mode
Inspect a stash:
git stash show -p
Enter fullscreen mode Exit fullscreen mode
Delete a stash:
git stash drop
Enter fullscreen mode Exit fullscreen mode
Clear all stashes:
git stash clear
Enter fullscreen mode Exit fullscreen mode
Think of stash as Git’s temporary locker.
8. Moving, Restoring, or Deleting Files
Rename a file:
git mv old.js new.js
Enter fullscreen mode Exit fullscreen mode
Delete a file:
git rm file.js
Enter fullscreen mode Exit fullscreen mode
If you want to restore a file you changed:
git restore filename
Enter fullscreen mode Exit fullscreen mode
Restore a file from staging:
git restore --staged filename
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
Undo the last commit and keep changes in your working directory:
git reset --mixed HEAD~1
Enter fullscreen mode Exit fullscreen mode
Completely remove the last commit and changes:
git reset --hard HEAD~1
Enter fullscreen mode Exit fullscreen mode
โ ๏ธ 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>
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
Interactive rebase is great for cleaning up commits:
git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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/
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
Remove untracked files:
git clean -f
Enter fullscreen mode Exit fullscreen mode
Remove untracked files and directories:
git clean -fd
Enter fullscreen mode Exit fullscreen mode
โ ๏ธ 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
Enter fullscreen mode Exit fullscreen mode
Create an annotated tag:
git tag -a v1.0.0 -m "First stable release"
Enter fullscreen mode Exit fullscreen mode
List tags:
git tag
Enter fullscreen mode Exit fullscreen mode
Push tags to GitHub:
git push origin v1.0.0
Enter fullscreen mode Exit fullscreen mode
Or push all tags:
git push origin --tags
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode
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 remote add origin https://github.com/SSH
Enter fullscreen mode Exit fullscreen mode
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.
๋ต๊ธ ๋จ๊ธฐ๊ธฐ