Happy new year!
Git is an essential tool for version control in modern development workflows.
While many developers are familiar with basic commands like git add
and git commit
, there are other commands that are not difficult to use, but it takes curiosity to try them and help manage complex codebases.
This blog post dives into practical Git commands and tips to help you level up your Git game, also focuses on a node based app assuming you're using npm/yarn.
Adding files
Git add: Why git add -p is Better Than git add .
When working with Git, adding changes to the staging area is a critical step before committing your work. While git add . is commonly used for convenience, git add -p offers more control and insight into your changes. Here's why git add -p is often the better choice.
When to use git add -p
You’ve made multiple changes, but only want to commit some of them.
You want to separate unrelated changes into different commits.
git add -p
When to Use git add .
git add .
- can be useful when: You’ve made simple, isolated changes, and you’re confident all of them should be staged.
- You’re in a rush to stage everything quickly.
Reverting to a Previous Commit: Targeted Rollbacks
To restore your working directory to the state of a specific commit, use in bash:
git checkout COMMIT_NUMBER .
Replace COMMIT_NUMBER with the SHA of the desired commit I.e. commit # e78897844.
git commit e78897844
Resolving Main Branch Issues: Common Fixes
If you're on the main branch and encounter issues, sometimes after merging code, you just need to remove and install your packages, try these steps:
Clear yarn
/npm
’s cache:
npm cache clean
Remove/delete with bash node_modules
, make sure you use the right version of node
use nvm use
if the project has a defined one and reinstall dependencies:
bash
rm -rf node_modules && npm install
Interactive Rebase: Rewrite History Like a Pro
Interactive rebasing is a powerful way to edit, squash, or remove commits from your branch. Here's how you can use it i.e branch name is main
git rebase -i main
Git will open a text editor (usually Vim). You'll see a list of commits with options like pick
.
To remove a commit, replace pick with d
.
Press ESC
followed by :wq
to save and quit.
git push -f
Combine All Commits into One: Clean Up History
To squash multiple commits into one—or undo a commit—you can use soft and hard resets:
- Soft reset moves back to a previous commit while keeping changes unstaged.
- Hard reset moves back and removes all changes permanently.
Example: Squashing the Last 5 Commits into One
Reset the last 5 commits (replace 5 with your number):
git reset --soft HEAD~5
Stage your changes:
git add .
Commit:
git commit -m "Your new commit message"
Push forcefully:
git push -f
Doing Code Reviews
Code reviews are a critical part of collaborative development, helping teams maintain code quality, share knowledge, and catch issues early. When conducting a review, you should first understand the feature requirements, then systematically examine all changed files.
In GitHub's web interface, there are several ways to compare files between two commits or pull requests. I typically use this URL format:
https://github.com/{owner}/{repo}/compare/{older-commit-hash}...{newer-commit-hash}
an example from a a bootcamp I attended
https://github.com/ExamProCo/free-genai-bootcamp-2025/compare/c890881...70d92251dca35aab4a79003c5f93d5c13efc7f24?diff=split&w=
When you've forked a repository and want to sync with the original author's updates, you can create a pull request comparing the owner's repository and branch to your forked version. The URL structure looks like this:
https://github.com/{original-owner}/{repo}/compare/{their-branch}...{your-username}:{repo}:{your-branch}
a real example
https://github.com/labeveryday/language-learning-assistant/compare/main...karnawis:language-learning-assistant:main
I hope that helps! These are pretty much the commands I use daily—except for cherry-picking, which I’m not a big fan of. 🙂 But hey, everyone has their own workflow! Let me know if you have any questions or if there's anything you'd add to the list.
Top comments (0)