Git is the backbone of modern team development, but many developers only use the basic commit, push, and pull. Mastering a few advanced commands can drastically improve your workflow, resolve conflicts faster, and keep your history clean.
Here are 13 genius Git commands you need to master for a lightning-fast team workflow.
I. Cleaning & Rewriting History (The Local Lifesavers)
These commands fix mistakes and tidy up your commits before you push them to the shared repository.
1. git commit --amend
Fix that typo or missed file in your last commit without creating a new one.
- What it does: Replaces your last commit with a new one that includes any staged changes. If you have no staged changes, it lets you edit the previous commit message.
- Use it for: Correcting a commit message, adding a forgotten file, or fixing a small bug immediately after a commit.
2. git reset (Soft, Mixed, Hard)
The ultimate “undo” button.
- What it does: Moves the branch pointer, effectively “un-committing” changes.
--soft: Un-commits, but keeps your files staged (ready to be re-committed).--mixed(default): Un-commits and unstages your files (files are still in the working directory).--hard: Un-commits and deletes all changes from your working directory. Use with extreme caution!
- Use it for: Undoing commits locally, or splitting one large commit into several smaller, cleaner ones.
3. git stash (Push, Pop, Apply)
Save your work-in-progress to switch branches quickly.
- What it does: Temporarily stores changes from your working directory and staging area, giving you a clean working directory.
- Workflow:
git stash push -m "WIP: Feature X": Save current changes.git checkout main: Switch branches to fix a bug.git stash pop: Restore the saved changes onto your new branch.
- Use it for: Quickly context-switching when an urgent task pops up.
II. Synchronization & Collaboration (The Team Tools)
These commands are essential for smoothly integrating your work with the rest of the team.
4. git pull --rebase
A safer, cleaner way to sync with the remote repository.
- What it does: Instead of merging remote changes (which creates an extra “Merge commit”), it first fetches changes from the remote, and then rewrites your local commits to appear after the remote commits. This results in a linear, clean history.
- Use it for: Keeping your feature branch up-to-date with
mainwithout unnecessary merge commits.
5. git cherry-pick <commit-hash>
Apply a specific change from one branch to another without merging the entire branch.
- What it does: Takes the changes introduced by a single commit and reapplies them as a brand-new commit on your currently checked-out branch.
- Use it for: Applying a small, urgent hotfix from a testing branch to the
mainbranch immediately, or moving a utility commit to a different feature branch.
6. git log --oneline --graph
A beautiful, clear visualization of your history.
- What it does: Shows a compact, one-line summary of each commit, including a visual ASCII graph to illustrate branch merges, splits, and relationships.
- Use it for: Quickly visualizing complex branch histories and identifying when features were merged.
7. git blame <file>
Determine who made which change and why.
- What it does: Annotates every line in the given file with the commit SHA, author, and timestamp that last modified that specific line.
- Use it for: Finding the source of a bug, understanding the context of a tricky piece of code, and knowing who to talk to about a change.
III. Advanced Refinement (The Scalability Secrets)
These commands are crucial for maintaining a clean, professional, and easily reviewable codebase.
8. git rebase -i HEAD~N (Interactive Rebase)
The most powerful tool for cleaning up your local history.
- What it does: Opens an interactive editor allowing you to re-order, squash (combine), fixup (combine without message), edit, or drop the last
Ncommits. - Use it for: Turning a messy series of “WIP,” “fix,” and “typo” commits into a few clear, meaningful commits before opening a Pull Request (PR).
9. git diff --staged or git diff --cached
Check exactly what’s going into your next commit.
- What it does: Shows the difference between your staging area and the last commit. This prevents you from accidentally committing unnecessary files or changes.
- Use it for: Final review before running
git commit.
10. git revert <commit-hash>
Undoing commits publicly and safely.
- What it does: Creates a new commit that completely undoes the changes introduced by the specified commit. This is the safe way to undo published commits because it doesn’t rewrite shared history.
- Use it for: Undoing a commit that has already been pushed to the remote repository.
11. git add -p (Patch)
Commit only parts of a modified file.
- What it does: Git goes through every change in the file and asks you interactively if you want to stage that specific “hunk” (block of changes).
- Use it for: Separating unrelated changes in a single file into two different, logical commits (e.g., separating a refactor from a bug fix).
12. git branch -d <branch-name> / git push --delete origin <branch-name>
Clean up local and remote branches.
- What it does: The local command deletes a local branch (use
-Dto force delete an unmerged branch). The remote command deletes the branch on your Git server. - Use it for: Routine cleanup after a feature branch has been merged and closed.
13. git bisect
A powerful tool to find which commit introduced a bug.
- What it does: Performs a binary search through your commit history. You tell Git if a specific commit is “good” (bug free) or “bad” (bug present), and Git automatically jumps to the midpoint to narrow down the search.
- Use it for: Pinpointing exactly when and where a difficult-to-diagnose bug was introduced in your project’s history.